Input/output

Syntax

int getc( FILE *stream );

int getchar( void );

Parameter Description

stream Current stream

The getc routine reads a single character from the stream position and increments the associated file pointer (if there is one) to point to the next character. The getchar routine is identical to getc(stdin).

Return Value Both getc and getchar return the character read. A return value of EOF indicates an error or end-of-file condition. Use ferror or feof to determine whether an error or end-of-file occurred.

Syntax

char *gets( char *buffer );

Parameter Description

buffer Storage location for input string

The gets function reads a line from the standard input stream stdin and stores it in buffer. The line consists of all characters up to and including the first newline character ('\n'). The gets function then replaces the newline character with a null character ('\0') before returning the line. In contrast, the fgets function retains the newline character.

Return Value If successful, the gets function returns its argument. A NULL pointer indicates an error or end-of-file condition. Use ferror or feof to determine which one has occurred.

Syntax

int scanf( const char *format [,argument]... );

Parameter Description

format Format control

argument Optional argument

The scanf function reads data from the standard input stream stdin into the locations given by argument. Each argument must be a pointer to a variable with a type that corresponds to a type specifier in format. The format controls the interpretation of the input fields. The format can contain one or more of the following:

White-space characters: blank (' '); tab ('\t'); or newline ('\n').

· A white-space character causes scanf to read, but not store, all consecutive white-space characters in the input up to the next non-white-space character. One white-space character in the format matches any number (including 0) and combination of white-space characters in the input.

· Non-white-space characters, except for the percent sign (%). A non-white-space character causes scanf to read, but not store, a matching non-white-space character. If the next character in stdin does not match, scanf terminates.

· Format specifications, introduced by the percent sign (%). A format specification causes scanf to read and convert characters in the input into values of a specified type. The value is assigned to an argument in the argument list. The format is read from left to right. Characters outside format specifications are expected to match the sequence of characters in stdin; the matching characters in stdin are scanned but not stored. If a character in stdin conflicts with the format specification, scanf terminates. The character is left in stdin as if it had not been read.When the first format specification is encountered, the value of the first input field is converted according to this specification and stored in the location that is specified by the first argument. The second format specification causes the second input field to be converted and stored in the second argument, and so on through the end of the format string.

An input field is defined as all characters up to the first white-space character (space, tab, or newline), or up to the first character that cannot be converted according to the format specification, or until the field width (if specified) is reached. If there are too many arguments for the given specifications, the extra arguments are evaluated but ignored. The results are unpredictable if there are not enough arguments for the format specification.

Return Value The scanf function returns the number of fields that were successfully converted and assigned. The return value may be less than the number requested in the call to scanf. The return value does not include fields that were read but not assigned.

The return value is EOF if the end-of-file or end-of-string is encountered in the first attempt to read a character.

Syntax

int sscanf( const char *buffer, const char *format [, argument ] ... );

Parameter Description

buffer Stored data

format Format-control string

argument Optional arguments

The sscanf function reads data from buffer into the locations given by each argument. Every argument must be a pointer to a variable with a type that corresponds to a type specifier in format. The format controls the interpretation of the input fields and has the same form and function as the format argument for the scanf function;

Return Value The sscanf function returns the number of fields that were successfully converted and assigned. The return value does not include fields that were read but not assigned.

The return value is EOF for an attempt to read at end-of-string. A return value of 0 means that no fields were assigned.

scanf Format Specifiers

A format specification has the following form:

%[*] [width] [{h | l}]type

Each field of the format specification is a single character or a number signifying a particular format option. The type character, which appears after the last optional format field, determines whether the input field is interpreted as a character, a string, or a number. The simplest format specification contains only the percent sign and a type character (for example, %s).

Each field of the format specification is discussed in detail below. If a percent sign (%) is followed by a character that has no meaning as a format-control character, that character and the following characters (up to the next percent sign) are treated as an ordinary sequence of characters, that is, a sequence of characters that must match the input. For example, to specify that a percent-sign character is to be input, use %%. An asterisk (*) following the percent sign suppresses assignment of the next input field, which is interpreted as a field of the specified type. The field is scanned but not

stored.

The width is a positive decimal integer controlling the maximum number of characters to be read from stdin. No more than width characters are converted and stored at the corresponding argument. Fewer than width characters may be read if a white-space character (space, tab, or newline) or a character that cannot be converted according to the given format occurs before width is reached.

scanf Prefixes

%[*] [width] [ {h | l}]type

