C Programming/Stream IO

Introduction
The  header declares a broad assortment of functions that perform input and output to files and devices such as the console. It was one of the earliest headers to appear in the C library. It declares more functions than any other standard header and also requires more explanation because of the complex machinery that underlies the functions.

The device-independent model of input and output has seen dramatic improvement over the years and has received little recognition for its success. FORTRAN II was touted as a machine-independent language in the 1960s, yet it was essentially impossible to move a FORTRAN program between architectures without some change. In FORTRAN II, you named the device you were talking to right in the FORTRAN statement in the middle of your FORTRAN code. So, you said  on a tape-oriented IBM 7090 but   to read a card image on other machines. FORTRAN IV had more generic  and   statements, specifying a logical unit number (LUN) instead of the device name. The era of device-independent I/O had dawned.

Peripheral devices such as printers still had fairly strong notions about what they were asked to do. And then, peripheral interchange utilities were invented to handle bizarre devices. When cathode-ray tubes came onto the scene, each manufacturer of consoles solved problems such as console cursor movement in an independent manner, causing further headaches.

It was into this atmosphere that Unix was born. Ken Thompson and Dennis Ritchie, the developers of Unix, deserve credit for packing any number of bright ideas into the operating system. Their approach to device independence was one of the brightest.

The ANSI C  library is based on the original Unix file I/O primitives but casts a wider net to accommodate the least-common denominator across varied systems.

Streams
Input and output, whether to or from physical devices such as terminals and tape drives, or whether to or from files supported on structured storage devices, are mapped into logical data streams, whose properties are more uniform than their various inputs and outputs. Two forms of mapping are supported: text streams and binary streams.

