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. Its return type is ‘int’, returning the number of characters copied to the buffer.
printf() function is used in C programs to print the output on the standard console (generally the output screen, though we can change the standard output programmatically). 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 as many arguments in both the functions depending on the program requirement, but there should be a minimum 1 argument required in the printf() function, whereas a minimum of 2 arguments are required for the sprintf() function.
- 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:
Sr. No | sprintf() Function | printf() Function |
1 | 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. |
2 | 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. |
3 | 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. |
4 | 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. |
5 | 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 the functions, i,e, printf() and sprintf(), performs the same task of formatting the characters set and sending the result somewhere, with the only difference being the location of the result sent. As in printf(), the result is sent to the standard console, which is generally the console screen, and in the sprintf() function, it is the character buffer as defined in the function.
Recommended Articles
This is a guide to sprintf vs printf. Here we discuss sprintf vs printf key differences with infographics and comparison table, respectively. You may also have a look at the following articles to learn more –
41 Online Courses | 29 Hands-on Projects | 305+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses