Copyright (c) 1984 by Daniel L. Roady. All rights reserved. ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º THE "STANDARD I/O LIBRARY" º º º º by º º º º Daniel L. Roady º ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ Introduction ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ This is the first in a series of articles describing a "UNIX" compatible implementation of the "Standard I/O Library." The library contains the members listed in the following table and several standard macros for character handling. ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ getchar ³ putchar ³ ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´ ³ printf ³ scanf ³ ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´ ³ fopen ³ fclose ³ ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´ ³ getc ³ putc ³ ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´ ³ ungetc ³ calloc ³ ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´ ³ cfree ³ strlen ³ ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´ ³ strcpy ³ strcat ³ ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´ ³ strcmp ³ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ This article discusses the printf function. The source code for this function can be viewed by pressing the Esc key, followed by the F2 key. ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ Printf Overview ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ Printf provides automatic formatting and type conversions for output to the standard output channel ("stdout"). The printf calling sequence is: printf(control-string, arg1, arg2 ....); The parameter "control-string" is actually a character pointer to a quoted string containing the text to be output. "Control-string" may additionally contain formatting and type conversion specifications. The arguments ( arg1, arg2, etc ) correspond with the conversion specifications in "control-string." A fuller discussion of the printf function can be found in "The C Programming Language" by Kernighan and Ritchie. ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ Printf Source Code package ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ The printf package consists of three externally accessible routines: printf, fprintf, and sprintf. Each routine performs the same formatting and conversion function but the output destinations are different. Printf outputs to stdout; fprintf outputs to a specified file; and sprintf outputs to a memory buffer. A typical call to each routine is shown below. ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ #include ³ ³ ³ ³ FILE *fopen(),*fp; ³ ³ char buffer[133]; ³ ³ ³ ³ main() ³ ³ { ³ ³ printf("It's a small world.\n"); ³ ³ ³ ³ fp = fopen("outfile.txt","w"); ³ ³ fprintf(fp,"It's a small world.\n"); ³ ³ fclose(fp); ³ ³ ³ ³ sprintf(buffer,"It's a small world.\n"); ³ ³ } ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ The printf package is written as three entry points to handle the details of the output selection. Each entry point performs some specific setup on a few global variables and calls the routine "outf." "Outf" scans the control-string, searching for a "%" character. If the character read is not a "%", it is passed to the output routine "putchar." If the character is a "%", the succeeding characters are examined and global variables are initialized to indicate left or right justification, the pad character, field width, precision, long or short, or the conversion type. Once the conversion type is determined, the appropriate parameter is passed to the routine that performs the particular conversion. The conversion routines create ASCII strings from their input parameters. The strings are output, temporarily, to an internal buffer. The routine "obstore" performs this function. When conversion is complete, the routine "bfrout" inserts any necessary padding for left or right justification and outputs the entire converted field via "putchar." "Outf" continues to scan the control-string until the entire control-string has been searched. ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ File Name: ÛÛ c1.txt ÛÛ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