A text stream consists of one or more lines. A line in a text stream consists of zero or more characters plus a terminating new-line character. (The only exception is that in some implementations the last line of a file does not require a terminating new-line character.) Unix adopted a standard internal format for all text streams. Each line of text is terminated by a new-line character. That's what any program expects when it reads text, and that's what any program produces when it writes text. (This is the most basic convention, and if it doesn't meet the needs of a text-oriented peripheral attached to a Unix machine, then the fix-up occurs out at the edges of the system. Nothing in between needs to change.) The string of characters that go into, or come out of a text stream may have to be modified to conform to specific conventions. This results in a possible difference between the data that go into a text stream and the data that come out. For instance, in some implementations when a space-character precedes a new-line character in the input, the space character gets removed out of the output. In general, when the data only consists of printable characters and control characters like horizontal tab and new-line, the input and output of a text stream are equal.

Compared to a text stream, a binary stream is pretty straight forward. A binary stream is an ordered sequence of characters that can transparently record internal data. Data written to a binary stream shall always equal the data that gets read out under the same implementation. Binary streams, however, may have an implementation-defined number of null characters appended to the end of the stream. There are no further conventions which need to be considered.

Nothing in Unix prevents the program from writing arbitrary 8-bit binary codes to any open file, or reading them back unchanged from an adequate repository. Thus, Unix obliterated the long-standing distinction between text streams and binary streams.

Standard Streams
When a C program starts its execution the program automatically opens three standard streams named ,, and. These are attached for every C program.

The first standard stream is used for input buffering and the other two are used for output. These streams are sequences of bytes.

Consider the following program:

By default  points to the keyboard and   and   point to the screen. It is possible under Unix and may be possible under other operating systems to redirect input from or output to a file or both.

Pointers to streams
The  header contains a definition for a type   (usually via a  ) which is capable of processing all the information needed to exercise control over a stream, including its file position indicator, a pointer to the associated buffer (if any), an error indicator that records whether a read/write error has occurred, and an end-of-file indicator that records whether the end of the file has been reached.

It is considered bad form to access the contents of  directly unless the programmer is writing an implementation of   and its contents. Better access to the contents of  is provided via the functions in. It can be said that the  type is an early example of object-oriented programming.

Opening and Closing Files
To open and close files, the  library has three functions: ,  , and.

Opening Files
and  opens the file whose name is in the string pointed to by   and associates a stream with it. Both return a pointer to the object controlling the stream, or, if the open operation fails, a null pointer. The error and end-of-file indicators are cleared, and if the open operation fails error is set. differs from  in that the file pointed to by   is closed first when already open and any close errors are ignored.

for both functions points to a string beginning with one of the following sequences (additional characters may follow the sequences):

r          open a text file for reading w          truncate to zero length or create a text file for writing a          append; open or create text file for writing at end-of-file rb         open binary file for reading wb         truncate to zero length or create a binary file for writing ab         append; open or create binary file for writing at end-of-file r+         open text file for update (reading and writing) w+         truncate to zero length or create a text file for update a+         append; open or create text file for update r+b or rb+ open binary file for update (reading and writing) w+b or wb+ truncate to zero length or create a binary file for update a+b or ab+ append; open or create binary file for update

Opening a file with read mode (' ' as the first character in the  argument) fails if the file does not exist or cannot be read.

Opening a file with append mode (' ' as the first character in the  argument) causes all subsequent writes to the file to be forced to the then-current end-of-file, regardless of intervening calls to the   function. In some implementations, opening a binary file with append mode (' ' as the second or third character in the above list of  arguments) may initially position the file position indicator for the stream beyond the last data written, because of null character padding.

When a file is opened with update mode (' ' as the second or third character in the above list of  argument values), both input and output may be performed on the associated stream. However, output may not be directly followed by input without an intervening call to the  function or to a file positioning function (,  , or  ), and input may not be directly followed by output without an intervening call to a file positioning function, unless the input operation encounters end-of-file. Opening (or creating) a text file with update mode may instead open (or create) a binary stream in some implementations.

When opened, a stream is fully buffered if and only if it can be determined not to refer to an interactive device.

Closing Files
The  function causes the stream pointed to by   to be flushed and the associated file to be closed. Any unwritten buffered data for the stream are delivered to the host environment to be written to the file; any unread buffered data are discarded. The stream is disassociated from the file. If the associated buffer was automatically allocated, it is deallocated. The function returns zero if the stream was successfully closed or  if any errors were detected.

The function
If  points to an output stream or an update stream in which the most recent operation was not input, the   function causes any unwritten data for that stream to be deferred to the host environment to be written to the file. The behavior of fflush is undefined for input stream.

If  is a null pointer, the   function performs this flushing action on all streams for which the behavior is defined above.

The  functions returns   if a write error occurs, otherwise zero.

The reason for having a  function is because streams in C can have buffered input/output; that is, functions that write to a file actually write to a buffer inside the   structure. If the buffer is filled to capacity, the write functions will call  to actually "write" the data that is in the buffer to the file. Because  is only called every once in a while, calls to the operating system to do a raw write are minimized.

The function
Except that it returns no value, the  function is equivalent to the   function invoked with the values   for   and   for , or (if   is a null pointer) with the value   for.

The function
The  function may be used only after the stream pointed to by   has been associated with an open file and before any other operation is performed on the stream. The argument  determines how the stream will be buffered, as follows:   causes input/output to be fully buffered;   causes input/output to be line buffered;   causes input/output to be unbuffered. If  is not a null pointer, the array it points to may be used instead of a buffer associated by the   function. (The buffer must have a lifetime at least as great as the open stream, so the stream should be closed before a buffer that has automatic storage duration is deallocated upon block exit.) The argument  specifies the size of the array. The contents of the array at any time are indeterminate.

The  function returns zero on success, or nonzero if an invalid value is given for   or if the request cannot be honored.

Functions that Modify the File Position Indicator
The  library has five functions that affect the file position indicator besides those that do reading or writing: ,  ,  ,  , and.

The  and   functions are older than   and.

The and   functions
The  function stores the current value of the file position indicator for the stream pointed to by   in the object pointed to by. The value stored contains unspecified information usable by the  function for repositioning the stream to its position at the time of the call to the   function.

If successful, the  function returns zero; on failure, the   function returns nonzero and stores an implementation-defined positive value in.

The  function sets the file position indicator for the stream pointed to by   according to the value of the object pointed to by , which shall be a value obtained from an earlier call to the   function on the same stream.

A successful call to the  function clears the end-of-file indicator for the stream and undoes any effects of the   function on the same stream. After an  call, the next operation on an update stream may be either input or output.

If successful, the  function returns zero; on failure, the   function returns nonzero and stores an implementation-defined positive value in.

The and   functions
The  function sets the file position indicator for the stream pointed to by.

For a binary stream, the new position, measured in characters from the beginning of the file, is obtained by adding  to the position specified by. Three macros in  called ,  , and   expand to unique values. If the position specified by  is , the specified position is the beginning of the file; if   is  , the specified position is the end of the file; and if   is  , the specified position is the current file position. A binary stream need not meaningfully support  calls with a   value of.

For a text stream, either  shall be zero, or   shall be a value returned by an earlier call to the   function on the same stream and   shall be.

The  function returns nonzero only for a request that cannot be satisfied.

The  function obtains the current value of the file position indicator for the stream pointed to by. For a binary stream, the value is the number of characters from the beginning of the file; for a text stream, its file position indicator contains unspecified information, usable by the  function for returning the file position indicator for the stream to its position at the time of the   call; the difference between two such return values is not necessarily a meaningful measure of the number of characters written or read.

If successful, the  function returns the current value of the file position indicator for the stream. On failure, the  function returns   and stores an implementation-defined positive value in.

The function
The  function sets the file position indicator for the stream pointed to by   to the beginning of the file. It is equivalent to

(void)fseek(stream, 0L, SEEK_SET)

except that the error indicator for the stream is also cleared.

The function
The  function clears the end-of-file and error indicators for the stream pointed to by.

The function
The  function tests the end-of-file indicator for the stream pointed to by   and returns nonzero if and only if the end-of-file indicator is set for , otherwise it returns zero.

The function
The  function tests the error indicator for the stream pointed to by   and returns nonzero if and only if the error indicator is set for , otherwise it returns zero.

The function
The  function maps the error number in the integer expression   to an error message. It writes a sequence of characters to the standard error stream thus: first, if  is not a null pointer and the character pointed to by   is not the null character, the string pointed to by   followed by a colon (:) and a space; then an appropriate error message string followed by a new-line character. The contents of the error message are the same as those returned by the  function with the argument , which are implementation-defined.

Other Operations on Files
The  library has a variety of functions that do some operation on files besides reading and writing.

The function
The  function causes the file whose name is the string pointed to by   to be no longer accessible by that name. A subsequent attempt to open that file using that name will fail, unless it is created anew. If the file is open, the behavior of the  function is implementation-defined.

The  function returns zero if the operation succeeds, nonzero if it fails.

The function
The  function causes the file whose name is the string pointed to by   to be henceforth known by the name given by the string pointed to by. The file named  is no longer accessible by that name. If a file named by the string pointed to by  exists prior to the call to the   function, the behavior is implementation-defined.

The  function returns zero if the operation succeeds, nonzero if it fails, in which case if the file existed previously it is still known by its original name.

The function
The  function creates a temporary binary file that will automatically be removed when it is closed or at program termination. If the program terminates abnormally, whether an open temporary file is removed is implementation-defined. The file is opened for update with  mode.

The  function returns a pointer to the stream of the file that it created. If the file cannot be created, the  function returns a null pointer.

The function
The  function generates a string that is a valid file name and that is not the name of an existing file.

The  function generates a different string each time it is called, up to   times. ( is a macro defined in  .) If it is called more than   times, the behavior is implementation-defined.

The implementation shall behave as if no library function calls the  function.

If the argument is a null pointer, the  function leaves its result in an internal static object and returns a pointer to that object. Subsequent calls to the  function may modify the same object. If the argument is not a null pointer, it is assumed to point to an array of at least  characters (  is another macro in  ); the   function writes its result in that array and returns the argument as its value.

The value of the macro  must be at least 25.

The function
The  function obtains the next character (if present) as an   converted to an , from the stream pointed to by  , and advances the associated file position indicator for the stream (if defined).

The  function returns the next character from the stream pointed to by. If the stream is at end-of-file or a read error occurs,  returns   (  is a negative value defined in , usually  ). The routines  and   must be used to distinguish between end-of-file and error. If an error occurs, the global variable  is set to indicate the error.

The function
The  function reads at most one less than the number of characters specified by   from the stream pointed to by   into the array pointed to by. No additional characters are read after a new-line character (which is retained) or after end-of-file. A null character is written immediately after the last character read into the array.

The  function returns   if successful. If end-of-file is encountered and no characters have been read into the array, the contents of the array remain unchanged and a null pointer is returned. If a read error occurs during the operation, the array contents are indeterminate and a null pointer is returned.

Warning: Different operating systems may use different character sequences to represent the end-of-line sequence. For example, some filesystems use the terminator  in text files;   may read those lines, removing the   but keeping the   as the last character of. This expurious character should be removed in the string  before the string is used for anything (unless the programmer doesn't care about it). Unixes typically use  as its end-of-line sequence, MS-DOS and Windows uses , and Mac OSes used   before OS X. Many compilers on operating systems other than Unix or Linux map newline sequences to   on input for text files; check your compiler's documentation to discover what it does in this situation.

The function
The  function is equivalent to , except that it may be implemented as a macro. If it is implemented as a macro, the  argument may be evaluated more than once, so the argument should never be an expression with side effects (i.e. have an assignment, increment, or decrement operators, or be a function call).

The  function returns the next character from the input stream pointed to by. If the stream is at end-of-file, the end-of-file indicator for the stream is set and  returns   (  is a negative value defined in , usually  ). If a read error occurs, the error indicator for the stream is set and  returns.

The function
The  function is equivalent to   with the argument.

The  function returns the next character from the input stream pointed to by. If  is at end-of-file, the end-of-file indicator for   is set and   returns   (  is a negative value defined in , usually  ). If a read error occurs, the error indicator for  is set and   returns.

The function
The  function reads characters from the input stream pointed to by   into the array pointed to by   until an end-of-file is encountered or a new-line character is read. Any new-line character is discarded, and a null character is written immediately after the last character read into the array.

The  function returns   if successful. If the end-of-file is encountered and no characters have been read into the array, the contents of the array remain unchanged and a null pointer is returned. If a read error occurs during the operation, the array contents are indeterminate and a null pointer is returned.

This function and description is only included here for completeness. Most C programmers nowadays shy away from using, as there is no way for the function to know how big the buffer is that the programmer wants to read into.

Commandment #5 of Henry Spencer's The Ten Commandments for C Programmers (Annotated Edition) reads

"Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest foo someone someday shall type supercalifragilisticexpialidocious."

It mentions  in the annotation:

"As demonstrated by the deeds of the Great Worm, a consequence of this commandment is that robust production software should never make use of, for it is truly a tool of the Devil. Thy interfaces should always inform thy servants of the bounds of thy arrays, and servants who spurn such advice or quietly fail to follow it should be dispatched forthwith to the Land Of Rm, where they can do no further harm to thee."

Before the 2018 version of the C standard, the  function was deprecated. It is hoped that programmers would use the  function instead.

The function
The  function pushes the character specified by   (converted to an  ) back onto the input stream pointed to by stream. The pushed-back characters will be returned by subsequent reads on that stream in the reverse order of their pushing. A successful intervening call (with the stream pointed to by ) to a file-positioning function (,  , or  ) discards any pushed-back characters for the stream. The external storage corresponding to the stream is unchanged.

One character of pushback is guaranteed. If the  function is called too many times on the same stream without an intervening read or file positioning operation on that stream, the operation may fail.

If the value of  equals that of the macro , the operation fails and the input stream is unchanged.

A successful call to the  function clears the end-of-file indicator for the stream. The value of the file position indicator for the stream after reading or discarding all pushed-back characters shall be the same as it was before the characters were pushed back. For a text stream, the value of its file-position indicator after a successful call to the  function is unspecified until all pushed-back characters are read or discarded. For a binary stream, its file position indicator is decremented by each successful call to the  function; if its value was zero before a call, it is indeterminate after the call.

The  function returns the character pushed back after conversion, or   if the operation fails.

EOF pitfall
A mistake when using,  , or   is to assign the result to a variable of type   before comparing it to. The following code fragments exhibit this mistake, and then show the correct approach (using type int):

Consider a system in which the type  is 8 bits wide, representing 256 different values. may return any of the 256 possible characters, and it also may return  to indicate end-of-file, for a total of 257 different possible return values.

When 's result is assigned to a , which can represent only 256 different values, there is necessarily some loss of information—when packing 257 items into 256 slots, there must be a collision. The  value, when converted to , becomes indistinguishable from whichever one of the 256 characters shares its numerical value. If that character is found in the file, the above example may mistake it for an end-of-file indicator; or, just as bad, if type  is unsigned, then because   is negative, it can never be equal to any unsigned , so the above example will not terminate at end-of-file. It will loop forever, repeatedly printing the character which results from converting  to.

However, this looping failure mode does not occur if the char definition is signed (C makes the signedness of the default char type implementation-dependent), assuming the commonly used   value of -1. However, the fundamental issue remains that if the  value is defined outside of the range of the   type, when assigned to a   that value is sliced and will no longer match the full   value necessary to exit the loop. On the other hand, if  is within range of , this guarantees a collision between   and a char value. Thus, regardless of how system types are defined, never use  types when testing against.

On systems where  and   are the same size (i.e., systems incompatible with minimally the POSIX and C99 standards), even the "good" example will suffer from the indistinguishability of   and some character's value. The proper way to handle this situation is to check  and   after   returns. If  indicates that end-of-file has not been reached, and   indicates that no errors have occurred, then the   returned by   can be assumed to represent an actual character. These extra checks are rarely done, because most programmers assume that their code will never need to run on one of these "big " systems. Another way is to use a compile-time assertion to make sure that, which at least prevents a program with such an assumption from compiling in such a system.

Direct input function: the function
The  function reads, into the array pointed to by , up to   elements whose size is specified by  , from the stream pointed to by. The file position indicator for the stream (if defined) is advanced by the number of characters successfully read. If an error occurs, the resulting value of the file position indicator for the stream is indeterminate. If a partial element is read, its value is indeterminate.

The  function returns the number of elements successfully read, which may be less than   if a read error or end-of-file is encountered. If  or   is zero,   returns zero and the contents of the array and the state of the stream remain unchanged.

Formatted input functions: the family of functions
The  function reads input from the stream pointed to by , under control of the string pointed to by   that specifies the admissible sequences and how they are to be converted for assignment, using subsequent arguments as pointers to the objects to receive converted input. If there are insufficient arguments for the format, the behavior is undefined. If the format is exhausted while arguments remain, the excess arguments are evaluated (as always) but are otherwise ignored.

The format shall be a multibyte character sequence, beginning and ending in its initial shift state. The format is composed of zero or more directives: one or more white-space characters; an ordinary multibyte character (neither % or a white-space character); or a conversion specification. Each conversion specification is introduced by the character %. After the %, the following appear in sequence:
 * An optional assignment-suppressing character *.
 * An optional nonzero decimal integer that specifies the maximum field width.
 * An optional h, l (ell) or L indicating the size of the receiving object. The conversion specifiers <tt>d</tt>, <tt>i</tt>, and <tt>n</tt> shall be preceded by <tt>h</tt> if the corresponding argument is a pointer to  rather than a pointer to , or by <tt>l</tt> if it is a pointer to  . Similarly, the conversion specifiers <tt>o</tt>, <tt>u</tt>, and <tt>x</tt> shall be preceded by <tt>h</tt> if the corresponding argument is a pointer to   rather than  , or by <tt>l</tt> if it is a pointer to  . Finally, the conversion specifiers <tt>e</tt>, <tt>f</tt>, and <tt>g</tt> shall be preceded by <tt>l</tt> if the corresponding argument is a pointer to   rather than a pointer to  , or by <tt>L</tt> if it is a pointer to  . If an <tt>h</tt>, <tt>l</tt>, or <tt>L</tt> appears with any other format specifier, the behavior is undefined.
 * A character that specifies the type of conversion to be applied. The valid conversion specifiers are described below.

The  function executes each directive of the format in turn. If a directive fails, as detailed below, the  function returns. Failures are described as input failures (due to the unavailability of input characters) or matching failures (due to inappropriate input).

A directive composed of white-space character(s) is executed by reading input up to the first non-white-space character (which remains unread) or until no more characters remain unread.

A directive that is an ordinary multibyte character is executed by reading the next characters of the stream. If one of the characters differs from one comprising the directive, the directive fails, and the differing and subsequent characters remain unread.

A directive that is a conversion specification defines a set of matching input sequences, as described below for each specifier. A conversion specification is executed in the following steps:

Input white-space characters (as specified by the  function) are skipped, unless the specification includes a <tt>[</tt>, <tt>c</tt>, or <tt>n</tt> specifier. (The white-space characters are not counted against the specified field width.)

An input item is read from the stream, unless the specification includes an <tt>n</tt> specifier. An input item is defined as the longest matching sequences of input characters, unless that exceeds a specified field width, in which case it is the initial subsequence of that length in the sequence. The first character, if any, after the input item remains unread. If the length of the input item is zero, the execution of the directive fails; this condition is a matching failure, unless an error prevented input from the stream, in which case it is an input failure.

Except in the case of a <tt>%</tt> specifier, the input item (or, in the case of a <tt>%n</tt> directive, the count of input characters) is converted to a type appropriate to the conversion specifier. If the input item is not a matching sequence, the execution of the directive fails; this condition is a matching failure. Unless assignment suppression was indicated by a <tt>*</tt>, the result of the conversion is placed in the object pointed to by the first argument following the  argument that has not already received a conversion result. If this object does not have an appropriate type, or if the result of the conversion cannot be represented in the space provided, the behavior is undefined.

The following conversion specifiers are valid:


 * <tt>d</tt> : Matches an optionally signed decimal integer, whose format is the same as expected for the subject sequence of the  function with the value 10 for the   argument. The corresponding argument shall be a pointer to integer.


 * <tt>i</tt> : Matches an optionally signed integer, whose format is the same as expected for the subject sequence of the  function with the value 0 for the   argument. The corresponding argument shall be a pointer to integer.


 * <tt>o</tt> : Matches an optionally signed octal integer, whose format is the same as expected for the subject sequence of the  function with the value 8 for the   argument. The corresponding argument shall be a pointer to unsigned integer.


 * <tt>u</tt> : Matches an optionally signed decimal integer, whose format is the same as expected for the subject sequence of the  function with the value 10 for the   argument. The corresponding argument shall be a pointer to unsigned integer.


 * <tt>x</tt> : Matches an optionally signed hexadecimal integer, whose format is the same as expected for the subject sequence of the  function with the value 16 for the   argument. The corresponding argument shall be a pointer to unsigned integer.


 * <tt>e</tt>, <tt>f</tt>, <tt>g</tt> : Matches an optionally signed floating-point number, whose format is the same as expected for the subject string of the  function. The corresponding argument will be a pointer to floating.


 * <tt>s</tt> : Matches a sequence of non-white-space characters. (No special provisions are made for multibyte characters.) The corresponding argument shall be a pointer to the initial character of an array large enough to accept the sequence and a terminating null character, which will be added automatically.


 * <tt>[</tt> : Matches a nonempty sequence of characters (no special provisions are made for multibyte characters) from a set of expected characters (the scanset). The corresponding argument shall be a pointer to the initial character of an array large enough to accept the sequence and a terminating null character, which will be added automatically. The conversion specifier includes all subsequent characters in the  string, up to and including the matching right bracket (<tt>]</tt>). The characters between the brackets (the scanlist) comprise the scanset, unless the character after the left bracket is a circumflex (<tt>^</tt>), in which case the scanset contains all the characters that do not appear in the scanlist between the circumflex and the right bracket. If the conversion specifier begins with <tt>[]</tt> or <tt>[^]</tt>, the right-bracket character is in the scanlist and the next right bracket character is the matching right bracket that ends the specification; otherwise, the first right bracket character is the one that ends the specification. If a <tt>-</tt> character is in the scanlist and is not the first, nor the second where the first character is a <tt>^</tt>, nor the last character, the behavior is implementation-defined.


 * <tt>c</tt> : Matches a sequence of characters (no special provisions are made for multibyte characters) of the number specified by the field width (1 if no field width is present in the directive). The corresponding argument shall be a pointer to the initial character of an array large enough to accept the sequence. No null character is added.


 * <tt>p</tt> : Matches an implementation-defined set of sequences, which should be the same as the set of sequences that may be produced by the <tt>%p</tt> conversion of the  function. The corresponding argument shall be a pointer to  . The interpretation of the input then is implementation-defined. If the input item is a value converted earlier during the same program execution, the pointer that results shall compare equal to that value; otherwise the behavior of the <tt>%p</tt> conversion is undefined.


 * <tt>n</tt> : No input is consumed. The corresponding argument shall be a pointer to integer into which is to be written the number of characters read from the input stream so far by this call to the  function. Execution of a <tt>%n</tt> directive does not increment the assignment count returned at the completion of execution of the   function.


 * <tt>%</tt> : Matches a single <tt>%</tt>; no conversion or assignment occurs. The complete conversion specification shall be <tt>%%</tt>.

If a conversion specification is invalid, the behavior is undefined.

The conversion specifiers <tt>E</tt>, <tt>G</tt>, and <tt>X</tt> are also valid and behave the same as, respectively, <tt>e</tt>, <tt>g</tt>, and <tt>x</tt>.

If end-of-file is encountered during input, conversion is terminated. If end-of-file occurs before any characters matching the current directive have been read (other than leading white space, where permitted), execution of the current directive terminates with an input failure; otherwise, unless execution of the current directive is terminated with a matching failure, execution of the following directive (if any) is terminated with an input failure.

If conversion terminates on a conflicting input character, the offending input character is left unread in the input stream. Trailing white space (including new-line characters) is left unread unless matched by a directive. The success of literal matches and suppressed assignments is not directly determinable other than via the <tt>%n</tt> directive.

The  function returns the value of the macro   if an input failure occurs before any conversion. Otherwise, the  function returns the number of input items assigned, which can be fewer than provided for, or even zero, in the event of an early matching failure.

The  function is equivalent to   with the argument   interposed before the arguments to. Its return value is similar to that of.

The  function is equivalent to , except that the argument   specifies a string from which the input is to be obtained, rather than from a stream. Reaching the end of the string is equivalent to encountering the end-of-file for the  function. If copying takes place between objects that overlap, the behavior is undefined.

The function
int fputc(int c, FILE *stream);
 * 1) include <stdio.h>

The  function writes the character specified by   (converted to an  ) to the stream pointed to by   at the position indicated by the associated file position indicator (if defined), and advances the indicator appropriately. If the file cannot support positioning requests, or if the stream is opened with append mode, the character is appended to the output stream. The function returns the character written, unless a write error occurs, in which case the error indicator for the stream is set and  returns.

The function
int fputs(const char *s, FILE *stream);
 * 1) include <stdio.h>

The  function writes the string pointed to by   to the stream pointed to by. The terminating null character is not written. The function returns  if a write error occurs, otherwise it returns a nonnegative value.

The function
int putc(int c, FILE *stream);
 * 1) include <stdio.h>

