Chapter Contents

Previous

Next
printf

printf



Write Formatted Output to the Standard Output Stream

Portability: ISO/ANSI C conforming, UNIX compatible


SYNOPSIS
DESCRIPTION
RETURN VALUE
DIAGNOSTICS
IMPLEMENTATION
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <stdio.h>

int printf(const char *format, var1, var2, ...);


DESCRIPTION

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 description for fprintf for detailed information concerning the formatting conversions.


RETURN VALUE

printf returns the number of characters transmitted to stdout .


DIAGNOSTICS

If there is an error during output, printf returns a negative value.


IMPLEMENTATION

printf is identical to fprintf with stdout as the output file.


EXAMPLE

This example displays a number of integer and floating-point values using different 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%-15s\n\n",
          "%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%-15x\n",
             values[i],values[i],values[i],values[i],
             values[i]);

   printf("\nFloating-point formats:
           \n%-16s%-16s%-16s%-16s%-16s\n\n",
          "%.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.5f\n",
             fvalues[i],fvalues[i],fvalues[i],fvalues[i],
             fvalues[i]);
}


RELATED FUNCTIONS

fprintf , sprintf , vprintf


SEE ALSO


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.