home *** CD-ROM | disk | FTP | other *** search
- 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 <stdio.h> │
- │ │
- │ 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 ██ │
- └──────────────────────────────────┘
-