Updated April 1, 2023
Introduction to sprintf in C
The sprintf in C is defined as a function within the programming language C where a string is composed but not necessarily printed. Using the function printf, one can print the string message in the console, but when one uses the function sprintf, instead of getting printed, the string content is stored in the buffer as a C String and is also pointer by a pointer. While using this function in building the application, one must make sure that the size of the buffer be large enough that the entire resulting string can be stored. While storing a character, a null character is appended automatically after storage content is prepared and stored. There are different parameters in which the function takes in.
Syntax of sprintf in C
The idea of having sprintf started gaining popularity when it became an alternative approach to look at the storing the printable messages instead of printing them in the console so that they can be referred to anytime as per the need of the application. sprintf stands for “String Print”, and here we will look at the syntax perspective of the sprintf function in C and understand the parameters or arguments in the syntax.
Declaration of the function to be declared in C:
int sprintf(char *str, const char *format, [arg1, arg2, ... ]);
Here, the *str is the pointer to the buffer where the C String is stored as a result of the declaration of the function in C. The buffer pointer needs to be large enough so that the entire resulting string can be stored. The *format is the argument that signifies the C string to describe the output and contain a placeholder for integer arguments so that they can be inserted in the formatted string and follow the same specification as printf( ). Finally, the [arg1, arg2, … ] are the integer arguments that we need to convert into the string buffer. Again, there is a particular format which needs to be followed while declaring the *format.
How sprintf Works in C?
- The working of sprintf is exactly the same as that of printf in C language. The first argument to be passed in the function is *str. This is the pointer of the array where the char elements will be stored, or in other words, the data will be written. We would need to declare this buffer before hand in order to use it later on in the application. The next argument is the format argument that holds the text, which needs to be written into the buffer array to which the first argument points to. The argument can contain embedded format tags that specific values can replace and formatted as per the use case requirement. The prototype of the format tag is %[flags][width][.precision][length]specifier.
- Each of the elements within [ ] brackets and the one outside has some special identities and needs to be used as per the use case. Specifier element denotes the data type of the data which needs to be written and is a must argument that needs to be passed to the sprintf function. Flags element allows developers to format the text that needs to be written by either justifying left or right or forcing a result with a +/- sign etc. The width element signifies the amount of data that needs to be stored in the buffer array. Precision element is used for denoting any special precision is required for the use case. And finally, the length element is used for interpretation of the data, which needs to be stored in the buffer array, for example, if it is a short int or unsigned short int or long int or any other type.
- Finally, after all the arguments are passed, the function tries to interpret each of the arguments and assigns space in the buffer array and returns the characters which are written excluding the null-character and is appended into a string and only happen if the execution is successful else gives a negative number in case of failure.
Examples of sprintf in C
Given below are the examples of sprintf in C:
Example #1
The basic program to get the hang of it.
Syntax:
#include <stdio.h>
int main() {
float pi = (float) 22/7;
printf("Value of pi is: %f\n", pi);
char buffOut[50];
sprintf(buffOut, "%f", pi);
printf("The message in form of string is stored as %s", buffOut);
}
Output:
Example #2
Topping up the basic program with multiple arguments.
Syntax:
#include <stdio.h>
int main() {
char bufferOut[50];
int arg1 = 27, arg2 = 9, result;
result = arg1 * arg2;
sprintf(bufferOut, "When %d is multiplied with %d the result is: %d", arg1, arg2, result);
printf("%s", bufferOut);
return 0;
}
Output:
Example #3
Get the length of the resultant string using the command sprintf from example 1.
Syntax:
#include <stdio.h>
int main() {
int len;
float pi = (float) 22/7;
printf("Value of pi is: %f\n", pi);
char buffOut[50];
len = sprintf(buffOut, "%f", pi);
printf("The message in form of string is stored as %s and its length is %d", buffOut, len);
}
Output:
Carefully notice that the number of digits in the variable pi is 7 and 1 decimal point, making it a total length of 8.
Conclusion
To conclude, we have seen the syntax and arguments passed to the sprintf function and looked at the different varieties of examples possible for a starter case. Readers are encouraged to use the prototype version of the *format argument to have more hands-on experience.
Recommended Articles
This is a guide to sprintf in C. Here we discuss the introduction, how sprintf works in c? and examples for better understanding. You may also have a look at the following articles to learn more –