Dec 082017
UNIX Compat. Std I/O Library.
File LIST1.ZIP from The Programmer’s Corner in
Category C Source Code
UNIX Compat. Std I/O Library.
File Name File Size Zip Size Zip Type
C1.TXT 5248 1718 deflated
LIST1.C 6272 1996 deflated

Download File LIST1.ZIP Here

Contents of the C1.TXT file

Copyright (c) 1984 by Daniel L. Roady.All rights reserved.



Daniel L. Roady


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

getchar putchar

printf scanf

fopen fclose

getc putc

ungetc calloc

cfree strlen

strcpy strcat


This article discusses the printf function. The source
codefor this function can be viewed by pressing the Esc
key, followed by the F2 key.

Printf Overview

Printfprovides automatic formatting and type
conversions for output tothe standard output channel

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.



printf("It's a small world.\n");

fp = fopen("outfile.txt","w");
fprintf(fp,"It's a small world.\n");

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 paddingfor left or right
justification and outputs the entire converted field via

"Outf" continues to scan the control-string until the
entire control-string has been searched.

File Name: c1.txt

 December 8, 2017  Add comments

Leave a Reply