[Solved] What is the difference between sprintf and printf in C? [duplicate]


sprintf formats a string and writes it into the character array specified by the first argument (assuming sufficient space); printf formats a string and writes it to stdout.

Ex: you can do this with sprintf:

char buffer[100];
sprintf(buffer, "My name is %s and I am %d years old", "John Doe", 25);
// buffer now contains "My name is John Doe and I am 25 years old"

However, if you want to write a formatted string to the standard output stream, you need to use printf (or fprintf with stdout as the first argument):

printf("My name is %s and I am %d years old", "John Doe", 25);
// the text "My name is John Doe and I am 25 years old" gets printed to the stdout stream

2

solved What is the difference between sprintf and printf in C? [duplicate]