The  function is equivalent to , except that if it is implemented as a macro, it may evaluate   more than once, so the argument should never be an expression with side effects. The function returns the character written, unless a write error occurs, in which case the error indicator for the stream is set and the function returns.

The function
int putchar(int c);
 * 1) include <stdio.h>

The  function is equivalent to   with the second argument. It returns the character written, unless a write error occurs, in which case the error indicator for  is set and the function returns.

The function
int puts(const char *s);
 * 1) include <stdio.h>

The  function writes the string pointed to by   to the stream pointed to by , and appends a new-line character to the output. The terminating null character is not written. The function returns  if a write error occurs; otherwise, it returns a nonnegative value.

Direct output function: the function
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
 * 1) include <stdio.h>

The  function writes, from the array pointed to by , up to   elements whose size is specified by   to the stream pointed to by. The file position indicator for the stream (if defined) is advanced by the number of characters successfully written. If an error occurs, the resulting value of the file position indicator for the stream is indeterminate. The function returns the number of elements successfully written, which will be less than  only if a write error is encountered.

Formatted output functions: the family of functions
int fprintf(FILE *stream, const char *format, ...); int printf(const char *format, ...); int sprintf(char *s, const char *format, ...); int vfprintf(FILE *stream, const char *format, va_list arg); int vprintf(const char *format, va_list arg); int vsprintf(char *s, const char *format, va_list arg);
 * 1) include <stdarg.h>
 * 2) include <stdio.h>

