Keith B. answered 03/27/19
Software Engineer and Math Geek
The printf function prints to the standard output:
x = 7;
printf("x=%d\n",x);
prints x=7 to your screen. printf is used to display data on the screen.
The fprintf function prints, but to a file instead of the screen. It takes an additional parameters, a pointer to a FILE type:
FILE *fptr = fopen("data.txt","w+");
x = 7;
fprintf(fptr,"x=%d\n",x);
fclose(fptr);
Your output will be in the file data.txt. fprintf() is used for such things as creating reports, saving data values for the next time a program runs, etc.
The sprintf function doesn't exactly print, but instead puts the formatted output into a string buffer:
char output[200];
x = 7;
sprintf(output,"x=%d",x);
If you dump (ie, print) the contents of output, you'll find it contains "x=7". sprintf is often used to create formatted strings which can then be displayed, save to files, put into structures, etc. One common use is as a means to convert one type of data (int, float, etc) into string..