Topic : C Lessons
Author : Christopher Sawtell
Page : << Previous 6  Next >>
Go to page :


printf, fprintf, sprintf - print formatted
output.

        SYNOPSIS
                                #include <stdio.h>

                                int printf ( format [ , arg ] ... )
                                char *format;

                                int fprintf ( stream, format [ , arg ] ... )
                                FILE *stream;
                                char *format;

                                int sprintf ( s, format [ , arg ] ... )
                                char *s, *format;


        DESCRIPTION

                                etc... etc...

        The NAME section above is obvious isn't it?

        The SYNOPSIS starts with the line #include <stdio.h>. This tells
        you that you MUST put this #include line in your 'C' source code
        before you mention any of the routines. The rest of the paragraph
        tells you how to call the routines. The " [ , arg ] ... " heiroglyph
        in effect says that you may have as many arguments here as you wish,
        but that you need not have any at all.

  The DESCRIPTION explains how to use the functions.

        Important Point to Note:

        Far too many people ( including the author ) ignore the fact that
        the printf family of functions return a useful number which can be
        used to check that the conversion has been done correctly, and that
        the i/o operation has been completed without error.

  Refer to the format string in the demonstration program above for
        an example of a fairly sophisticated formatting string.

  In order to fix the concepts of printf in you mind, you
        might care to write a program which prints some text in three ways:

a) Justified to the left of the page. ( Normal printing. )
b) Justified to the right of the page.
c) Centred exactly in the middle of the page.

        Suggestions and Hint.

        Set up a data area of text using the first verse of "Quangle" as data.
        Here is the program fragment for the data:-

/* ----------------------------------------- */

char *verse[] =
{
  "On top of the Crumpetty Tree",
  "The Quangle Wangle sat,",
  "But his face you could not see,",
  "On account of his Beaver Hat.",
  "For his Hat was a hundred and two feet wide.",
  "With ribbons and bibbons on every side,",
  "And bells, and buttons, and loops, and lace,",
  "So that nobody ever could see the face",
  "Of the Quangle Wangle Quee.",
  NULL
  };

/* ----------------------------------------- */


  Cut it out of the news file and use it in a 'C' program file called
        verse.c

        Now write a main() function which uses printf alone for (a) & (b)
        You can use both printf() and sprintf() in order to create
        a solution for (c) which makes a good use of the capabilities
        of the printf family. The big hint is that the string controlling
        the format of the printing can change dynamically as program execution
        proceeds. A possible solution is presented in the file verse.c which is
        appended here. I'd like to suggest that you have a good try at making
        a program of you own before looking at my solution.
        ( One of many I'm sure )

/* ----------------------------------------- */

#include <stdio.h>

char *verse[] =
{
  "On top of the Crumpetty Tree",
  "The Quangle Wangle sat,",
  "But his face you could not see,",
  "On account of his Beaver Hat.",
  "For his Hat was a hundred and two feet wide.",
  "With ribbons and bibbons on every side,",
  "And bells, and buttons, and loops, and lace,",
  "So that nobody ever could see the face",
  "Of the Quangle Wangle Quee.",
  NULL
  };

main()
{
        char **ch_pp;

        /*
        ** This will print the data left justified.
        */

        for ( ch_pp = verse; *ch_pp; ch_pp++ ) printf ( "%s\n", *ch_pp );
        printf( "\n" );

        /*
        ** This will print the data right justified.
        **
        **  ( As this will print a character in column 80 of
        **    the terminal you should make sure any terminal setting
        **    which automatically inserts a new line is turned off. )
        */

        for ( ch_pp = verse; *ch_pp; ch_pp++ ) printf ( "%79s\n", *ch_pp );
        printf( "\n" );

        /*
        ** This will centre the data.
        */

        for ( ch_pp = verse; *ch_pp; ch_pp++ )
        {
                int length;
                char format[10];

                length = 40 + strlen ( *ch_pp ) / 2;      /* Calculate the
field length  */
                sprintf ( format, "%%%ds\n", length );    /* Make a format
string.       */
                printf ( format, *ch_pp );                /* Print line of
verse, using  */
                }                            


Page : << Previous 6  Next >>