''Note: Some length specifiers and format specifiers are new in C99. These may not be available in older compilers and versions of the stdio library, which adhere to the C89/C90 standard. Wherever possible, the new ones will be marked with (C99).''

The  function writes output to the stream pointed to by   under control of the string pointed to by   that specifies how subsequent arguments are converted for output. If there are insufficient arguments for the format, the behavior is undefined. If the format is exhausted while arguments remain, the excess arguments are evaluated (as always) but are otherwise ignored. The  function returns when the end of the format string is encountered.

The format shall be a multibyte character sequence, beginning and ending in its initial shift state. The format is composed of zero or more directives: ordinary multibyte characters (not <tt>%</tt>), which are copied unchanged to the output stream; and conversion specifications, each of which results in fetching zero or more subsequent arguments, converting them, if applicable, according to the corresponding conversion specifier, and then writing the result to the output stream.

Each conversion specification is introduced by the character <tt>%</tt>. After the <tt>%</tt>, the following appear in sequence:


 * Zero or more flags (in any order) that modify the meaning of the conversion specification.
 * An optional minimum field width. If the converted value has fewer characters than the field width, it is padded with spaces (by default) on the left (or right, if the left adjustment flag, described later, has been given) to the field width. The field width takes the form of an asterisk <tt>*</tt> (described later) or a decimal integer. (Note that 0 is taken as a flag, not as the beginning of a field width.)
 * An optional precision that gives the minimum number of digits to appear for the <tt>d</tt>, <tt>i</tt>, <tt>o</tt>, <tt>u</tt>, <tt>x</tt>, and <tt>X</tt> conversions, the number of digits to appear after the decimal-point character for <tt>a</tt>, <tt>A</tt>, <tt>e</tt>, <tt>E</tt>, <tt>f</tt>, and <tt>F</tt> conversions, the maximum number of significant digits for the <tt>g</tt> and <tt>G</tt> conversions, or the maximum number of characters to be written from a string in <tt>s</tt> conversions. The precision takes the form of a period (<tt>.</tt>) followed either by an asterisk <tt>*</tt> (described later) or by an optional decimal integer; if only the period is specified, the precision is taken as zero. If a precision appears with any other conversion specifier, the behavior is undefined. Floating-point numbers are rounded to fit the precision; i.e. <tt>printf("%1.1f\n", 1.19);</tt> produces <tt>1.2</tt>.
 * An optional length modifier that specifies the size of the argument.
 * A conversion specifier character that specifies the type of conversion to be applied.

