#include <stdio.h> int printf(const char *format, var1, var2, ...);
printf
writes output to the standard output stream under the control of
the string addressed by format
. In the argument list following
format
, there may be one or more additional arguments whose values are
to be formatted and transmitted.
The string pointed to by format
is in the same form as that used by
fprintf
. Refer to the fprintf
description for detailed
information concerning the formatting conversions.
printf
returns the number of characters transmitted to stdout
.
printf
returns a negative value.
printf
is identical to fprintf
with stdout
as the output
file.
printf
formats to contrast the behavior of these formats:
#include <stdio.h> int values[] = { 0, 25, 1048576, -1, 6000}; double fvalues[] = { 13, 55555.5, .00034562, 14.99999816, -6.37e11}; main() { int i; /* Label the output columns. */ printf("Integral formats:n%-15s%-15s%-15s%-15s%-15snn", "%d", "%+.5d", "%u", "%#o", "%x"); /* Note: All formats include a "-15" specification to */ /* force them to appear in 15 columns, left-justified. */ for(i = 0; i < sizeof(values)/sizeof(int); ++i) printf("%-15d%+-15.5d%-15u%-#15o%-15xn", values[i],values[i],values[i],values[i], values[i]); printf("nFloating-point formats: n%-16s%-16s%-16s%-16s%-16snn", "%.5e", "%.8e", "%.8g", "%#.8g", "%.5f"); /* Note: All formats include a "-16" specification to */ /* force them to appear in 16 columns, left-justified. */ for(i = 0; i < sizeof(fvalues)/sizeof(double); ++i) printf("%-16.5e%-16.8e%-16.8g%-#16.8g%-16.5fn", fvalues[i],fvalues[i],fvalues[i],fvalues[i], fvalues[i]); }
fprintf
, sprintf
, vprintf
Copyright (c) 1998 SAS Institute Inc. Cary, NC, USA. All rights reserved.