Reference:C Functions and Keywords/ANSI-ISO Standard C/printf()
From CoderGuide
| Language Cross Reference |
|---|
printf function (and variants)
#include <stdio.h> int printf(cont char *format, ...); int fprintf(FILE *stream, const char *format, ...); int sprintf(char *str, const char *format, ...); int snprintf(char *str, size_t size, const char *format, ...); #include <stdarg.h> int vprintf(const char *format, va_list ap); int vfprintf(FILE *stream, const char *format, va_list ap); int vsprintf(char *str, const char *format, va_list ap); int vsnprintf(char *str, size_t size, const char *format, va_list ap);
printf is used to create a formatted text string and output it to a stream. The standard printf function will send the formatted string to stdout (normally the console). The 'f' prefix are for functions that output to a stream or a FILE opened with fopen(). The 's' means that the output is to be send to a character array str; for these string functions, the 'n' prefix is used for those functions that will limit the size of the formatted string to size_t size to prevent buffer overruns. Finally, the 'v' prefix are for functions that use va_list (variable argument list) to allow you to pass the function an arbitrary number of parameters at runtime. The return value is the number of characters actually written.
The Format String
| Code | Function |
|---|---|
| %s | display a string |
| %d or %i | display a signed integer |
| %u | display an unsigned integer |
| %ld | display a signed long integer |
| %lu | display an unsigned long integer |
| %lld | display a signed long long integer |
| %h | display a short integer |
| %hu | display an unsigned short integer |
| %f | display a float or double |
| %L | display a long double |
| %dx | Display an int in hexadecimal (lower case) |
| %dX | display an int in hexadecimal (upper case) |
| %e | display a float in scientific notation |
| %E | same as above, but the 'e' is upper case |
| %'.2f | display a float, rounding up to the nearest 1/100th place |
| %5.2f | Same as above, but also left justify to fit in a minimum space of 5 characters |
| %8d | right justify an int to fit in 8 characters |
| %08d | Same as above, but pad space with zeros |
| % 8d | Same as above, but pad with spaces |
| %-10d | Left justify the number to fit in 10 character |
| %*d %*s %-*d ...etc | Specifies that the width should be read as the next argument, and then the value to be printed, for example: printf("%*d",width,number); printf("%*.*f",width,precision,number); |
| %% | displays a single percent sign |
| %z | display a size_t argument |
| %t | display a ptrdiff_t argument |
| %lc | display a wchar_t |
| %ls | display a wchar_t * array |
| %n | does not display. Argument must be a integer pointer. printf will put the count of the number of characters currently written to the string into the argument |
| %p | print the pointer reference |
Examples
The following examples were shamelessly lifted from the GNU Glib man pages:
#include <math.h> #include <stdio.h> int main(){ /*print Pi to 5 decimal places*/ fprintf(stdout, "pi = %.5f\n", 4 * atan(1.0)); /*print the date/time in the form of ‘Sunday, July 3, 10:02’*/ {/*stard codeblock*/ char *weekday="Sunday"; char *month="July"; int day=3; int hour=10; int min=2; fprintf(stdout, "%s, %s %d, %.2d:%.2d\n",weekday, month, day, hour, min); }/*end code block*/ }
For specifics relating to your implementation, see printf(3) Unix man page. We have only covered those format strings which are defined in the C99/ISO standards.