As noted above, a field width, or precision, or both, may be indicated by an asterisk. In this case, an  argument supplies the field width or precision. The arguments specifying field width, or precision, or both, shall appear (in that order) before the argument (if any) to be converted. A negative field width argument is taken as a <tt>-</tt> flag followed by a positive field width. A negative precision argument is taken as if the precision were omitted.

The flag characters and their meanings are:
 * <tt>-</tt> : The result of the conversion is left-justified within the field. (It is right-justified if this flag is not specified.)
 * <tt>+</tt> : The result of a signed conversion always begins with a plus or minus sign. (It begins with a sign only when a negative value is converted if this flag is not specified. The results of all floating conversions of a negative zero, and of negative values that round to zero, include a minus sign.)
 * space : If the first character of a signed conversion is not a sign, or if a signed conversion results in no characters, a space is prefixed to the result. If the space and <tt>+</tt> flags both appear, the space flag is ignored.
 * <tt>#</tt> : The result is converted to an "alternative form". For <tt>o</tt> conversion, it increases the precision, if and only if necessary, to force the first digit of the result to be a zero (if the value and precision are both 0, a single 0 is printed). For <tt>x</tt> (or <tt>X</tt>) conversion, a nonzero result has <tt>0x</tt> (or <tt>0X</tt>) prefixed to it. For <tt>a</tt>, <tt>A</tt>, <tt>e</tt>, <tt>E</tt>, <tt>f</tt>, <tt>F</tt>, <tt>g</tt>, and <tt>G</tt> conversions, the result always contains a decimal-point character, even if no digits follow it. (Normally, a decimal-point character appears in the result of these conversions only if a digit follows it.) For <tt>g</tt> and <tt>G</tt> conversions, trailing zeros are not removed from the result. For other conversions, the behavior is undefined.
 * <tt>0</tt> : For <tt>d</tt>, <tt>i</tt>, <tt>o</tt>, <tt>u</tt>, <tt>x</tt>, <tt>X</tt>, <tt>a</tt>, <tt>A</tt>, <tt>e</tt>, <tt>E</tt>, <tt>f</tt>, <tt>F</tt>, <tt>g</tt>, and <tt>G</tt> conversions, leading zeros (following any indication of sign or base) are used to pad to the field width; no space padding is performed. If the <tt>0</tt> and <tt>-</tt> flags both appear, the <tt>0</tt> flag is ignored. For <tt>d</tt>, <tt>i</tt>, <tt>o</tt>, <tt>u</tt>, <tt>x</tt>, and <tt>X</tt> conversions, if a precision is specified, the <tt>0</tt> flag is ignored. For other conversions, the behavior is undefined.

