Reference:C Functions and Keywords/ANSI-ISO Standard C/printf()

From CoderGuide

Jump to: navigation, search


Language Cross Reference

 C++ Java Perl PHP Python


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

CodeFunction
%sdisplay a string
%d or %idisplay a signed integer
%udisplay an unsigned integer
%lddisplay a signed long integer
%ludisplay an unsigned long integer
%llddisplay a signed long long integer
%hdisplay a short integer
%hudisplay an unsigned short integer
%fdisplay a float or double
%Ldisplay a long double
%dxDisplay an int in hexadecimal (lower case)
%dXdisplay an int in hexadecimal (upper case)
%edisplay a float in scientific notation
%Esame as above, but the 'e' is upper case
%'.2fdisplay a float, rounding up to the nearest 1/100th place
%5.2fSame as above, but also left justify to fit in a minimum space of 5 characters
%8dright justify an int to fit in 8 characters
%08dSame as above, but pad space with zeros
% 8dSame as above, but pad with spaces
%-10dLeft 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
%zdisplay a size_t argument
%tdisplay a ptrdiff_t argument
%lcdisplay a wchar_t
%lsdisplay a wchar_t * array
%ndoes 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
%pprint 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.

Personal tools