The optional prefix l indicates that the long version of the following type is to be used, while the prefix h indicates that the short version is to be used. The corresponding argument should point to a long or double object (with the l character) or a short object (with the h character). The l and h modifiers can be used with the d, i, n, o, x, and u type characters. The l modifier can also be used with the e, f, and g type characters. The l and h modifiers are ignored if specified for any other type.

l refer to the "size" of the object being read in (16-bit short or 32-bit long). The list below clarifies this use of l:

Program Code Action

scanf( "%s", &x ); Read a string into memory

scanf( "%d", &x ); Read an int into memory

scanf( "%ld", &x ); Read a long int into memory

scanf( "%lp", &x ); Read a 32-bit pointer into far memory

Note that %[a-z] and %[z-a] are interpreted as equivalent to %[abcde...z]. This is a common scanf extension, but note that it is not required by the ANSI standard.To store a string without storing a terminating null character ('\0'), use the specification %nc, where n is a decimal integer. In this case, the c type character indicates that the argument is a pointer to a character array. The next n characters

are read from the input stream into the specified location, and no null character ('\0') is appended. If n is not specified, the default value for it is 1.

scanf Type Characters

%[*] [width] [ {F | N} ] [ {h | l}]type

Char-acter

Type of Input Expected

Type of Argument

d

Decimal integer

Pointer to int

o

Octal integer

Pointer to int

x

Hexadecimal integer

Pointer to int

i

Decimal, hexadecimal, or octal integer

Pointer to int

u

Unsigned decimal integer

Pointer to unsigned int

e, Ef g, G

Floating-point value consisting of an optional sign (+ or -), a series of one or more decimal digits containing a decimal point, and an optional exponent ("e" or "E") followed by an optionally signed integer value.

Pointer to float

c

Character. White-space characters that are ordinarily skipped are read when c is specified; to read the next non-white-space character, use %1s.

Pointer to char

s

String

Pointer to character array large enough for input field plus a terminating null character ('\0'), which is automatically appended.

n

No input read from stream or buffer.

Pointer to int, into which is stored the number of characters successfully read from the stream or buffer up to that point in the current call to scanf.

p

Value in the form xxxx:yyyy, where the digits x and y are uppercase hexadecimal digits.

Pointer to pointer to void

To read strings not delimited by space characters, a set of characters in brackets ([ ]) can be substituted for the s (string) type character. The corresponding input field is read up to the first character that does not appear in the bracketed character set. If the first character in the set is a caret (^), the effect is reversed: the input field is read up to the first character that does appear in the rest of the character set.

Syntax

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

Parameter Description

format Format control

argument Optional arguments

The printf function formats and prints a series of characters and values to the standard output stream, stdout. The format argument consists of ordinary characters, escape sequences, and (if arguments follow format) format specifications. The ordinary characters and escape sequences are copied to stdout in order of their appearance. For example, the line

printf("Line one\n\t\tLine two\n");

produces the output

Line one

Line two

If arguments follow the format string, the format string must contain specifications that determine the output format for the arguments.

Format specifications always begin with a percent sign (%) and are read left to right. When the first format specification (if any) is encountered, the value of the first argument after format is converted and output accordingly. The second format specification causes the second argument to be converted and output, and so on. If there are more arguments than there are format specifications, the extra arguments are ignored. The results are undefined if there are not enough arguments for all the

format specifications.

Return Value The printf function returns the number of characters printed, or a negative value in the case of an error.

Syntax

int putc( int c, FILE *stream );

int putchar( int c );

Parameter Description

c Character to be written

stream Pointer to FILE structure

The putc routine writes the single character c to the output stream at the current position. The putchar routine is identical to putc(c, stdout). These routines are implemented as both macros and functions.

Return Value The putc and putchar routines return the character written, or EOF in the case of an error. Any integer can be passed to putc, but only the lower 8 bits are written.

Syntax

int puts( const char *string );

Parameter Description

string String to be output

The puts function writes string to the standard output stream stdout, replacing the string's terminating null character ('\0') with a newline character (\n) in the output stream.

Return Value The puts function returns a nonnegative value if it is successful. If the function fails, it returns EOF.

Syntax

int sprintf( char *buffer, const char *format [, argument] ... );

Parameter Description

buffer Storage location for output

format Format-control string

argument Optional arguments

count Maximum number of bytes to store

The sprintf function formats and stores a series of characters and values in buffer. Each argument (if any) is converted and output according to the corresponding format specification in the format. The format consists of ordinary characters and has the same form and function as the format argument for the printf function. (See printf for a description of the format and arguments.) A null character is appended to the end of the characters written but is not counted in the return value.

Return Value The sprintf function return the number of characters stored in buffer, not counting the terminating null character.