The length modifiers and their meanings are:
 * <tt>hh</tt> : (C99) Specifies that a following <tt>d</tt>, <tt>i</tt>, <tt>o</tt>, <tt>u</tt>, <tt>x</tt>, or <tt>X</tt> conversion specifier applies to a  or   argument (the argument will have been promoted according to the integer promotions, but its value shall be converted to   or   before printing); or that a following <tt>n</tt> conversion specifier applies to a pointer to a   argument.


 * <tt>h</tt> : Specifies that a following <tt>d</tt>, <tt>i</tt>, <tt>o</tt>, <tt>u</tt>, <tt>x</tt>, or <tt>X</tt> conversion specifier applies to a  or   argument (the argument will have been promoted according to the integer promotions, but its value shall be converted to   or   before printing); or that a following <tt>n</tt> conversion specifier applies to a pointer to a   argument.


 * <tt>l</tt> (ell) : Specifies that a following <tt>d</tt>, <tt>i</tt>, <tt>o</tt>, <tt>u</tt>, <tt>x</tt>, or <tt>X</tt> conversion specifier applies to a  or   argument; that a following <tt>n</tt> conversion specifier applies to a pointer to a   argument; (C99) that a following <tt>c</tt> conversion specifier applies to a   argument; (C99) that a following <tt>s</tt> conversion specifier applies to a pointer to a   argument; or has no effect on a following <tt>a</tt>, <tt>A</tt>, <tt>e</tt>, <tt>E</tt>, <tt>f</tt>, <tt>F</tt>, <tt>g</tt>, or <tt>G</tt> conversion specifier.


 * <tt>ll</tt> (ell-ell) : (C99) Specifies that a following <tt>d</tt>, <tt>i</tt>, <tt>o</tt>, <tt>u</tt>, <tt>x</tt>, or <tt>X</tt> conversion specifier applies to a  or   argument; or that a following <tt>n</tt> conversion specifier applies to a pointer to a   argument.


 * <tt>j</tt> : (C99) Specifies that a following <tt>d</tt>, <tt>i</tt>, <tt>o</tt>, <tt>u</tt>, <tt>x</tt>, or <tt>X</tt> conversion specifier applies to an  or   argument; or that a following <tt>n</tt> conversion specifier applies to a pointer to an   argument.


 * <tt>z</tt> : (C99) Specifies that a following <tt>d</tt>, <tt>i</tt>, <tt>o</tt>, <tt>u</tt>, <tt>x</tt>, or <tt>X</tt> conversion specifier applies to a  or the corresponding signed integer type argument; or that a following <tt>n</tt> conversion specifier applies to a pointer to a signed integer type corresponding to   argument.


 * <tt>t</tt> : (C99) Specifies that a following <tt>d</tt>, <tt>i</tt>, <tt>o</tt>, <tt>u</tt>, <tt>x</tt>, or <tt>X</tt> conversion specifier applies to a  or the corresponding unsigned integer type argument; or that a following <tt>n</tt> conversion specifier applies to a pointer to a   argument.


 * <tt>L</tt> : Specifies that a following <tt>a</tt>, <tt>A</tt>, <tt>e</tt>, <tt>E</tt>, <tt>f</tt>, <tt>F</tt>, <tt>g</tt>, or <tt>G</tt> conversion specifier applies to a  argument.

