Updated May 6, 2023
Difference Between sprintf vs printf
The following article provides an outline for sprintf vs printf. sprintf() stands for ‘string print formatted’, a function used to copy or write the character stream in the string buffer of computer memory instead of printing it on the console. It takes at least two parameters, the first being the name of the character array or the pointer pointing to the character buffer memory. The return type of sprintf() is int, and it returns the number of characters that are written into the character buffer.
The printf() function is used in C programs to print the output on the standard console, which is generally the output screen, although we can programmatically change the standard output to another location. It takes at least one parameter: the message and the format of the string that needs to be printed. The return type of printf() function is ‘int’ which returns the number of characters printed on the standard output.
Head to Head Comparison Between sprintf vs printf (Infographics)
Below are the top 5 differences between sprintf vs printf:
Key Difference Between sprintf vs printf
Let us discuss some of the major key differences between sprintf vs printf:
- First, function printf() is used to print the character stream of data having the message with all the values and variables on the stdout console, whereas sprintf() function does not print the message on the console; instead, it stores the character stream on char buffer as mentioned in the sprintf() function.
- The basic syntax of using the printf() function in the code is:
int printf (const char *format, . . . )
Where,
- format: It specifies the string format that needs to be printed on the console with the help of format specifiers like %f, %s, %d, etc.
- . . . : Defines the list of arguments. It can be of any length, depending on the code requirements.
- Return Type: Return type of printf() function is ‘int’ which specifies the number of characters printed on the console. It returns the negative value if the output is wrong.
Whereas the basic syntax of using the sprintf() function in the program is:
int sprintf (char *str, const char *format, . . .)
Where,
- char *str: It is a character array where the formatted character stream will be copied.
- format: It specifies the string format that needs to be printed on the console with the help of format specifiers like %f, %s, %d, etc.
- . . . : Defines the list of arguments. It can be of any length, depending on the code requirements.
- Return Type: Return type of sprintf is ‘int’ as it specifies the number of characters stored/ copied in the character buffer, excluding all the null characters.
In case of an error, sprintf() function returns -1.
- printf() function stands for ‘print formatted’, which prints output on the standard console, whereas sprintf() stands for ‘string print formatted’, which actually does not print anything but loads the buffer with the character stream.
- sprintf() function has one extra parameter in the syntax as compared to the printf() function. This extra parameter is the first parameter which points to the memory location, which will receive the result in the buffer. This memory location has enough space to store the whole formatted result. This extra parameter is the either name of the pointer which dynamically allocated the character buffer or the pointer created to point to an already existing char array, whereas printf() function does not have that extra parameter as by default, it stores the output on the standard console instead of any particular location in the memory pointed out by the pointer.
- There can be any number of arguments in both the printf() and sprintf() functions, depending on the program requirement. However, there is a minimum of 1 argument required in the printf() function to specify the format string, while a minimum of 2 arguments are required for the sprintf() function, which includes the character buffer to store the result and the format string.
- Both the sprintf() and printf() function performs the same task of printing the formatted string, but the location is different. In the case of the printf() function, location is the standard console, whereas, in the sprintf() function, location is a character buffer in the memory.
Let us see printf() and sprintf() function how they implement them in a program along with their outputs with the help of an example.
Example #1: printf() Function
Code:
#include <stdio.h>
int main()
{
char topic[] = "Learning Functions";
printf("In the above string :");
printf(", the number of characters returned by printf() is : %d",
printf("%s", topic));
return 0;
}
Output:
Example #2: sprintf() Function
Code:
#include<stdio.h>
int main() {
char str[90];
float num1 = 78.90, num2 = 28.89, result;
result = num1 - num2;
sprintf(str, "Result of substraction of values %f and %f is %f" , num1, num2, result);
printf("%s", str);
return 0;
}
Output:
sprintf vs printf Comparison Table
Let’s discuss the top comparison between sprintf vs printf:
sprintf() Function | printf() Function |
sprintf() function writes/ copies the character set in the character buffer as mentioned in the sprintf() function instead of printing on the console. | printf() function writes the message or character stream on the standard output stream or console. |
sprintf() has one extra parameter than the printf() function, which defines the location of memory where the formatted result of the function will be stored in the char buffer. | printf() does not need that extra parameter of the memory location to store the formatted result as it directly prints the output on the standard console. |
Return type of sprintf() function is ‘int’. It defines the number of characters stored/ copied in the char buffer in memory. | Return type of printf() function is ‘int’. It defines the number of characters printed on the standard output. |
A minimum number of arguments that should be there in the sprintf() function is two. | A minimum number of arguments that needs to be present in the printf() function is one. |
Formatted output is sent to the character buffer in the sprintf() function. | Formatted output is sent to the output screen (standard console) in the printf() function. |
Conclusion
The above description clearly explains what the sprintf and printf are and the major differences between the two. Both printf() and sprintf() format a set of characters and send the output to a designated location. The only distinction is the output’s destination. printf() sends the output to the standard console, typically the console screen. While sprintf() sends it to a character buffer defined within the function.
Recommended Articles
We hope that this EDUCBA information on “sprintf vs printf” was beneficial to you. You can view EDUCBA’s recommended articles for more information.