If a length modifier appears with any conversion specifier other than as specified above, the behavior is undefined.

The conversion specifiers and their meanings are:


 * <tt>d</tt>, <tt>i</tt> : The  argument is converted to signed decimal in the style [<tt>−</tt>]dddd. The precision specifies the minimum number of digits to appear; if the value being converted can be represented in fewer digits, it is expanded with leading zeros. The default precision is 1. The result of converting a zero value with a precision of zero is no characters.


 * <tt>o</tt>, <tt>u</tt>, <tt>x</tt>, <tt>X</tt> : The  argument is converted to unsigned octal (<tt>o</tt>), unsigned decimal (<tt>u</tt>), or unsigned hexadecimal notation (<tt>x</tt> or <tt>X</tt>) in the style dddd; the letters <tt>abcdef</tt> are used for <tt>x</tt> conversion and the letters <tt>ABCDEF</tt> for <tt>X</tt> conversion. The precision specifies the minimum number of digits to appear; if the value being converted can be represented in fewer digits, it is expanded with leading zeros. The default precision is 1. The result of converting a zero value with a precision of zero is no characters.


 * <tt>f</tt>, <tt>F</tt> : A  argument representing a (finite) floating-point number is converted to decimal notation in the style [<tt>−</tt>]ddd<tt>.</tt>ddd, where the number of digits after the decimal-point character is equal to the precision specification. If the precision is missing, it is taken as 6; if the precision is zero and the <tt>#</tt> flag is not specified, no decimal-point character appears. If a decimal-point character appears, at least one digit appears before it. The value is rounded to the appropriate number of digits. (C99) A   argument representing an infinity is converted in one of the styles [<tt>-</tt>]<tt>inf</tt> or [<tt>-</tt>]<tt>infinity</tt> &mdash; which style is implementation-defined. A double argument representing a NaN is converted in one of the styles [<tt>-</tt>]<tt>nan</tt> or [<tt>-</tt>]<tt>nan(</tt>n-char-sequence<tt>)</tt> &mdash; which style, and the meaning of any n-char-sequence, is implementation-defined. The <tt>F</tt> conversion specifier produces <tt>INF</tt>, <tt>INFINITY</tt>, or <tt>NAN</tt> instead of <tt>inf</tt>, <tt>infinity</tt>, or <tt>nan</tt>, respectively. (When applied to infinite and NaN values, the <tt>-</tt>, <tt>+</tt>, and space flags have their usual meaning; the <tt>#</tt> and <tt>0</tt> flags have no effect.)


 * <tt>e</tt>, <tt>E</tt> : A  argument representing a (finite) floating-point number is converted in the style [<tt>−</tt>]d<tt>.</tt>ddd<tt>e&plusmn;</tt>dd, where there is one digit (which is nonzero if the argument is nonzero) before the decimal-point character and the number of digits after it is equal to the precision; if the precision is missing, it is taken as 6; if the precision is zero and the <tt>#</tt> flag is not specified, no decimal-point character appears. The value is rounded to the appropriate number of digits. The <tt>E</tt> conversion specifier produces a number with <tt>E</tt> instead of <tt>e</tt> introducing the exponent. The exponent always contains at least two digits, and only as many more digits as necessary to represent the exponent. If the value is zero, the exponent is zero. (C99) A   argument representing an infinity or NaN is converted in the style of an <tt>f</tt> or <tt>F</tt> conversion specifier.


 * <tt>g</tt>, <tt>G</tt> : A  argument representing a (finite) floating-point number is converted in style <tt>f</tt> or <tt>e</tt> (or in style <tt>F</tt> or <tt>E</tt> in the case of a <tt>G</tt> conversion specifier), with the precision specifying the number of significant digits. If the precision is zero, it is taken as 1. The style used depends on the value converted; style <tt>e</tt> (or <tt>E</tt>) is used only if the exponent resulting from such a conversion is less than –4 or greater than or equal to the precision. Trailing zeros are removed from the fractional portion of the result unless the <tt>#</tt> flag is specified; a decimal-point character appears only if it is followed by a digit. (C99) A   argument representing an infinity or NaN is converted in the style of an <tt>f</tt> or <tt>F</tt> conversion specifier.


 * <tt>a</tt>, <tt>A</tt> : (C99) A double argument representing a (finite) floating-point number is converted in the style [<tt>−</tt>]<tt>0x</tt>h<tt>.</tt>hhhh<tt>p&plusmn;</tt>d, where there is one hexadecimal digit (which is nonzero if the argument is a normalized floating-point number and is otherwise unspecified) before the decimal-point character (Binary implementations can choose the hexadecimal digit to the left of the decimal-point character so that subsequent digits align to nibble [4-bit] boundaries.) and the number of hexadecimal digits after it is equal to the precision; if the precision is missing and  is a power of 2, then the precision is sufficient for an exact representation of the value; if the precision is missing and   is not a power of 2, then the precision is sufficient to distinguish (The precision p is sufficient to distinguish values of the source type if 16p–1 > bn where b is   and n is the number of base-b digits in the significand of the source type. A smaller p might suffice depending on the implementation's scheme for determining the digit to the left of the decimal-point character.) values of type , except that trailing zeros may be omitted; if the precision is zero and the <tt>#</tt> flag is not specified, no decimal-point character appears. The letters <tt>abcdef</tt> are used for <tt>a</tt> conversion and the letters <tt>ABCDEF</tt> for <tt>A</tt> conversion. The <tt>A</tt> conversion specifier produces a number with <tt>X</tt> and <tt>P</tt> instead of <tt>x</tt> and <tt>p</tt>. The exponent always contains at least one digit, and only as many more digits as necessary to represent the decimal exponent of 2. If the value is zero, the exponent is zero. A   argument representing an infinity or NaN is converted in the style of an <tt>f</tt> or <tt>F</tt> conversion specifier.


 * <tt>c</tt> : If no <tt>l</tt> length modifier is present, the  argument is converted to an , and the resulting character is written. (C99) If an <tt>l</tt> length modifier is present, the   argument is converted as if by an <tt>ls</tt> conversion specification with no precision and an argument that points to the initial element of a two-element array of  , the first element containing the   argument to the <tt>lc</tt> conversion specification and the second a null wide character.


 * <tt>s</tt> : If no <tt>l</tt> length modifier is present, the argument shall be a pointer to the initial element of an array of character type. (No special provisions are made for multibyte characters.) Characters from the array are written up to (but not including) the terminating null character. If the precision is specified, no more than that many characters are written. If the precision is not specified or is greater than the size of the array, the array shall contain a null character. (C99) If an <tt>l</tt> length modifier is present, the argument shall be a pointer to the initial element of an array of  type. Wide characters from the array are converted to multibyte characters (each as if by a call to the   function, with the conversion state described by an   object initialized to zero before the first wide character is converted) up to and including a terminating null wide character. The resulting multibyte characters are written up to (but not including) the terminating null character (byte). If no precision is specified, the array shall contain a null wide character. If a precision is specified, no more than that many characters (bytes) are written (including shift sequences, if any), and the array shall contain a null wide character if, to equal the multibyte character sequence length given by the precision, the function would need to access a wide character one past the end of the array. In no case is a partial multibyte character written. (Redundant shift sequences may result if multibyte characters have a state-dependent encoding.)


 * <tt>p</tt> : The argument shall be a pointer to . The value of the pointer is converted to a sequence of printable characters, in an implementation-defined manner.


 * <tt>n</tt> : The argument shall be a pointer to signed integer into which is written the number of characters written to the output stream so far by this call to . No argument is converted, but one is consumed. If the conversion specification includes any flags, a field width, or a precision, the behavior is undefined.


 * <tt>%</tt> : A <tt>%</tt> character is written. No argument is converted. The complete conversion specification shall be <tt>%%</tt>.

If a conversion specification is invalid, the behavior is undefined. If any argument is not the correct type for the corresponding coversion specification, the behavior is undefined.

In no case does a nonexistent or small field width cause truncation of a field; if the result of a conversion is wider than the field width, the field is expanded to contain the conversion result.

For <tt>a</tt> and <tt>A</tt> conversions, if  is a power of 2, the value is correctly rounded to a hexadecimal floating number with the given precision.

It is recommended practice that if  is not a power of 2, the result should be one of the two adjacent numbers in hexadecimal floating style with the given precision, with the extra stipulation that the error should have a correct sign for the current rounding direction.

It is recommended practice that for <tt>e</tt>, <tt>E</tt>, <tt>f</tt>, <tt>F</tt>, <tt>g</tt>, and <tt>G</tt> conversions, if the number of significant decimal digits is at most, then the result should be correctly rounded. (For binary-to-decimal conversion, the result format's values are the numbers representable with the given format specifier. The number of significant digits is determined by the format specifier, and in the case of fixed-point conversion by the source value as well.) If the number of significant decimal digits is more than  but the source value is exactly representable with   digits, then the result should be an exact representation with trailing zeros. Otherwise, the source value is bounded by two adjacent decimal strings L < U, both having <tt>DECIMAL_DIG</tt> significant digits; the value of the resultant decimal string D should satisfy L &le; D &le; U, with the extra stipulation that the error should have a correct sign for the current rounding direction.

The  function returns the number of characters transmitted, or a negative value if an output or encoding error occurred.

The  function is equivalent to   with the argument   interposed before the arguments to. It returns the number of characters transmitted, or a negative value if an output error occurred.

The  function is equivalent to , except that the argument   specifies an array into which the generated input is to be written, rather than to a stream. A null character is written at the end of the characters written; it is not counted as part of the returned sum. If copying takes place between objects that overlap, the behavior is undefined. The function returns the number of characters written in the array, not counting the terminating null character.

The  function is equivalent to , with the variable argument list replaced by  , which shall have been initialized by the   macro (and possibly subsequent   calls). The  function does not invoke the   macro. The function returns the number of characters transmitted, or a negative value if an output error occurred.

The  function is equivalent to , with the variable argument list replaced by  , which shall have been initialized by the   macro (and possibly subsequent   calls). The  function does not invoke the   macro. The function returns the number of characters transmitted, or a negative value if an output error occurred.

The  function is equivalent to , with the variable argument list replaced by  , which shall have been initialized by the   macro (and possibly subsequent   calls). The  function does not invoke the   macro. If copying takes place between objects that overlap, the behavior is undefined. The function returns the number of characters written into the array, not counting the terminating null character.