Go to the first, previous, next, last section, table of contents.


Input/Output on Streams

This chapter describes the functions for creating streams and performing input and output operations on them. As discussed in section Input/Output Overview, a stream is a fairly abstract, high-level concept representing a communications channel to a file, device, or process.

Streams

For historical reasons, the type of the C data structure that represents a stream is called FILE rather than "stream". Since most of the library functions deal with objects of type FILE *, sometimes the term file pointer is also used to mean "stream". This leads to unfortunate confusion over terminology in many books on C. This manual, however, is careful to use the terms "file" and "stream" only in the technical sense.

The FILE type is declared in the header file `stdio.h'.

Data Type: FILE
This is the data type used to represent stream objects. A FILE object holds all of the internal state information about the connection to the associated file, including such things as the file position indicator and buffering information. Each stream also has error and end-of-file status indicators that can be tested with the ferror and feof functions; see section End-Of-File and Errors.

FILE objects are allocated and managed internally by the input/output library functions. Don't try to create your own objects of type FILE; let the library do it. Your programs should deal only with pointers to these objects (that is, FILE * values) rather than the objects themselves.

Standard Streams

When the main function of your program is invoked, it already has three predefined streams open and available for use. These represent the "standard" input and output channels that have been established for the process.

These streams are declared in the header file `stdio.h'.

Variable: FILE * stdin
The standard input stream, which is the normal source of input for the program.

Variable: FILE * stdout
The standard output stream, which is used for normal output from the program.

Variable: FILE * stderr
The standard error stream, which is used for error messages and diagnostics issued by the program.

In the GNU system, you can specify what files or processes correspond to these streams using the pipe and redirection facilities provided by the shell. (The primitives shells use to implement these facilities are described in section File System Interface.) Most other operating systems provide similar mechanisms, but the details of how to use them can vary.

In the GNU C library, stdin, stdout, and stderr are normal variables which you can set just like any others. For example, to redirect the standard output to a file, you could do:

fclose (stdout);
stdout = fopen ("standard-output-file", "w");

Note however, that in other systems stdin, stdout, and stderr are macros that you cannot assign to in the normal way. But you can use freopen to get the effect of closing one and reopening it. See section Opening Streams.

The three streams stdin, stdout, and stderr are not unoriented at program start (see section Streams in Internationalized Applications).

Opening Streams

Opening a file with the fopen function creates a new stream and establishes a connection between the stream and a file. This may involve creating a new file.

Everything described in this section is declared in the header file `stdio.h'.

Function: FILE * fopen (const char *filename, const char *opentype)
The fopen function opens a stream for I/O to the file filename, and returns a pointer to the stream.

The opentype argument is a string that controls how the file is opened and specifies attributes of the resulting stream. It must begin with one of the following sequences of characters:

`r'
Open an existing file for reading only.
`w'
Open the file for writing only. If the file already exists, it is truncated to zero length. Otherwise a new file is created.
`a'
Open a file for append access; that is, writing at the end of file only. If the file already exists, its initial contents are unchanged and output to the stream is appended to the end of the file. Otherwise, a new, empty file is created.
`r+'
Open an existing file for both reading and writing. The initial contents of the file are unchanged and the initial file position is at the beginning of the file.
`w+'
Open a file for both reading and writing. If the file already exists, it is truncated to zero length. Otherwise, a new file is created.
`a+'
Open or create file for both reading and appending. If the file exists, its initial contents are unchanged. Otherwise, a new file is created. The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file.

As you can see, `+' requests a stream that can do both input and output. The ISO standard says that when using such a stream, you must call fflush (see section Stream Buffering) or a file positioning function such as fseek (see section File Positioning) when switching from reading to writing or vice versa. Otherwise, internal buffers might not be emptied properly. The GNU C library does not have this limitation; you can do arbitrary reading and writing operations on a stream in whatever order.

Additional characters may appear after these to specify flags for the call. Always put the mode (`r', `w+', etc.) first; that is the only part you are guaranteed will be understood by all systems.

The GNU C library defines one additional character for use in opentype: the character `x' insists on creating a new file--if a file filename already exists, fopen fails rather than opening it. If you use `x' you are guaranteed that you will not clobber an existing file. This is equivalent to the O_EXCL option to the open function (see section Opening and Closing Files).

The character `b' in opentype has a standard meaning; it requests a binary stream rather than a text stream. But this makes no difference in POSIX systems (including the GNU system). If both `+' and `b' are specified, they can appear in either order. See section Text and Binary Streams.

If the opentype string contains the sequence ,ccs=STRING then STRING is taken as the name of a coded character set and fopen will mark the stream as wide-oriented which appropriate conversion functions in place to convert from and to the character set STRING is place. Any other stream is opened initially unoriented and the orientation is decided with the first file operation. If the first operation is a wide character operation, the stream is not only marked as wide-oriented, also the conversion functions to convert to the coded character set used for the current locale are loaded. This will not change anymore from this point on even if the locale selected for the LC_CTYPE category is changed.

Any other characters in opentype are simply ignored. They may be meaningful in other systems.

If the open fails, fopen returns a null pointer.

When the sources are compiling with _FILE_OFFSET_BITS == 64 on a 32 bit machine this function is in fact fopen64 since the LFS interface replaces transparently the old interface.

You can have multiple streams (or file descriptors) pointing to the same file open at the same time. If you do only input, this works straightforwardly, but you must be careful if any output streams are included. See section Dangers of Mixing Streams and Descriptors. This is equally true whether the streams are in one program (not usual) or in several programs (which can easily happen). It may be advantageous to use the file locking facilities to avoid simultaneous access. See section File Locks.

Function: FILE * fopen64 (const char *filename, const char *opentype)
This function is similar to fopen but the stream it returns a pointer for is opened using open64. Therefore this stream can be used even on files larger then @math{2^31} bytes on 32 bit machines.

Please note that the return type is still FILE *. There is no special FILE type for the LFS interface.

If the sources are compiled with _FILE_OFFSET_BITS == 64 on a 32 bits machine this function is available under the name fopen and so transparently replaces the old interface.

Macro: int FOPEN_MAX
The value of this macro is an integer constant expression that represents the minimum number of streams that the implementation guarantees can be open simultaneously. You might be able to open more than this many streams, but that is not guaranteed. The value of this constant is at least eight, which includes the three standard streams stdin, stdout, and stderr. In POSIX.1 systems this value is determined by the OPEN_MAX parameter; see section General Capacity Limits. In BSD and GNU, it is controlled by the RLIMIT_NOFILE resource limit; see section Limiting Resource Usage.

Function: FILE * freopen (const char *filename, const char *opentype, FILE *stream)
This function is like a combination of fclose and fopen. It first closes the stream referred to by stream, ignoring any errors that are detected in the process. (Because errors are ignored, you should not use freopen on an output stream if you have actually done any output using the stream.) Then the file named by filename is opened with mode opentype as for fopen, and associated with the same stream object stream.

If the operation fails, a null pointer is returned; otherwise, freopen returns stream.

freopen has traditionally been used to connect a standard stream such as stdin with a file of your own choice. This is useful in programs in which use of a standard stream for certain purposes is hard-coded. In the GNU C library, you can simply close the standard streams and open new ones with fopen. But other systems lack this ability, so using freopen is more portable.

When the sources are compiling with _FILE_OFFSET_BITS == 64 on a 32 bit machine this function is in fact freopen64 since the LFS interface replaces transparently the old interface.

Function: FILE * freopen64 (const char *filename, const char *opentype, FILE *stream)
This function is similar to freopen. The only difference is that on 32 bit machine the stream returned is able to read beyond the @math{2^31} bytes limits imposed by the normal interface. It should be noted that the stream pointed to by stream need not be opened using fopen64 or freopen64 since its mode is not important for this function.

If the sources are compiled with _FILE_OFFSET_BITS == 64 on a 32 bits machine this function is available under the name freopen and so transparently replaces the old interface.

In some situations it is useful to know whether a given stream is available for reading or writing. This information is normally not available and would have to be remembered separately. Solaris introduced a few functions to get this information from the stream descriptor and these functions are also available in the GNU C library.

Function: int __freadable (FILE *stream)
The __freadable function determines whether the stream stream was opened to allow reading. In this case the return value is nonzero. For write-only streams the function returns zero.

This function is declared in `stdio_ext.h'.

Function: int __fwritable (FILE *stream)
The __fwritable function determines whether the stream stream was opened to allow writing. In this case the return value is nonzero. For read-only streams the function returns zero.

This function is declared in `stdio_ext.h'.

For slightly different kind of problems there are two more functions. They provide even finer-grained information.

Function: int __freading (FILE *stream)
The __freading function determines whether the stream stream was last read from or whether it is opened read-only. In this case the return value is nonzero, otherwise it is zero. Determining whether a stream opened for reading and writing was last used for writing allows to draw conclusions about the content about the buffer, among other things.

This function is declared in `stdio_ext.h'.

Function: int __fwriting (FILE *stream)
The __fwriting function determines whether the stream stream was last written to or whether it is opened write-only. In this case the return value is nonzero, otherwise it is zero.

This function is declared in `stdio_ext.h'.

Closing Streams

When a stream is closed with fclose, the connection between the stream and the file is cancelled. After you have closed a stream, you cannot perform any additional operations on it.

Function: int fclose (FILE *stream)
This function causes stream to be closed and the connection to the corresponding file to be broken. Any buffered output is written and any buffered input is discarded. The fclose function returns a value of 0 if the file was closed successfully, and EOF if an error was detected.

It is important to check for errors when you call fclose to close an output stream, because real, everyday errors can be detected at this time. For example, when fclose writes the remaining buffered output, it might get an error because the disk is full. Even if you know the buffer is empty, errors can still occur when closing a file if you are using NFS.

The function fclose is declared in `stdio.h'.

To close all streams currently available the GNU C Library provides another function.

Function: int fcloseall (void)
This function causes all open streams of the process to be closed and the connection to corresponding files to be broken. All buffered data is written and any buffered input is discarded. The fcloseall function returns a value of 0 if all the files were closed successfully, and EOF if an error was detected.

This function should be used only in special situations, e.g., when an error occurred and the program must be aborted. Normally each single stream should be closed separately so that problems with individual streams can be identified. It is also problematic since the standard streams (see section Standard Streams) will also be closed.

The function fcloseall is declared in `stdio.h'.

If the main function to your program returns, or if you call the exit function (see section Normal Termination), all open streams are automatically closed properly. If your program terminates in any other manner, such as by calling the abort function (see section Aborting a Program) or from a fatal signal (see section Signal Handling), open streams might not be closed properly. Buffered output might not be flushed and files may be incomplete. For more information on buffering of streams, see section Stream Buffering.

Streams and Threads

Streams can be used in multi-threaded applications in the same way they are used in single-threaded applications. But the programmer must be aware of a the possible complications. It is important to know about these also if the program one writes never use threads since the design and implementation of many stream functions is heavily influenced by the requirements added by multi-threaded programming.

The POSIX standard requires that by default the stream operations are atomic. I.e., issueing two stream operations for the same stream in two threads at the same time will cause the operations to be executed as if they were issued sequentially. The buffer operations performed while reading or writing are protected from other uses of the same stream. To do this each stream has an internal lock object which has to be (implicitly) acquired before any work can be done.

But there are situations where this is not enough and there are also situations where this is not wanted. The implicit locking is not enough if the program requires more than one stream function call to happen atomically. One example would be if an output line a program wants to generate is created by several function calls. The functions by themselves would ensure only atomicity of their own operation, but not atomicity over all the function calls. For this it is necessary to perform the stream locking in the application code.

Function: void flockfile (FILE *stream)
The flockfile function acquires the internal locking object associated with the stream stream. This ensures that no other thread can explicitly through flockfile/ftrylockfile or implicit through a call of a stream function lock the stream. The thread will block until the lock is acquired. An explicit call to funlockfile has to be used to release the lock.

Function: int ftrylockfile (FILE *stream)
The ftrylockfile function tries to acquire the internal locking object associated with the stream stream just like flockfile. But unlike flockfile this function does not block if the lock is not available. ftrylockfile returns zero if the lock was successfully acquired. Otherwise the stream is locked by another thread.

Function: void funlockfile (FILE *stream)
The funlockfile function releases the internal locking object of the stream stream. The stream must have been locked before by a call to flockfile or a successful call of ftrylockfile. The implicit locking performed by the stream operations do not count. The funlockfile function does not return an error status and the behavior of a call for a stream which is not locked by the current thread is undefined.

The following example shows how the functions above can be used to generate an output line atomically even in multi-threaded applications (yes, the same job could be done with one fprintf call but it is sometimes not possible):

FILE *fp;
{
   ...
   flockfile (fp);
   fputs ("This is test number ", fp);
   fprintf (fp, "%d\n", test);
   funlockfile (fp)
}

Without the explicit locking it would be possible for another thread to use the stream fp after the fputs call return and before fprintf was called with the result that the number does not follow the word `number'.

From this description it might already be clear that the locking objects in streams are no simple mutexes. Since locking the same stream twice in the same thread is allowed the locking objects must be equivalent to recursive mutexes. These mutexes keep track of the owner and the number of times the lock is acquired. The same number of funlockfile calls by the same threads is necessary to unlock the stream completely. For instance:

void
foo (FILE *fp)
{
  ftrylockfile (fp);
  fputs ("in foo\n", fp);
  /* This is very wrong!!!  */
  funlockfile (fp);
}

It is important here that the funlockfile function is only called if the ftrylockfile function succeeded in locking the stream. It is therefore always wrong to ignore the result of ftrylockfile. And it makes no sense since otherwise one would use flockfile. The result of code like that above is that either funlockfile tries to free a stream that hasn't been locked by the current thread or it frees the stream prematurely. The code should look like this:

void
foo (FILE *fp)
{
  if (ftrylockfile (fp) == 0)
    {
      fputs ("in foo\n", fp);
      funlockfile (fp);
    }
}

Now that we covered why it is necessary to have these locking it is necessary to talk about situations when locking is unwanted and what can be done. The locking operations (explicit or implicit) don't come for free. Even if a lock is not taken the cost is not zero. The operations which have to be performed require memory operations which are save in multi-processor environments. With the many local caches involved in such systems this is quite costly. So it is best to avoid the locking completely if it is known that the code using the stream is never used in a context where more than one thread can use the stream at one time. This can be determined most of the time for application code; for library code which can be used in many contexts one should default to be conservative and use locking.

There are two basic mechanisms to avoid locking. The first is to use the _unlocked variants of the stream operations. The POSIX standard defines quite a few of those and the GNU library adds a few more. These variants of the functions behave just like the functions with the name without the suffix except that they are not locking the stream. Using these functions is very desirable since they are potentially much faster. This is not only because the locking operation itself is avoided. More importantly, functions like putc and getc are very simple and tradionally (before the introduction of threads) were implemented as macros which are very fast if the buffer is not empty. With locking required these functions are now no macros anymore (the code generated would be too much). But these macros are still available with the same functionality under the new names putc_unlocked and getc_unlocked. This possibly huge difference of speed also suggests the use of the _unlocked functions even if locking is required. The difference is that the locking then has to be performed in the program:

void
foo (FILE *fp, char *buf)
{
  flockfile (fp);
  while (*buf != '/')
    putc_unlocked (*buf++, fp);
  funlockfile (fp);
}

If in this example the putc function would be used and the explicit locking would be missing the putc function would have to acquire the lock in every call, potentially many times depending on when the loop terminates. Writing it the way illustrated above allows the putc_unlocked macro to be used which means no locking and direct manipulation of the buffer of the stream.

A second way to avoid locking is by using a non-standard function which was introduced in Solaris and is available in the GNU C library as well.

Function: int __fsetlocking (FILE *stream, int type)

The __fsetlocking function can be used to select whether the stream operations will implicitly acquire the locking object of the stream stream. By default this is done but it can be disabled and reinstated using this function. There are three values defined for the type parameter.

FSETLOCKING_INTERNAL
The stream stream will from now on use the default internal locking. Every stream operation with exception of the _unlocked variants will implicitly lock the stream.
FSETLOCKING_BYCALLER
After the __fsetlocking function returns the user is responsible for locking the stream. None of the stream operations will implicitly do this anymore until the state is set back to FSETLOCKING_INTERNAL.
FSETLOCKING_QUERY
__fsetlocking only queries the current locking state of the stream. The return value will be FSETLOCKING_INTERNAL or FSETLOCKING_BYCALLER depending on the state.

The return value of __fsetlocking is either FSETLOCKING_INTERNAL or FSETLOCKING_BYCALLER depending on the state of the stream before the call.

This function and the values for the type parameter are declared in `stdio_ext.h'.

This function is especially useful when program code has to be used which is written without knowledge about the _unlocked functions (or if the programmer was to lazy to use them).

Streams in Internationalized Applications

ISO C90 introduced the new type wchar_t to allow handling larger character sets. What was missing was a possibility to output strings of wchar_t directly. One had to convert them into multibyte strings using mbstowcs (there was no mbsrtowcs yet) and then use the normal stream functions. While this is doable it is very cumbersome since performing the conversions is not trivial and greatly increases program complexity and size.

The Unix standard early on (I think in XPG4.2) introduced two additional format specifiers for the printf and scanf families of functions. Printing and reading of single wide characters was made possible using the %C specifier and wide character strings can be handled with %S. These modifiers behave just like %c and %s only that they expect the corresponding argument to have the wide character type and that the wide character and string are transformed into/from multibyte strings before being used.

This was a beginning but it is still not good enough. Not always is it desirable to use printf and scanf. The other, smaller and faster functions cannot handle wide characters. Second, it is not possible to have a format string for printf and scanf consisting of wide characters. The result is that format strings would have to be generated if they have to contain non-basic characters.

In the Amendment 1 to ISO C90 a whole new set of functions was added to solve the problem. Most of the stream functions got a counterpart which take a wide character or wide character string instead of a character or string respectively. The new functions operate on the same streams (like stdout). This is different from the model of the C++ runtime library where separate streams for wide and normal I/O are used.

Being able to use the same stream for wide and normal operations comes with a restriction: a stream can be used either for wide operations or for normal operations. Once it is decided there is no way back. Only a call to freopen or freopen64 can reset the orientation. The orientation can be decided in three ways:

It is important to never mix the use of wide and not wide operations on a stream. There are no diagnostics issued. The application behavior will simply be strange or the application will simply crash. The fwide function can help avoiding this.

Function: int fwide (FILE *stream, int mode)

The fwide function can be used to set and query the state of the orientation of the stream stream. If the mode parameter has a positive value the streams get wide oriented, for negative values narrow oriented. It is not possible to overwrite previous orientations with fwide. I.e., if the stream stream was already oriented before the call nothing is done.

If mode is zero the current orientation state is queried and nothing is changed.

The fwide function returns a negative value, zero, or a positive value if the stream is narrow, not at all, or wide oriented respectively.

This function was introduced in Amendment 1 to ISO C90 and is declared in `wchar.h'.

It is generally a good idea to orient a stream as early as possible. This can prevent surprise especially for the standard streams stdin, stdout, and stderr. If some library function in some situations uses one of these streams and this use orients the stream in a different way the rest of the application expects it one might end up with hard to reproduce errors. Remember that no errors are signal if the streams are used incorrectly. Leaving a stream unoriented after creation is normally only necessary for library functions which create streams which can be used in different contexts.

When writing code which uses streams and which can be used in different contexts it is important to query the orientation of the stream before using it (unless the rules of the library interface demand a specific orientation). The following little, silly function illustrates this.

void
print_f (FILE *fp)
{
  if (fwide (fp, 0) > 0)
    /* Positive return value means wide orientation.  */
    fputwc (L'f', fp);
  else
    fputc ('f', fp);
}

Note that in this case the function print_f decides about the orientation of the stream if it was unoriented before (will not happen if the advise above is followed).

The encoding used for the wchar_t values is unspecified and the user must not make any assumptions about it. For I/O of wchar_t values this means that it is impossible to write these values directly to the stream. This is not what follows from the ISO C locale model either. What happens instead is that the bytes read from or written to the underlying media are first converted into the internal encoding chosen by the implementation for wchar_t. The external encoding is determined by the LC_CTYPE category of the current locale or by the `ccs' part of the mode specification given to fopen, fopen64, freopen, or freopen64. How and when the conversion happens is unspecified and it happens invisible to the user.

Since a stream is created in the unoriented state it has at that point no conversion associated with it. The conversion which will be used is determined by the LC_CTYPE category selected at the time the stream is oriented. If the locales are changed at the runtime this might produce surprising results unless one pays attention. This is just another good reason to orient the stream explicitly as soon as possible, perhaps with a call to fwide.

Simple Output by Characters or Lines

This section describes functions for performing character- and line-oriented output.

These narrow streams functions are declared in the header file `stdio.h' and the wide stream functions in `wchar.h'.

Function: int fputc (int c, FILE *stream)
The fputc function converts the character c to type unsigned char, and writes it to the stream stream. EOF is returned if a write error occurs; otherwise the character c is returned.

Function: wint_t fputwc (wchar_t wc, FILE *stream)
The fputwc function writes the wide character wc to the stream stream. WEOF is returned if a write error occurs; otherwise the character wc is returned.

Function: int fputc_unlocked (int c, FILE *stream)
The fputc_unlocked function is equivalent to the fputc function except that it does not implicitly lock the stream.

Function: wint_t fputwc_unlocked (wint_t wc, FILE *stream)
The fputwc_unlocked function is equivalent to the fputwc function except that it does not implicitly lock the stream.

This function is a GNU extension.

Function: int putc (int c, FILE *stream)
This is just like fputc, except that most systems implement it as a macro, making it faster. One consequence is that it may evaluate the stream argument more than once, which is an exception to the general rule for macros. putc is usually the best function to use for writing a single character.

Function: wint_t putwc (wchar_t wc, FILE *stream)
This is just like fputwc, except that it can be implement as a macro, making it faster. One consequence is that it may evaluate the stream argument more than once, which is an exception to the general rule for macros. putwc is usually the best function to use for writing a single wide character.

Function: int putc_unlocked (int c, FILE *stream)
The putc_unlocked function is equivalent to the putc function except that it does not implicitly lock the stream.

Function: wint_t putwc_unlocked (wchar_t wc, FILE *stream)
The putwc_unlocked function is equivalent to the putwc function except that it does not implicitly lock the stream.

This function is a GNU extension.

Function: int putchar (int c)
The putchar function is equivalent to putc with stdout as the value of the stream argument.

Function: wint_t putwchar (wchar_t wc)
The putwchar function is equivalent to putwc with stdout as the value of the stream argument.

Function: int putchar_unlocked (int c)
The putchar_unlocked function is equivalent to the putchar function except that it does not implicitly lock the stream.

Function: wint_t putwchar_unlocked (wchar_t wc)
The putwchar_unlocked function is equivalent to the putwchar function except that it does not implicitly lock the stream.

This function is a GNU extension.

Function: int fputs (const char *s, FILE *stream)
The function fputs writes the string s to the stream stream. The terminating null character is not written. This function does not add a newline character, either. It outputs only the characters in the string.

This function returns EOF if a write error occurs, and otherwise a non-negative value.

For example:

fputs ("Are ", stdout);
fputs ("you ", stdout);
fputs ("hungry?\n", stdout);

outputs the text `Are you hungry?' followed by a newline.

Function: int fputws (const wchar_t *ws, FILE *stream)
The function fputws writes the wide character string ws to the stream stream. The terminating null character is not written. This function does not add a newline character, either. It outputs only the characters in the string.

This function returns WEOF if a write error occurs, and otherwise a non-negative value.

Function: int fputs_unlocked (const char *s, FILE *stream)
The fputs_unlocked function is equivalent to the fputs function except that it does not implicitly lock the stream.

This function is a GNU extension.

Function: int fputws_unlocked (const wchar_t *ws, FILE *stream)
The fputws_unlocked function is equivalent to the fputws function except that it does not implicitly lock the stream.

This function is a GNU extension.

Function: int puts (const char *s)
The puts function writes the string s to the stream stdout followed by a newline. The terminating null character of the string is not written. (Note that fputs does not write a newline as this function does.)

puts is the most convenient function for printing simple messages. For example:

puts ("This is a message.");

outputs the text `This is a message.' followed by a newline.

Function: int putw (int w, FILE *stream)
This function writes the word w (that is, an int) to stream. It is provided for compatibility with SVID, but we recommend you use fwrite instead (see section Block Input/Output).

Character Input

This section describes functions for performing character-oriented input. These narrow streams functions are declared in the header file `stdio.h' and the wide character functions are declared in `wchar.h'.

These functions return an int or wint_t value (for narrow and wide stream functions respectively) that is either a character of input, or the special value EOF/WEOF (usually -1). For the narrow stream functions it is important to store the result of these functions in a variable of type int instead of char, even when you plan to use it only as a character. Storing EOF in a char variable truncates its value to the size of a character, so that it is no longer distinguishable from the valid character `(char) -1'. So always use an int for the result of getc and friends, and check for EOF after the call; once you've verified that the result is not EOF, you can be sure that it will fit in a `char' variable without loss of information.

Function: int fgetc (FILE *stream)
This function reads the next character as an unsigned char from the stream stream and returns its value, converted to an int. If an end-of-file condition or read error occurs, EOF is returned instead.

Function: wint_t fgetwc (FILE *stream)
This function reads the next wide character from the stream stream and returns its value. If an end-of-file condition or read error occurs, WEOF is returned instead.

Function: int fgetc_unlocked (FILE *stream)
The fgetc_unlocked function is equivalent to the fgetc function except that it does not implicitly lock the stream.

Function: wint_t fgetwc_unlocked (FILE *stream)
The fgetwc_unlocked function is equivalent to the fgetwc function except that it does not implicitly lock the stream.

This function is a GNU extension.

Function: int getc (FILE *stream)
This is just like fgetc, except that it is permissible (and typical) for it to be implemented as a macro that evaluates the stream argument more than once. getc is often highly optimized, so it is usually the best function to use to read a single character.

Function: wint_t getwc (FILE *stream)
This is just like fgetwc, except that it is permissible for it to be implemented as a macro that evaluates the stream argument more than once. getwc can be highly optimized, so it is usually the best function to use to read a single wide character.

Function: int getc_unlocked (FILE *stream)
The getc_unlocked function is equivalent to the getc function except that it does not implicitly lock the stream.

Function: wint_t getwc_unlocked (FILE *stream)
The getwc_unlocked function is equivalent to the getwc function except that it does not implicitly lock the stream.

This function is a GNU extension.

Function: int getchar (void)
The getchar function is equivalent to getc with stdin as the value of the stream argument.

Function: wint_t getwchar (void)
The getwchar function is equivalent to getwc with stdin as the value of the stream argument.

Function: int getchar_unlocked (void)
The getchar_unlocked function is equivalent to the getchar function except that it does not implicitly lock the stream.

Function: wint_t getwchar_unlocked (void)
The getwchar_unlocked function is equivalent to the getwchar function except that it does not implicitly lock the stream.

This function is a GNU extension.

Here is an example of a function that does input using fgetc. It would work just as well using getc instead, or using getchar () instead of fgetc (stdin). The code would also work the same for the wide character stream functions.

int
y_or_n_p (const char *question)
{
  fputs (question, stdout);
  while (1)
    {
      int c, answer;
      /* Write a space to separate answer from question. */
      fputc (' ', stdout);
      /* Read the first character of the line.
         This should be the answer character, but might not be. */
      c = tolower (fgetc (stdin));
      answer = c;
      /* Discard rest of input line. */
      while (c != '\n' && c != EOF)
        c = fgetc (stdin);
      /* Obey the answer if it was valid. */
      if (answer == 'y')
        return 1;
      if (answer == 'n')
        return 0;
      /* Answer was invalid: ask for valid answer. */
      fputs ("Please answer y or n:", stdout);
    }
}

Function: int getw (FILE *stream)
This function reads a word (that is, an int) from stream. It's provided for compatibility with SVID. We recommend you use fread instead (see section Block Input/Output). Unlike getc, any int value could be a valid result. getw returns EOF when it encounters end-of-file or an error, but there is no way to distinguish this from an input word with value -1.

Line-Oriented Input

Since many programs interpret input on the basis of lines, it is convenient to have functions to read a line of text from a stream.

Standard C has functions to do this, but they aren't very safe: null characters and even (for gets) long lines can confuse them. So the GNU library provides the nonstandard getline function that makes it easy to read lines reliably.

Another GNU extension, getdelim, generalizes getline. It reads a delimited record, defined as everything through the next occurrence of a specified delimiter character.

All these functions are declared in `stdio.h'.

Function: ssize_t getline (char **lineptr, size_t *n, FILE *stream)
This function reads an entire line from stream, storing the text (including the newline and a terminating null character) in a buffer and storing the buffer address in *lineptr.

Before calling getline, you should place in *lineptr the address of a buffer *n bytes long, allocated with malloc. If this buffer is long enough to hold the line, getline stores the line in this buffer. Otherwise, getline makes the buffer bigger using realloc, storing the new buffer address back in *lineptr and the increased size back in *n. See section Unconstrained Allocation.

If you set *lineptr to a null pointer, and *n to zero, before the call, then getline allocates the initial buffer for you by calling malloc.

In either case, when getline returns, *lineptr is a char * which points to the text of the line.

When getline is successful, it returns the number of characters read (including the newline, but not including the terminating null). This value enables you to distinguish null characters that are part of the line from the null character inserted as a terminator.

This function is a GNU extension, but it is the recommended way to read lines from a stream. The alternative standard functions are unreliable.

If an error occurs or end of file is reached without any bytes read, getline returns -1.

Function: ssize_t getdelim (char **lineptr, size_t *n, int delimiter, FILE *stream)
This function is like getline except that the character which tells it to stop reading is not necessarily newline. The argument delimiter specifies the delimiter character; getdelim keeps reading until it sees that character (or end of file).

The text is stored in lineptr, including the delimiter character and a terminating null. Like getline, getdelim makes lineptr bigger if it isn't big enough.

getline is in fact implemented in terms of getdelim, just like this:

ssize_t
getline (char **lineptr, size_t *n, FILE *stream)
{
  return getdelim (lineptr, n, '\n', stream);
}

Function: char * fgets (char *s, int count, FILE *stream)
The fgets function reads characters from the stream stream up to and including a newline character and stores them in the string s, adding a null character to mark the end of the string. You must supply count characters worth of space in s, but the number of characters read is at most count - 1. The extra character space is used to hold the null character at the end of the string.

If the system is already at end of file when you call fgets, then the contents of the array s are unchanged and a null pointer is returned. A null pointer is also returned if a read error occurs. Otherwise, the return value is the pointer s.

Warning: If the input data has a null character, you can't tell. So don't use fgets unless you know the data cannot contain a null. Don't use it to read files edited by the user because, if the user inserts a null character, you should either handle it properly or print a clear error message. We recommend using getline instead of fgets.

Function: wchar_t * fgetws (wchar_t *ws, int count, FILE *stream)
The fgetws function reads wide characters from the stream stream up to and including a newline character and stores them in the string ws, adding a null wide character to mark the end of the string. You must supply count wide characters worth of space in ws, but the number of characters read is at most count - 1. The extra character space is used to hold the null wide character at the end of the string.

If the system is already at end of file when you call fgetws, then the contents of the array ws are unchanged and a null pointer is returned. A null pointer is also returned if a read error occurs. Otherwise, the return value is the pointer ws.

Warning: If the input data has a null wide character (which are null bytes in the input stream), you can't tell. So don't use fgetws unless you know the data cannot contain a null. Don't use it to read files edited by the user because, if the user inserts a null character, you should either handle it properly or print a clear error message.

Function: char * fgets_unlocked (char *s, int count, FILE *stream)
The fgets_unlocked function is equivalent to the fgets function except that it does not implicitly lock the stream.

This function is a GNU extension.

Function: wchar_t * fgetws_unlocked (wchar_t *ws, int count, FILE *stream)
The fgetws_unlocked function is equivalent to the fgetws function except that it does not implicitly lock the stream.

This function is a GNU extension.

Deprecated function: char * gets (char *s)
The function gets reads characters from the stream stdin up to the next newline character, and stores them in the string s. The newline character is discarded (note that this differs from the behavior of fgets, which copies the newline character into the string). If gets encounters a read error or end-of-file, it returns a null pointer; otherwise it returns s.

Warning: The gets function is very dangerous because it provides no protection against overflowing the string s. The GNU library includes it for compatibility only. You should always use fgets or getline instead. To remind you of this, the linker (if using GNU ld) will issue a warning whenever you use gets.

Unreading

In parser programs it is often useful to examine the next character in the input stream without removing it from the stream. This is called "peeking ahead" at the input because your program gets a glimpse of the input it will read next.

Using stream I/O, you can peek ahead at input by first reading it and then unreading it (also called pushing it back on the stream). Unreading a character makes it available to be input again from the stream, by the next call to fgetc or other input function on that stream.

What Unreading Means

Here is a pictorial explanation of unreading. Suppose you have a stream reading a file that contains just six characters, the letters `foobar'. Suppose you have read three characters so far. The situation looks like this:

f  o  o  b  a  r
         ^

so the next input character will be `b'.

If instead of reading `b' you unread the letter `o', you get a situation like this:

f  o  o  b  a  r
         |
      o--
      ^

so that the next input characters will be `o' and `b'.

If you unread `9' instead of `o', you get this situation:

f  o  o  b  a  r
         |
      9--
      ^

so that the next input characters will be `9' and `b'.

Using ungetc To Do Unreading

The function to unread a character is called ungetc, because it reverses the action of getc.

Function: int ungetc (int c, FILE *stream)
The ungetc function pushes back the character c onto the input stream stream. So the next input from stream will read c before anything else.

If c is EOF, ungetc does nothing and just returns EOF. This lets you call ungetc with the return value of getc without needing to check for an error from getc.

The character that you push back doesn't have to be the same as the last character that was actually read from the stream. In fact, it isn't necessary to actually read any characters from the stream before unreading them with ungetc! But that is a strange way to write a program; usually ungetc is used only to unread a character that was just read from the same stream.

The GNU C library only supports one character of pushback--in other words, it does not work to call ungetc twice without doing input in between. Other systems might let you push back multiple characters; then reading from the stream retrieves the characters in the reverse order that they were pushed.

Pushing back characters doesn't alter the file; only the internal buffering for the stream is affected. If a file positioning function (such as fseek, fseeko or rewind; see section File Positioning) is called, any pending pushed-back characters are discarded.

Unreading a character on a stream that is at end of file clears the end-of-file indicator for the stream, because it makes the character of input available. After you read that character, trying to read again will encounter end of file.

Function: wint_t ungetwc (wint_t wc, FILE *stream)
The ungetwc function behaves just like ungetc just that it pushes back a wide character.

Here is an example showing the use of getc and ungetc to skip over whitespace characters. When this function reaches a non-whitespace character, it unreads that character to be seen again on the next read operation on the stream.

#include <stdio.h>
#include <ctype.h>

void
skip_whitespace (FILE *stream)
{
  int c;
  do
    /* No need to check for EOF because it is not
       isspace, and ungetc ignores EOF.  */
    c = getc (stream);
  while (isspace (c));
  ungetc (c, stream);
}

Block Input/Output

This section describes how to do input and output operations on blocks of data. You can use these functions to read and write binary data, as well as to read and write text in fixed-size blocks instead of by characters or lines.

Binary files are typically used to read and write blocks of data in the same format as is used to represent the data in a running program. In other words, arbitrary blocks of memory--not just character or string objects--can be written to a binary file, and meaningfully read in again by the same program.

Storing data in binary form is often considerably more efficient than using the formatted I/O functions. Also, for floating-point numbers, the binary form avoids possible loss of precision in the conversion process. On the other hand, binary files can't be examined or modified easily using many standard file utilities (such as text editors), and are not portable between different implementations of the language, or different kinds of computers.

These functions are declared in `stdio.h'.

Function: size_t fread (void *data, size_t size, size_t count, FILE *stream)
This function reads up to count objects of size size into the array data, from the stream stream. It returns the number of objects actually read, which might be less than count if a read error occurs or the end of the file is reached. This function returns a value of zero (and doesn't read anything) if either size or count is zero.

If fread encounters end of file in the middle of an object, it returns the number of complete objects read, and discards the partial object. Therefore, the stream remains at the actual end of the file.

Function: size_t fread_unlocked (void *data, size_t size, size_t count, FILE *stream)
The fread_unlocked function is equivalent to the fread function except that it does not implicitly lock the stream.

This function is a GNU extension.

Function: size_t fwrite (const void *data, size_t size, size_t count, FILE *stream)
This function writes up to count objects of size size from the array data, to the stream stream. The return value is normally count, if the call succeeds. Any other value indicates some sort of error, such as running out of space.

Function: size_t fwrite_unlocked (const void *data, size_t size, size_t count, FILE *stream)
The fwrite_unlocked function is equivalent to the fwrite function except that it does not implicitly lock the stream.

This function is a GNU extension.

Formatted Output

The functions described in this section (printf and related functions) provide a convenient way to perform formatted output. You call printf with a format string or template string that specifies how to format the values of the remaining arguments.

Unless your program is a filter that specifically performs line- or character-oriented processing, using printf or one of the other related functions described in this section is usually the easiest and most concise way to perform output. These functions are especially useful for printing error messages, tables of data, and the like.

Formatted Output Basics

The printf function can be used to print any number of arguments. The template string argument you supply in a call provides information not only about the number of additional arguments, but also about their types and what style should be used for printing them.

Ordinary characters in the template string are simply written to the output stream as-is, while conversion specifications introduced by a `%' character in the template cause subsequent arguments to be formatted and written to the output stream. For example,

int pct = 37;
char filename[] = "foo.txt";
printf ("Processing of `%s' is %d%% finished.\nPlease be patient.\n",
        filename, pct);

produces output like

Processing of `foo.txt' is 37% finished.
Please be patient.

This example shows the use of the `%d' conversion to specify that an int argument should be printed in decimal notation, the `%s' conversion to specify printing of a string argument, and the `%%' conversion to print a literal `%' character.

There are also conversions for printing an integer argument as an unsigned value in octal, decimal, or hexadecimal radix (`%o', `%u', or `%x', respectively); or as a character value (`%c').

Floating-point numbers can be printed in normal, fixed-point notation using the `%f' conversion or in exponential notation using the `%e' conversion. The `%g' conversion uses either `%e' or `%f' format, depending on what is more appropriate for the magnitude of the particular number.

You can control formatting more precisely by writing modifiers between the `%' and the character that indicates which conversion to apply. These slightly alter the ordinary behavior of the conversion. For example, most conversion specifications permit you to specify a minimum field width and a flag indicating whether you want the result left- or right-justified within the field.

The specific flags and modifiers that are permitted and their interpretation vary depending on the particular conversion. They're all described in more detail in the following sections. Don't worry if this all seems excessively complicated at first; you can almost always get reasonable free-format output without using any of the modifiers at all. The modifiers are mostly used to make the output look "prettier" in tables.

Output Conversion Syntax

This section provides details about the precise syntax of conversion specifications that can appear in a printf template string.

Characters in the template string that are not part of a conversion specification are printed as-is to the output stream. Multibyte character sequences (see section Character Set Handling) are permitted in a template string.

The conversion specifications in a printf template string have the general form:

% [ param-no $] flags width [ . precision ] type conversion

For example, in the conversion specifier `%-10.8ld', the `-' is a flag, `10' specifies the field width, the precision is `8', the letter `l' is a type modifier, and `d' specifies the conversion style. (This particular type specifier says to print a long int argument in decimal notation, with a minimum of 8 digits left-justified in a field at least 10 characters wide.)

In more detail, output conversion specifications consist of an initial `%' character followed in sequence by:

The exact options that are permitted and how they are interpreted vary between the different conversion specifiers. See the descriptions of the individual conversions for information about the particular options that they use.

With the `-Wformat' option, the GNU C compiler checks calls to printf and related functions. It examines the format string and verifies that the correct number and types of arguments are supplied. There is also a GNU C syntax to tell the compiler that a function you write uses a printf-style format string. See section `Declaring Attributes of Functions' in Using GNU CC, for more information.

Table of Output Conversions

Here is a table summarizing what all the different conversions do:

`%d', `%i'
Print an integer as a signed decimal number. See section Integer Conversions, for details. `%d' and `%i' are synonymous for output, but are different when used with scanf for input (see section Table of Input Conversions).
`%o'
Print an integer as an unsigned octal number. See section Integer Conversions, for details.
`%u'
Print an integer as an unsigned decimal number. See section Integer Conversions, for details.
`%x', `%X'
Print an integer as an unsigned hexadecimal number. `%x' uses lower-case letters and `%X' uses upper-case. See section Integer Conversions, for details.
`%f'
Print a floating-point number in normal (fixed-point) notation. See section Floating-Point Conversions, for details.
`%e', `%E'
Print a floating-point number in exponential notation. `%e' uses lower-case letters and `%E' uses upper-case. See section Floating-Point Conversions, for details.
`%g', `%G'
Print a floating-point number in either normal or exponential notation, whichever is more appropriate for its magnitude. `%g' uses lower-case letters and `%G' uses upper-case. See section Floating-Point Conversions, for details.
`%a', `%A'
Print a floating-point number in a hexadecimal fractional notation which the exponent to base 2 represented in decimal digits. `%a' uses lower-case letters and `%A' uses upper-case. See section Floating-Point Conversions, for details.
`%c'
Print a single character. See section Other Output Conversions.
`%C'
This is an alias for `%lc' which is supported for compatibility with the Unix standard.
`%s'
Print a string. See section Other Output Conversions.
`%S'
This is an alias for `%ls' which is supported for compatibility with the Unix standard.
`%p'
Print the value of a pointer. See section Other Output Conversions.
`%n'
Get the number of characters printed so far. See section Other Output Conversions. Note that this conversion specification never produces any output.
`%m'
Print the string corresponding to the value of errno. (This is a GNU extension.) See section Other Output Conversions.
`%%'
Print a literal `%' character. See section Other Output Conversions.

If the syntax of a conversion specification is invalid, unpredictable things will happen, so don't do this. If there aren't enough function arguments provided to supply values for all the conversion specifications in the template string, or if the arguments are not of the correct types, the results are unpredictable. If you supply more arguments than conversion specifications, the extra argument values are simply ignored; this is sometimes useful.

Integer Conversions

This section describes the options for the `%d', `%i', `%o', `%u', `%x', and `%X' conversion specifications. These conversions print integers in various formats.

The `%d' and `%i' conversion specifications both print an int argument as a signed decimal number; while `%o', `%u', and `%x' print the argument as an unsigned octal, decimal, or hexadecimal number (respectively). The `%X' conversion specification is just like `%x' except that it uses the characters `ABCDEF' as digits instead of `abcdef'.

The following flags are meaningful:

`-'
Left-justify the result in the field (instead of the normal right-justification).
`+'
For the signed `%d' and `%i' conversions, print a plus sign if the value is positive.
` '
For the signed `%d' and `%i' conversions, if the result doesn't start with a plus or minus sign, prefix it with a space character instead. Since the `+' flag ensures that the result includes a sign, this flag is ignored if you supply both of them.
`#'
For the `%o' conversion, this forces the leading digit to be `0', as if by increasing the precision. For `%x' or `%X', this prefixes a leading `0x' or `0X' (respectively) to the result. This doesn't do anything useful for the `%d', `%i', or `%u' conversions. Using this flag produces output which can be parsed by the strtoul function (see section Parsing of Integers) and scanf with the `%i' conversion (see section Numeric Input Conversions).
`''
Separate the digits into groups as specified by the locale specified for the LC_NUMERIC category; see section Generic Numeric Formatting Parameters. This flag is a GNU extension.
`0'
Pad the field with zeros instead of spaces. The zeros are placed after any indication of sign or base. This flag is ignored if the `-' flag is also specified, or if a precision is specified.

If a precision is supplied, it specifies the minimum number of digits to appear; leading zeros are produced if necessary. If you don't specify a precision, the number is printed with as many digits as it needs. If you convert a value of zero with an explicit precision of zero, then no characters at all are produced.

Without a type modifier, the corresponding argument is treated as an int (for the signed conversions `%i' and `%d') or unsigned int (for the unsigned conversions `%o', `%u', `%x', and `%X'). Recall that since printf and friends are variadic, any char and short arguments are automatically converted to int by the default argument promotions. For arguments of other integer types, you can use these modifiers:

`hh'
Specifies that the argument is a signed char or unsigned char, as appropriate. A char argument is converted to an int or unsigned int by the default argument promotions anyway, but the `h' modifier says to convert it back to a char again. This modifier was introduced in ISO C99.
`h'
Specifies that the argument is a short int or unsigned short int, as appropriate. A short argument is converted to an int or unsigned int by the default argument promotions anyway, but the `h' modifier says to convert it back to a short again.
`j'
Specifies that the argument is a intmax_t or uintmax_t, as appropriate. This modifier was introduced in ISO C99.
`l'
Specifies that the argument is a long int or unsigned long int, as appropriate. Two `l' characters is like the `L' modifier, below. If used with `%c' or `%s' the corresponding parameter is considered as a wide character or wide character string respectively. This use of `l' was introduced in Amendment 1 to ISO C90.
`L'
`ll'
`q'
Specifies that the argument is a long long int. (This type is an extension supported by the GNU C compiler. On systems that don't support extra-long integers, this is the same as long int.) The `q' modifier is another name for the same thing, which comes from 4.4 BSD; a long long int is sometimes called a "quad" int.
`t'
Specifies that the argument is a ptrdiff_t. This modifier was introduced in ISO C99.
`z'
`Z'
Specifies that the argument is a size_t. `z' was introduced in ISO C99. `Z' is a GNU extension predating this addition and should not be used in new code.

Here is an example. Using the template string:

"|%5d|%-5d|%+5d|%+-5d|% 5d|%05d|%5.0d|%5.2d|%d|\n"

to print numbers using the different options for the `%d' conversion gives results like:

|    0|0    |   +0|+0   |    0|00000|     |   00|0|
|    1|1    |   +1|+1   |    1|00001|    1|   01|1|
|   -1|-1   |   -1|-1   |   -1|-0001|   -1|  -01|-1|
|100000|100000|+100000| 100000|100000|100000|100000|100000|

In particular, notice what happens in the last case where the number is too large to fit in the minimum field width specified.

Here are some more examples showing how unsigned integers print under various format options, using the template string:

"|%5u|%5o|%5x|%5X|%#5o|%#5x|%#5X|%#10.8x|\n"
|    0|    0|    0|    0|    0|  0x0|  0X0|0x00000000|
|    1|    1|    1|    1|   01|  0x1|  0X1|0x00000001|
|100000|303240|186a0|186A0|0303240|0x186a0|0X186A0|0x000186a0|

Floating-Point Conversions

This section discusses the conversion specifications for floating-point numbers: the `%f', `%e', `%E', `%g', and `%G' conversions.

The `%f' conversion prints its argument in fixed-point notation, producing output of the form [-]ddd.ddd, where the number of digits following the decimal point is controlled by the precision you specify.

The `%e' conversion prints its argument in exponential notation, producing output of the form [-]d.ddde[+|-]dd. Again, the number of digits following the decimal point is controlled by the precision. The exponent always contains at least two digits. The `%E' conversion is similar but the exponent is marked with the letter `E' instead of `e'.

The `%g' and `%G' conversions print the argument in the style of `%e' or `%E' (respectively) if the exponent would be less than -4 or greater than or equal to the precision; otherwise they use the `%f' style. Trailing zeros are removed from the fractional portion of the result and a decimal-point character appears only if it is followed by a digit.

The `%a' and `%A' conversions are meant for representing floating-point numbers exactly in textual form so that they can be exchanged as texts between different programs and/or machines. The numbers are represented is the form [-]0xh.hhhp[+|-]dd. At the left of the decimal-point character exactly one digit is print. This character is only 0 if the number is denormalized. Otherwise the value is unspecified; it is implementation dependent how many bits are used. The number of hexadecimal digits on the right side of the decimal-point character is equal to the precision. If the precision is zero it is determined to be large enough to provide an exact representation of the number (or it is large enough to distinguish two adjacent values if the FLT_RADIX is not a power of 2, see section Floating Point Parameters). For the `%a' conversion lower-case characters are used to represent the hexadecimal number and the prefix and exponent sign are printed as 0x and p respectively. Otherwise upper-case characters are used and 0X and P are used for the representation of prefix and exponent string. The exponent to the base of two is printed as a decimal number using at least one digit but at most as many digits as necessary to represent the value exactly.

If the value to be printed represents infinity or a NaN, the output is [-]inf or nan respectively if the conversion specifier is `%a', `%e', `%f', or `%g' and it is [-]INF or NAN respectively if the conversion is `%A', `%E', or `%G'.

The following flags can be used to modify the behavior:

`-'
Left-justify the result in the field. Normally the result is right-justified.
`+'
Always include a plus or minus sign in the result.
` '
If the result doesn't start with a plus or minus sign, prefix it with a space instead. Since the `+' flag ensures that the result includes a sign, this flag is ignored if you supply both of them.
`#'
Specifies that the result should always include a decimal point, even if no digits follow it. For the `%g' and `%G' conversions, this also forces trailing zeros after the decimal point to be left in place where they would otherwise be removed.
`''
Separate the digits of the integer part of the result into groups as specified by the locale specified for the LC_NUMERIC category; see section Generic Numeric Formatting Parameters. This flag is a GNU extension.
`0'
Pad the field with zeros instead of spaces; the zeros are placed after any sign. This flag is ignored if the `-' flag is also specified.

The precision specifies how many digits follow the decimal-point character for the `%f', `%e', and `%E' conversions. For these conversions, the default precision is 6. If the precision is explicitly 0, this suppresses the decimal point character entirely. For the `%g' and `%G' conversions, the precision specifies how many significant digits to print. Significant digits are the first digit before the decimal point, and all the digits after it. If the precision is 0 or not specified for `%g' or `%G', it is treated like a value of 1. If the value being printed cannot be expressed accurately in the specified number of digits, the value is rounded to the nearest number that fits.

Without a type modifier, the floating-point conversions use an argument of type double. (By the default argument promotions, any float arguments are automatically converted to double.) The following type modifier is supported:

`L'
An uppercase `L' specifies that the argument is a long double.

Here are some examples showing how numbers print using the various floating-point conversions. All of the numbers were printed using this template string:

"|%13.4a|%13.4f|%13.4e|%13.4g|\n"

Here is the output:

|  0x0.0000p+0|       0.0000|   0.0000e+00|            0|
|  0x1.0000p-1|       0.5000|   5.0000e-01|          0.5|
|  0x1.0000p+0|       1.0000|   1.0000e+00|            1|
| -0x1.0000p+0|      -1.0000|  -1.0000e+00|           -1|
|  0x1.9000p+6|     100.0000|   1.0000e+02|          100|
|  0x1.f400p+9|    1000.0000|   1.0000e+03|         1000|
| 0x1.3880p+13|   10000.0000|   1.0000e+04|        1e+04|
| 0x1.81c8p+13|   12345.0000|   1.2345e+04|    1.234e+04|
| 0x1.86a0p+16|  100000.0000|   1.0000e+05|        1e+05|
| 0x1.e240p+16|  123456.0000|   1.2346e+05|    1.235e+05|

Notice how the `%g' conversion drops trailing zeros.

Other Output Conversions

This section describes miscellaneous conversions for printf.

The `%c' conversion prints a single character. In case there is no `l' modifier the int argument is first converted to an unsigned char. Then, if used in a wide stream function, the character is converted into the corresponding wide character. The `-' flag can be used to specify left-justification in the field, but no other flags are defined, and no precision or type modifier can be given. For example:

printf ("%c%c%c%c%c", 'h', 'e', 'l', 'l', 'o');

prints `hello'.

If there is a `l' modifier present the argument is expected to be of type wint_t. If used in a multibyte function the wide character is converted into a multibyte character before being added to the output. In this case more than one output byte can be produced.

The `%s' conversion prints a string. If no `l' modifier is present the corresponding argument must be of type char * (or const char *). If used in a wide stream function the string is first converted in a wide character string. A precision can be specified to indicate the maximum number of characters to write; otherwise characters in the string up to but not including the terminating null character are written to the output stream. The `-' flag can be used to specify left-justification in the field, but no other flags or type modifiers are defined for this conversion. For example:

printf ("%3s%-6s", "no", "where");

prints ` nowhere '.

If there is a `l' modifier present the argument is expected to be of type wchar_t (or const wchar_t *).

If you accidentally pass a null pointer as the argument for a `%s' conversion, the GNU library prints it as `(null)'. We think this is more useful than crashing. But it's not good practice to pass a null argument intentionally.

The `%m' conversion prints the string corresponding to the error code in errno. See section Error Messages. Thus:

fprintf (stderr, "can't open `%s': %m\n", filename);

is equivalent to:

fprintf (stderr, "can't open `%s': %s\n", filename, strerror (errno));

The `%m' conversion is a GNU C library extension.

The `%p' conversion prints a pointer value. The corresponding argument must be of type void *. In practice, you can use any type of pointer.

In the GNU system, non-null pointers are printed as unsigned integers, as if a `%#x' conversion were used. Null pointers print as `(nil)'. (Pointers might print differently in other systems.)

For example:

printf ("%p", "testing");

prints `0x' followed by a hexadecimal number--the address of the string constant "testing". It does not print the word `testing'.

You can supply the `-' flag with the `%p' conversion to specify left-justification, but no other flags, precision, or type modifiers are defined.

The `%n' conversion is unlike any of the other output conversions. It uses an argument which must be a pointer to an int, but instead of printing anything it stores the number of characters printed so far by this call at that location. The `h' and `l' type modifiers are permitted to specify that the argument is of type short int * or long int * instead of int *, but no flags, field width, or precision are permitted.

For example,

int nchar;
printf ("%d %s%n\n", 3, "bears", &nchar);

prints:

3 bears

and sets nchar to 7, because `3 bears' is seven characters.

The `%%' conversion prints a literal `%' character. This conversion doesn't use an argument, and no flags, field width, precision, or type modifiers are permitted.

Formatted Output Functions

This section describes how to call printf and related functions. Prototypes for these functions are in the header file `stdio.h'. Because these functions take a variable number of arguments, you must declare prototypes for them before using them. Of course, the easiest way to make sure you have all the right prototypes is to just include `stdio.h'.

Function: int printf (const char *template, ...)
The printf function prints the optional arguments under the control of the template string template to the stream stdout. It returns the number of characters printed, or a negative value if there was an output error.

Function: int wprintf (const wchar_t *template, ...)
The wprintf function prints the optional arguments under the control of the wide template string template to the stream stdout. It returns the number of wide characters printed, or a negative value if there was an output error.

Function: int fprintf (FILE *stream, const char *template, ...)
This function is just like printf, except that the output is written to the stream stream instead of stdout.

Function: int fwprintf (FILE *stream, const wchar_t *template, ...)
This function is just like wprintf, except that the output is written to the stream stream instead of stdout.

Function: int sprintf (char *s, const char *template, ...)
This is like printf, except that the output is stored in the character array s instead of written to a stream. A null character is written to mark the end of the string.

The sprintf function returns the number of characters stored in the array s, not including the terminating null character.

The behavior of this function is undefined if copying takes place between objects that overlap--for example, if s is also given as an argument to be printed under control of the `%s' conversion. See section Copying and Concatenation.

Warning: The sprintf function can be dangerous because it can potentially output more characters than can fit in the allocation size of the string s. Remember that the field width given in a conversion specification is only a minimum value.

To avoid this problem, you can use snprintf or asprintf, described below.

Function: int swprintf (wchar_t *s, size_t size, const wchar_t *template, ...)
This is like wprintf, except that the output is stored in the wide character array ws instead of written to a stream. A null wide character is written to mark the end of the string. The size argument specifies the maximum number of characters to produce. The trailing null character is counted towards this limit, so you should allocate at least size wide characters for the string ws.

The return value is the number of characters which would be generated for the given input, excluding the trailing null. If this value is greater or equal to size, not all characters from the result have been stored in ws. You should try again with a bigger output string.

Note that the corresponding narrow stream function takes fewer parameters. swprintf in fact corresponds to the snprintf function. Since the sprintf function can be dangerous and should be avoided the ISO C committee refused to make the same mistake again and decided to not define an function exactly corresponding to sprintf.

Function: int snprintf (char *s, size_t size, const char *template, ...)
The snprintf function is similar to sprintf, except that the size argument specifies the maximum number of characters to produce. The trailing null character is counted towards this limit, so you should allocate at least size characters for the string s.

The return value is the number of characters which would be generated for the given input, excluding the trailing null. If this value is greater or equal to size, not all characters from the result have been stored in s. You should try again with a bigger output string. Here is an example of doing this:

/* Construct a message describing the value of a variable
   whose name is name and whose value is value. */
char *
make_message (char *name, char *value)
{
  /* Guess we need no more than 100 chars of space. */
  int size = 100;
  char *buffer = (char *) xmalloc (size);
  int nchars;
  if (buffer == NULL)
    return NULL;

 /* Try to print in the allocated space. */
  nchars = snprintf (buffer, size, "value of %s is %s",
                     name, value);
  if (nchars >= size)
    {
      /* Reallocate buffer now that we know
         how much space is needed. */
      buffer = (char *) xrealloc (buffer, nchars + 1);

      if (buffer != NULL)
        /* Try again. */
        snprintf (buffer, size, "value of %s is %s",
                  name, value);
    }
  /* The last call worked, return the string. */
  return buffer;
}

In practice, it is often easier just to use asprintf, below.

Attention: In versions of the GNU C library prior to 2.1 the return value is the number of characters stored, not including the terminating null; unless there was not enough space in s to store the result in which case -1 is returned. This was changed in order to comply with the ISO C99 standard.

Dynamically Allocating Formatted Output

The functions in this section do formatted output and place the results in dynamically allocated memory.

Function: int asprintf (char **ptr, const char *template, ...)
This function is similar to sprintf, except that it dynamically allocates a string (as with malloc; see section Unconstrained Allocation) to hold the output, instead of putting the output in a buffer you allocate in advance. The ptr argument should be the address of a char * object, and asprintf stores a pointer to the newly allocated string at that location.

The return value is the number of characters allocated for the buffer, or less than zero if an error occured. Usually this means that the buffer could not be allocated.

Here is how to use asprintf to get the same result as the snprintf example, but more easily:

/* Construct a message describing the value of a variable
   whose name is name and whose value is value. */
char *
make_message (char *name, char *value)
{
  char *result;
  if (asprintf (&result, "value of %s is %s", name, value) < 0)
    return NULL;
  return result;
}

Function: int obstack_printf (struct obstack *obstack, const char *template, ...)
This function is similar to asprintf, except that it uses the obstack obstack to allocate the space. See section Obstacks.

The characters are written onto the end of the current object. To get at them, you must finish the object with obstack_finish (see section Growing Objects).

Variable Arguments Output Functions

The functions vprintf and friends are provided so that you can define your own variadic printf-like functions that make use of the same internals as the built-in formatted output functions.

The most natural way to define such functions would be to use a language construct to say, "Call printf and pass this template plus all of my arguments after the first five." But there is no way to do this in C, and it would be hard to provide a way, since at the C language level there is no way to tell how many arguments your function received.

Since that method is impossible, we provide alternative functions, the vprintf series, which lets you pass a va_list to describe "all of my arguments after the first five."

When it is sufficient to define a macro rather than a real function, the GNU C compiler provides a way to do this much more easily with macros. For example:

#define myprintf(a, b, c, d, e, rest...) \
            printf (mytemplate , ## rest...)

See section `Macros with Variable Numbers of Arguments' in Using GNU CC, for details. But this is limited to macros, and does not apply to real functions at all.

Before calling vprintf or the other functions listed in this section, you must call va_start (see section Variadic Functions) to initialize a pointer to the variable arguments. Then you can call va_arg to fetch the arguments that you want to handle yourself. This advances the pointer past those arguments.

Once your va_list pointer is pointing at the argument of your choice, you are ready to call vprintf. That argument and all subsequent arguments that were passed to your function are used by vprintf along with the template that you specified separately.

In some other systems, the va_list pointer may become invalid after the call to vprintf, so you must not use va_arg after you call vprintf. Instead, you should call va_end to retire the pointer from service. However, you can safely call va_start on another pointer variable and begin fetching the arguments again through that pointer. Calling vprintf does not destroy the argument list of your function, merely the particular pointer that you passed to it.

GNU C does not have such restrictions. You can safely continue to fetch arguments from a va_list pointer after passing it to vprintf, and va_end is a no-op. (Note, however, that subsequent va_arg calls will fetch the same arguments which vprintf previously used.)

Prototypes for these functions are declared in `stdio.h'.

Function: int vprintf (const char *template, va_list ap)
This function is similar to printf except that, instead of taking a variable number of arguments directly, it takes an argument list pointer ap.

Function: int vwprintf (const wchar_t *template, va_list ap)
This function is similar to wprintf except that, instead of taking a variable number of arguments directly, it takes an argument list pointer ap.

Function: int vfprintf (FILE *stream, const char *template, va_list ap)
This is the equivalent of fprintf with the variable argument list specified directly as for vprintf.

Function: int vfwprintf (FILE *stream, const wchar_t *template, va_list ap)
This is the equivalent of fwprintf with the variable argument list specified directly as for vwprintf.

Function: int vsprintf (char *s, const char *template, va_list ap)
This is the equivalent of sprintf with the variable argument list specified directly as for vprintf.

Function: int vswprintf (wchar_t *s, size_t size, const wchar_t *template, va_list ap)
This is the equivalent of swprintf with the variable argument list specified directly as for vwprintf.

Function: int vsnprintf (char *s, size_t size, const char *template, va_list ap)
This is the equivalent of snprintf with the variable argument list specified directly as for vprintf.

Function: int vasprintf (char **ptr, const char *template, va_list ap)
The vasprintf function is the equivalent of asprintf with the variable argument list specified directly as for vprintf.

Function: int obstack_vprintf (struct obstack *obstack, const char *template, va_list ap)
The obstack_vprintf function is the equivalent of obstack_printf with the variable argument list specified directly as for vprintf.

Here's an example showing how you might use vfprintf. This is a function that prints error messages to the stream stderr, along with a prefix indicating the name of the program (see section Error Messages, for a description of program_invocation_short_name).

#include <stdio.h>
#include <stdarg.h>

void
eprintf (const char *template, ...)
{
  va_list ap;
  extern char *program_invocation_short_name;

  fprintf (stderr, "%s: ", program_invocation_short_name);
  va_start (ap, template);
  vfprintf (stderr, template, ap);
  va_end (ap);
}

You could call eprintf like this:

eprintf ("file `%s' does not exist\n", filename);

In GNU C, there is a special construct you can use to let the compiler know that a function uses a printf-style format string. Then it can check the number and types of arguments in each call to the function, and warn you when they do not match the format string. For example, take this declaration of eprintf:

void eprintf (const char *template, ...)
        __attribute__ ((format (printf, 1, 2)));

This tells the compiler that eprintf uses a format string like printf (as opposed to scanf; see section Formatted Input); the format string appears as the first argument; and the arguments to satisfy the format begin with the second. See section `Declaring Attributes of Functions' in Using GNU CC, for more information.

Parsing a Template String

You can use the function parse_printf_format to obtain information about the number and types of arguments that are expected by a given template string. This function permits interpreters that provide interfaces to printf to avoid passing along invalid arguments from the user's program, which could cause a crash.

All the symbols described in this section are declared in the header file `printf.h'.

Function: size_t parse_printf_format (const char *template, size_t n, int *argtypes)
This function returns information about the number and types of arguments expected by the printf template string template. The information is stored in the array argtypes; each element of this array describes one argument. This information is encoded using the various `PA_' macros, listed below.

The argument n specifies the number of elements in the array argtypes. This is the maximum number of elements that parse_printf_format will try to write.

parse_printf_format returns the total number of arguments required by template. If this number is greater than n, then the information returned describes only the first n arguments. If you want information about additional arguments, allocate a bigger array and call parse_printf_format again.

The argument types are encoded as a combination of a basic type and modifier flag bits.

Macro: int PA_FLAG_MASK
This macro is a bitmask for the type modifier flag bits. You can write the expression (argtypes[i] & PA_FLAG_MASK) to extract just the flag bits for an argument, or (argtypes[i] & ~PA_FLAG_MASK) to extract just the basic type code.

Here are symbolic constants that represent the basic types; they stand for integer values.

PA_INT
This specifies that the base type is int.
PA_CHAR
This specifies that the base type is int, cast to char.
PA_STRING
This specifies that the base type is char *, a null-terminated string.
PA_POINTER
This specifies that the base type is void *, an arbitrary pointer.
PA_FLOAT
This specifies that the base type is float.
PA_DOUBLE
This specifies that the base type is double.
PA_LAST
You can define additional base types for your own programs as offsets from PA_LAST. For example, if you have data types `foo' and `bar' with their own specialized printf conversions, you could define encodings for these types as:
#define PA_FOO  PA_LAST
#define PA_BAR  (PA_LAST + 1)

Here are the flag bits that modify a basic type. They are combined with the code for the basic type using inclusive-or.

PA_FLAG_PTR
If this bit is set, it indicates that the encoded type is a pointer to the base type, rather than an immediate value. For example, `PA_INT|PA_FLAG_PTR' represents the type `int *'.
PA_FLAG_SHORT
If this bit is set, it indicates that the base type is modified with short. (This corresponds to the `h' type modifier.)
PA_FLAG_LONG
If this bit is set, it indicates that the base type is modified with long. (This corresponds to the `l' type modifier.)
PA_FLAG_LONG_LONG
If this bit is set, it indicates that the base type is modified with long long. (This corresponds to the `L' type modifier.)
PA_FLAG_LONG_DOUBLE
This is a synonym for PA_FLAG_LONG_LONG, used by convention with a base type of PA_DOUBLE to indicate a type of long double.

Example of Parsing a Template String

Here is an example of decoding argument types for a format string. We assume this is part of an interpreter which contains arguments of type NUMBER, CHAR, STRING and STRUCTURE (and perhaps others which are not valid here).

/* Test whether the nargs specified objects
   in the vector args are valid
   for the format string format:
   if so, return 1.
   If not, return 0 after printing an error message.  */

int
validate_args (char *format, int nargs, OBJECT *args)
{
  int *argtypes;
  int nwanted;

  /* Get the information about the arguments.
     Each conversion specification must be at least two characters
     long, so there cannot be more specifications than half the
     length of the string.  */

  argtypes = (int *) alloca (strlen (format) / 2 * sizeof (int));
  nwanted = parse_printf_format (string, nelts, argtypes);

  /* Check the number of arguments.  */
  if (nwanted > nargs)
    {
      error ("too few arguments (at least %d required)", nwanted);
      return 0;
    }

  /* Check the C type wanted for each argument
     and see if the object given is suitable.  */
  for (i = 0; i < nwanted; i++)
    {
      int wanted;

      if (argtypes[i] & PA_FLAG_PTR)
        wanted = STRUCTURE;
      else
        switch (argtypes[i] & ~PA_FLAG_MASK)
          {
          case PA_INT:
          case PA_FLOAT:
          case PA_DOUBLE:
            wanted = NUMBER;
            break;
          case PA_CHAR:
            wanted = CHAR;
            break;
          case PA_STRING:
            wanted = STRING;
            break;
          case PA_POINTER:
            wanted = STRUCTURE;
            break;
          }
      if (TYPE (args[i]) != wanted)
        {
          error ("type mismatch for arg number %d", i);
          return 0;
        }
    }
  return 1;
}

Customizing printf

The GNU C library lets you define your own custom conversion specifiers for printf template strings, to teach printf clever ways to print the important data structures of your program.

The way you do this is by registering the conversion with the function register_printf_function; see section Registering New Conversions. One of the arguments you pass to this function is a pointer to a handler function that produces the actual output; see section Defining the Output Handler, for information on how to write this function.

You can also install a function that just returns information about the number and type of arguments expected by the conversion specifier. See section Parsing a Template String, for information about this.

The facilities of this section are declared in the header file `printf.h'.

Portability Note: The ability to extend the syntax of printf template strings is a GNU extension. ISO standard C has nothing similar.

Registering New Conversions

The function to register a new output conversion is register_printf_function, declared in `printf.h'.

Function: int register_printf_function (int spec, printf_function handler-function, printf_arginfo_function arginfo-function)
This function defines the conversion specifier character spec. Thus, if spec is 'Y', it defines the conversion `%Y'. You can redefine the built-in conversions like `%s', but flag characters like `#' and type modifiers like `l' can never be used as conversions; calling register_printf_function for those characters has no effect. It is advisable not to use lowercase letters, since the ISO C standard warns that additional lowercase letters may be standardized in future editions of the standard.

The handler-function is the function called by printf and friends when this conversion appears in a template string. See section Defining the Output Handler, for information about how to define a function to pass as this argument. If you specify a null pointer, any existing handler function for spec is removed.

The arginfo-function is the function called by parse_printf_format when this conversion appears in a template string. See section Parsing a Template String, for information about this.

Attention: In the GNU C library versions before 2.0 the arginfo-function function did not need to be installed unless the user used the parse_printf_format function. This has changed. Now a call to any of the printf functions will call this function when this format specifier appears in the format string.

The return value is 0 on success, and -1 on failure (which occurs if spec is out of range).

You can redefine the standard output conversions, but this is probably not a good idea because of the potential for confusion. Library routines written by other people could break if you do this.

Conversion Specifier Options

If you define a meaning for `%A', what if the template contains `%+23A' or `%-#A'? To implement a sensible meaning for these, the handler when called needs to be able to get the options specified in the template.

Both the handler-function and arginfo-function accept an argument that points to a struct printf_info, which contains information about the options appearing in an instance of the conversion specifier. This data type is declared in the header file `printf.h'.

Type: struct printf_info
This structure is used to pass information about the options appearing in an instance of a conversion specifier in a printf template string to the handler and arginfo functions for that specifier. It contains the following members:

int prec
This is the precision specified. The value is -1 if no precision was specified. If the precision was given as `*', the printf_info structure passed to the handler function contains the actual value retrieved from the argument list. But the structure passed to the arginfo function contains a value of INT_MIN, since the actual value is not known.
int width
This is the minimum field width specified. The value is 0 if no width was specified. If the field width was given as `*', the printf_info structure passed to the handler function contains the actual value retrieved from the argument list. But the structure passed to the arginfo function contains a value of INT_MIN, since the actual value is not known.
wchar_t spec
This is the conversion specifier character specified. It's stored in the structure so that you can register the same handler function for multiple characters, but still have a way to tell them apart when the handler function is called.
unsigned int is_long_double
This is a boolean that is true if the `L', `ll', or `q' type modifier was specified. For integer conversions, this indicates long long int, as opposed to long double for floating point conversions.
unsigned int is_char
This is a boolean that is true if the `hh' type modifier was specified.
unsigned int is_short
This is a boolean that is true if the `h' type modifier was specified.
unsigned int is_long
This is a boolean that is true if the `l' type modifier was specified.
unsigned int alt
This is a boolean that is true if the `#' flag was specified.
unsigned int space
This is a boolean that is true if the ` ' flag was specified.
unsigned int left
This is a boolean that is true if the `-' flag was specified.
unsigned int showsign
This is a boolean that is true if the `+' flag was specified.
unsigned int group
This is a boolean that is true if the `'' flag was specified.
unsigned int extra
This flag has a special meaning depending on the context. It could be used freely by the user-defined handlers but when called from the printf function this variable always contains the value 0.
unsigned int wide
This flag is set if the stream is wide oriented.
wchar_t pad
This is the character to use for padding the output to the minimum field width. The value is '0' if the `0' flag was specified, and ' ' otherwise.

Defining the Output Handler

Now let's look at how to define the handler and arginfo functions which are passed as arguments to register_printf_function.

Compatibility Note: The interface changed in GNU libc version 2.0. Previously the third argument was of type va_list *.

You should define your handler functions with a prototype like:

int function (FILE *stream, const struct printf_info *info,
                    const void *const *args)

The stream argument passed to the handler function is the stream to which it should write output.

The info argument is a pointer to a structure that contains information about the various options that were included with the conversion in the template string. You should not modify this structure inside your handler function. See section Conversion Specifier Options, for a description of this data structure.

The args is a vector of pointers to the arguments data. The number of arguments was determined by calling the argument information function provided by the user.

Your handler function should return a value just like printf does: it should return the number of characters it has written, or a negative value to indicate an error.

Data Type: printf_function
This is the data type that a handler function should have.

If you are going to use parse_printf_format in your application, you must also define a function to pass as the arginfo-function argument for each new conversion you install with register_printf_function.

You have to define these functions with a prototype like:

int function (const struct printf_info *info,
                    size_t n, int *argtypes)

The return value from the function should be the number of arguments the conversion expects. The function should also fill in no more than n elements of the argtypes array with information about the types of each of these arguments. This information is encoded using the various `PA_' macros. (You will notice that this is the same calling convention parse_printf_format itself uses.)

Data Type: printf_arginfo_function
This type is used to describe functions that return information about the number and type of arguments used by a conversion specifier.

printf Extension Example

Here is an example showing how to define a printf handler function. This program defines a data structure called a Widget and defines the `%W' conversion to print information about Widget * arguments, including the pointer value and the name stored in the data structure. The `%W' conversion supports the minimum field width and left-justification options, but ignores everything else.

#include <stdio.h>
#include <stdlib.h>
#include <printf.h>

typedef struct
{
  char *name;
}
Widget;

int
print_widget (FILE *stream,
              const struct printf_info *info,
              const void *const *args)
{
  const Widget *w;
  char *buffer;
  int len;

  /* Format the output into a string. */
  w = *((const Widget **) (args[0]));
  len = asprintf (&buffer, "<Widget %p: %s>", w, w->name);
  if (len == -1)
    return -1;

  /* Pad to the minimum field width and print to the stream. */
  len = fprintf (stream, "%*s",
                 (info->left ? -info->width : info->width),
                 buffer);

  /* Clean up and return. */
  free (buffer);
  return len;
}

int
print_widget_arginfo (const struct printf_info *info, size_t n,
                      int *argtypes)
{
  /* We always take exactly one argument and this is a pointer to the
     structure.. */
  if (n > 0)
    argtypes[0] = PA_POINTER;
  return 1;
}

int
main (void)
{
  /* Make a widget to print. */
  Widget mywidget;
  mywidget.name = "mywidget";

  /* Register the print function for widgets. */
  register_printf_function ('W', print_widget, print_widget_arginfo);

  /* Now print the widget. */
  printf ("|%W|\n", &mywidget);
  printf ("|%35W|\n", &mywidget);
  printf ("|%-35W|\n", &mywidget);

  return 0;
}

The output produced by this program looks like:

|<Widget 0xffeffb7c: mywidget>|
|      <Widget 0xffeffb7c: mywidget>|
|<Widget 0xffeffb7c: mywidget>      |

Predefined printf Handlers

The GNU libc also contains a concrete and useful application of the printf handler extension. There are two functions available which implement a special way to print floating-point numbers.

Function: int printf_size (FILE *fp, const struct printf_info *info, const void *const *args)
Print a given floating point number as for the format %f except that there is a postfix character indicating the divisor for the number to make this less than 1000. There are two possible divisors: powers of 1024 or powers of 1000. Which one is used depends on the format character specified while registered this handler. If the character is of lower case, 1024 is used. For upper case characters, 1000 is used.

The postfix tag corresponds to bytes, kilobytes, megabytes, gigabytes, etc. The full table is:

The default precision is 3, i.e., 1024 is printed with a lower-case format character as if it were %.3fk and will yield 1.000k.

Due to the requirements of register_printf_function we must also provide the function which returns information about the arguments.

Function: int printf_size_info (const struct printf_info *info, size_t n, int *argtypes)
This function will return in argtypes the information about the used parameters in the way the vfprintf implementation expects it. The format always takes one argument.

To use these functions both functions must be registered with a call like

register_printf_function ('B', printf_size, printf_size_info);

Here we register the functions to print numbers as powers of 1000 since the format character 'B' is an upper-case character. If we would additionally use 'b' in a line like

register_printf_function ('b', printf_size, printf_size_info);

we could also print using a power of 1024. Please note that all that is different in these two lines is the format specifier. The printf_size function knows about the difference between lower and upper case format specifiers.

The use of 'B' and 'b' is no coincidence. Rather it is the preferred way to use this functionality since it is available on some other systems which also use format specifiers.

Formatted Input

The functions described in this section (scanf and related functions) provide facilities for formatted input analogous to the formatted output facilities. These functions provide a mechanism for reading arbitrary values under the control of a format string or template string.

Formatted Input Basics

Calls to scanf are superficially similar to calls to printf in that arbitrary arguments are read under the control of a template string. While the syntax of the conversion specifications in the template is very similar to that for printf, the interpretation of the template is oriented more towards free-format input and simple pattern matching, rather than fixed-field formatting. For example, most scanf conversions skip over any amount of "white space" (including spaces, tabs, and newlines) in the input file, and there is no concept of precision for the numeric input conversions as there is for the corresponding output conversions. Ordinarily, non-whitespace characters in the template are expected to match characters in the input stream exactly, but a matching failure is distinct from an input error on the stream.

Another area of difference between scanf and printf is that you must remember to supply pointers rather than immediate values as the optional arguments to scanf; the values that are read are stored in the objects that the pointers point to. Even experienced programmers tend to forget this occasionally, so if your program is getting strange errors that seem to be related to scanf, you might want to double-check this.

When a matching failure occurs, scanf returns immediately, leaving the first non-matching character as the next character to be read from the stream. The normal return value from scanf is the number of values that were assigned, so you can use this to determine if a matching error happened before all the expected values were read.

The scanf function is typically used for things like reading in the contents of tables. For example, here is a function that uses scanf to initialize an array of double:

void
readarray (double *array, int n)
{
  int i;
  for (i=0; i<n; i++)
    if (scanf (" %lf", &(array[i])) != 1)
      invalid_input_error ();
}

The formatted input functions are not used as frequently as the formatted output functions. Partly, this is because it takes some care to use them properly. Another reason is that it is difficult to recover from a matching error.

If you are trying to read input that doesn't match a single, fixed pattern, you may be better off using a tool such as Flex to generate a lexical scanner, or Bison to generate a parser, rather than using scanf. For more information about these tools, see section `' in Flex: The Lexical Scanner Generator, and section `' in The Bison Reference Manual.

Input Conversion Syntax

A scanf template string is a string that contains ordinary multibyte characters interspersed with conversion specifications that start with `%'.

Any whitespace character (as defined by the isspace function; see section Classification of Characters) in the template causes any number of whitespace characters in the input stream to be read and discarded. The whitespace characters that are matched need not be exactly the same whitespace characters that appear in the template string. For example, write ` , ' in the template to recognize a comma with optional whitespace before and after.

Other characters in the template string that are not part of conversion specifications must match characters in the input stream exactly; if this is not the case, a matching failure occurs.

The conversion specifications in a scanf template string have the general form:

% flags width type conversion

In more detail, an input conversion specification consists of an initial `%' character followed in sequence by:

The exact options that are permitted and how they are interpreted vary between the different conversion specifiers. See the descriptions of the individual conversions for information about the particular options that they allow.

With the `-Wformat' option, the GNU C compiler checks calls to scanf and related functions. It examines the format string and verifies that the correct number and types of arguments are supplied. There is also a GNU C syntax to tell the compiler that a function you write uses a scanf-style format string. See section `Declaring Attributes of Functions' in Using GNU CC, for more information.

Table of Input Conversions

Here is a table that summarizes the various conversion specifications:

`%d'
Matches an optionally signed integer written in decimal. See section Numeric Input Conversions.
`%i'
Matches an optionally signed integer in any of the formats that the C language defines for specifying an integer constant. See section Numeric Input Conversions.
`%o'
Matches an unsigned integer written in octal radix. See section Numeric Input Conversions.
`%u'
Matches an unsigned integer written in decimal radix. See section Numeric Input Conversions.
`%x', `%X'
Matches an unsigned integer written in hexadecimal radix. See section Numeric Input Conversions.
`%e', `%f', `%g', `%E', `%G'
Matches an optionally signed floating-point number. See section Numeric Input Conversions.
`%s'
Matches a string containing only non-whitespace characters. See section String Input Conversions. The presence of the `l' modifier determines whether the output is stored as a wide character string or a multibyte string. If `%s' is used in a wide character function the string is converted as with multiple calls to wcrtomb into a multibyte string. This means that the buffer must provide room for MB_CUR_MAX bytes for each wide character read. In case `%ls' is used in a multibyte function the result is converted into wide characters as with multiple calls of mbrtowc before being stored in the user provided buffer.
`%S'
This is an alias for `%ls' which is supported for compatibility with the Unix standard.
`%['
Matches a string of characters that belong to a specified set. See section String Input Conversions. The presence of the `l' modifier determines whether the output is stored as a wide character string or a multibyte string. If `%[' is used in a wide character function the string is converted as with multiple calls to wcrtomb into a multibyte string. This means that the buffer must provide room for MB_CUR_MAX bytes for each wide character read. In case `%l[' is used in a multibyte function the result is converted into wide characters as with multiple calls of mbrtowc before being stored in the user provided buffer.
`%c'
Matches a string of one or more characters; the number of characters read is controlled by the maximum field width given for the conversion. See section String Input Conversions. If the `%c' is used in a wide stream function the read value is converted from a wide character to the corresponding multibyte character before storing it. Note that this conversion can produce more than one byte of output and therefore the provided buffer be large enough for up to MB_CUR_MAX bytes for each character. If `%lc' is used in a multibyte function the input is treated as a multibyte sequence (and not bytes) and the result is converted as with calls to mbrtowc.
`%C'
This is an alias for `%lc' which is supported for compatibility with the Unix standard.
`%p'
Matches a pointer value in the same implementation-defined format used by the `%p' output conversion for printf. See section Other Input Conversions.
`%n'
This conversion doesn't read any characters; it records the number of characters read so far by this call. See section Other Input Conversions.
`%%'
This matches a literal `%' character in the input stream. No corresponding argument is used. See section Other Input Conversions.

If the syntax of a conversion specification is invalid, the behavior is undefined. If there aren't enough function arguments provided to supply addresses for all the conversion specifications in the template strings that perform assignments, or if the arguments are not of the correct types, the behavior is also undefined. On the other hand, extra arguments are simply ignored.

Numeric Input Conversions

This section describes the scanf conversions for reading numeric values.

The `%d' conversion matches an optionally signed integer in decimal radix. The syntax that is recognized is the same as that for the strtol function (see section Parsing of Integers) with the value 10 for the base argument.

The `%i' conversion matches an optionally signed integer in any of the formats that the C language defines for specifying an integer constant. The syntax that is recognized is the same as that for the strtol function (see section Parsing of Integers) with the value 0 for the base argument. (You can print integers in this syntax with printf by using the `#' flag character with the `%x', `%o', or `%d' conversion. See section Integer Conversions.)

For example, any of the strings `10', `0xa', or `012' could be read in as integers under the `%i' conversion. Each of these specifies a number with decimal value 10.

The `%o', `%u', and `%x' conversions match unsigned integers in octal, decimal, and hexadecimal radices, respectively. The syntax that is recognized is the same as that for the strtoul function (see section Parsing of Integers) with the appropriate value (8, 10, or 16) for the base argument.

The `%X' conversion is identical to the `%x' conversion. They both permit either uppercase or lowercase letters to be used as digits.

The default type of the corresponding argument for the %d and %i conversions is int *, and unsigned int * for the other integer conversions. You can use the following type modifiers to specify other sizes of integer:

`hh'
Specifies that the argument is a signed char * or unsigned char *. This modifier was introduced in ISO C99.
`h'
Specifies that the argument is a short int * or unsigned short int *.
`j'
Specifies that the argument is a intmax_t * or uintmax_t *. This modifier was introduced in ISO C99.
`l'
Specifies that the argument is a long int * or unsigned long int *. Two `l' characters is like the `L' modifier, below. If used with `%c' or `%s' the corresponding parameter is considered as a pointer to a wide character or wide character string respectively. This use of `l' was introduced in Amendment 1 to ISO C90.
`ll'
`L'
`q'
Specifies that the argument is a long long int * or unsigned long long int *. (The long long type is an extension supported by the GNU C compiler. For systems that don't provide extra-long integers, this is the same as long int.) The `q' modifier is another name for the same thing, which comes from 4.4 BSD; a long long int is sometimes called a "quad" int.
`t'
Specifies that the argument is a ptrdiff_t *. This modifier was introduced in ISO C99.
`z'
Specifies that the argument is a size_t *. This modifier was introduced in ISO C99.

All of the `%e', `%f', `%g', `%E', and `%G' input conversions are interchangeable. They all match an optionally signed floating point number, in the same syntax as for the strtod function (see section Parsing of Floats).

For the floating-point input conversions, the default argument type is float *. (This is different from the corresponding output conversions, where the default type is double; remember that float arguments to printf are converted to double by the default argument promotions, but float * arguments are not promoted to double *.) You can specify other sizes of float using these type modifiers:

`l'
Specifies that the argument is of type double *.
`L'
Specifies that the argument is of type long double *.

For all the above number parsing formats there is an additional optional flag `''. When this flag is given the scanf function expects the number represented in the input string to be formatted according to the grouping rules of the currently selected locale (see section Generic Numeric Formatting Parameters).

If the "C" or "POSIX" locale is selected there is no difference. But for a locale which specifies values for the appropriate fields in the locale the input must have the correct form in the input. Otherwise the longest prefix with a correct form is processed.

String Input Conversions

This section describes the scanf input conversions for reading string and character values: `%s', `%S', `%[', `%c', and `%C'.

You have two options for how to receive the input from these conversions:

The `%c' conversion is the simplest: it matches a fixed number of characters, always. The maximum field width says how many characters to read; if you don't specify the maximum, the default is 1. This conversion doesn't append a null character to the end of the text it reads. It also does not skip over initial whitespace characters. It reads precisely the next n characters, and fails if it cannot get that many. Since there is always a maximum field width with `%c' (whether specified, or 1 by default), you can always prevent overflow by making the buffer long enough.

If the format is `%lc' or `%C' the function stores wide characters which are converted using the conversion determined at the time the stream was opened from the external byte stream. The number of bytes read from the medium is limited by MB_CUR_LEN * n but at most n wide character get stored in the output string.

The `%s' conversion matches a string of non-whitespace characters. It skips and discards initial whitespace, but stops when it encounters more whitespace after having read something. It stores a null character at the end of the text that it reads.

For example, reading the input:

 hello, world

with the conversion `%10c' produces " hello, wo", but reading the same input with the conversion `%10s' produces "hello,".

Warning: If you do not specify a field width for `%s', then the number of characters read is limited only by where the next whitespace character appears. This almost certainly means that invalid input can make your program crash--which is a bug.

The `%ls' and `%S' format are handled just like `%s' except that the external byte sequence is converted using the conversion associated with the stream to wide characters with their own encoding. A width or precision specified with the format do not directly determine how many bytes are read from the stream since they measure wide characters. But an upper limit can be computed by multiplying the value of the width or precision by MB_CUR_MAX.

To read in characters that belong to an arbitrary set of your choice, use the `%[' conversion. You specify the set between the `[' character and a following `]' character, using the same syntax used in regular expressions. As special cases:

The `%[' conversion does not skip over initial whitespace characters.

Here are some examples of `%[' conversions and what they mean:

`%25[1234567890]'
Matches a string of up to 25 digits.
`%25[][]'
Matches a string of up to 25 square brackets.
`%25[^ \f\n\r\t\v]'
Matches a string up to 25 characters long that doesn't contain any of the standard whitespace characters. This is slightly different from `%s', because if the input begins with a whitespace character, `%[' reports a matching failure while `%s' simply discards the initial whitespace.
`%25[a-z]'
Matches up to 25 lowercase characters.

As for `%c' and `%s' the `%[' format is also modified to produce wide characters if the `l' modifier is present. All what is said about `%ls' above is true for `%l['.

One more reminder: the `%s' and `%[' conversions are dangerous if you don't specify a maximum width or use the `a' flag, because input too long would overflow whatever buffer you have provided for it. No matter how long your buffer is, a user could supply input that is longer. A well-written program reports invalid input with a comprehensible error message, not with a crash.

Dynamically Allocating String Conversions

A GNU extension to formatted input lets you safely read a string with no maximum size. Using this feature, you don't supply a buffer; instead, scanf allocates a buffer big enough to hold the data and gives you its address. To use this feature, write `a' as a flag character, as in `%as' or `%a[0-9a-z]'.

The pointer argument you supply for where to store the input should have type char **. The scanf function allocates a buffer and stores its address in the word that the argument points to. You should free the buffer with free when you no longer need it.

Here is an example of using the `a' flag with the `%[...]' conversion specification to read a "variable assignment" of the form `variable = value'.

{
  char *variable, *value;

  if (2 > scanf ("%a[a-zA-Z0-9] = %a[^\n]\n",
                 &variable, &value))
    {
      invalid_input_error ();
      return 0;
    }

  ...
}

Other Input Conversions

This section describes the miscellaneous input conversions.

The `%p' conversion is used to read a pointer value. It recognizes the same syntax used by the `%p' output conversion for printf (see section Other Output Conversions); that is, a hexadecimal number just as the `%x' conversion accepts. The corresponding argument should be of type void **; that is, the address of a place to store a pointer.

The resulting pointer value is not guaranteed to be valid if it was not originally written during the same program execution that reads it in.

The `%n' conversion produces the number of characters read so far by this call. The corresponding argument should be of type int *. This conversion works in the same way as the `%n' conversion for printf; see section Other Output Conversions, for an example.

The `%n' conversion is the only mechanism for determining the success of literal matches or conversions with suppressed assignments. If the `%n' follows the locus of a matching failure, then no value is stored for it since scanf returns before processing the `%n'. If you store -1 in that argument slot before calling scanf, the presence of -1 after scanf indicates an error occurred before the `%n' was reached.

Finally, the `%%' conversion matches a literal `%' character in the input stream, without using an argument. This conversion does not permit any flags, field width, or type modifier to be specified.

Formatted Input Functions

Here are the descriptions of the functions for performing formatted input. Prototypes for these functions are in the header file `stdio.h'.

Function: int scanf (const char *template, ...)
The scanf function reads formatted input from the stream stdin under the control of the template string template. The optional arguments are pointers to the places which receive the resulting values.

The return value is normally the number of successful assignments. If an end-of-file condition is detected before any matches are performed, including matches against whitespace and literal characters in the template, then EOF is returned.

Function: int wscanf (const wchar_t *template, ...)
The wscanf function reads formatted input from the stream stdin under the control of the template string template. The optional arguments are pointers to the places which receive the resulting values.

The return value is normally the number of successful assignments. If an end-of-file condition is detected before any matches are performed, including matches against whitespace and literal characters in the template, then WEOF is returned.

Function: int fscanf (FILE *stream, const char *template, ...)
This function is just like scanf, except that the input is read from the stream stream instead of stdin.

Function: int fwscanf (FILE *stream, const wchar_t *template, ...)
This function is just like wscanf, except that the input is read from the stream stream instead of stdin.

Function: int sscanf (const char *s, const char *template, ...)
This is like scanf, except that the characters are taken from the null-terminated string s instead of from a stream. Reaching the end of the string is treated as an end-of-file condition.

The behavior of this function is undefined if copying takes place between objects that overlap--for example, if s is also given as an argument to receive a string read under control of the `%s', `%S', or `%[' conversion.

Function: int swscanf (const wchar_t *ws, const char *template, ...)
This is like wscanf, except that the characters are taken from the null-terminated string ws instead of from a stream. Reaching the end of the string is treated as an end-of-file condition.

The behavior of this function is undefined if copying takes place between objects that overlap--for example, if ws is also given as an argument to receive a string read under control of the `%s', `%S', or `%[' conversion.

Variable Arguments Input Functions

The functions vscanf and friends are provided so that you can define your own variadic scanf-like functions that make use of the same internals as the built-in formatted output functions. These functions are analogous to the vprintf series of output functions. See section Variable Arguments Output Functions, for important information on how to use them.

Portability Note: The functions listed in this section were introduced in ISO C99 and were before available as GNU extensions.

Function: int vscanf (const char *template, va_list ap)
This function is similar to scanf, but instead of taking a variable number of arguments directly, it takes an argument list pointer ap of type va_list (see section Variadic Functions).

Function: int vwscanf (const wchar_t *template, va_list ap)
This function is similar to wscanf, but instead of taking a variable number of arguments directly, it takes an argument list pointer ap of type va_list (see section Variadic Functions).

Function: int vfscanf (FILE *stream, const char *template, va_list ap)
This is the equivalent of fscanf with the variable argument list specified directly as for vscanf.

Function: int vfwscanf (FILE *stream, const wchar_t *template, va_list ap)
This is the equivalent of fwscanf with the variable argument list specified directly as for vwscanf.

Function: int vsscanf (const char *s, const char *template, va_list ap)
This is the equivalent of sscanf with the variable argument list specified directly as for vscanf.

Function: int vswscanf (const wchar_t *s, const wchar_t *template, va_list ap)
This is the equivalent of swscanf with the variable argument list specified directly as for vwscanf.

In GNU C, there is a special construct you can use to let the compiler know that a function uses a scanf-style format string. Then it can check the number and types of arguments in each call to the function, and warn you when they do not match the format string. For details, See section `Declaring Attributes of Functions' in Using GNU CC.

End-Of-File and Errors

Many of the functions described in this chapter return the value of the macro EOF to indicate unsuccessful completion of the operation. Since EOF is used to report both end of file and random errors, it's often better to use the feof function to check explicitly for end of file and ferror to check for errors. These functions check indicators that are part of the internal state of the stream object, indicators set if the appropriate condition was detected by a previous I/O operation on that stream.

Macro: int EOF
This macro is an integer value that is returned by a number of narrow stream functions to indicate an end-of-file condition, or some other error situation. With the GNU library, EOF is -1. In other libraries, its value may be some other negative number.

This symbol is declared in `stdio.h'.

Macro: int WEOF
This macro is an integer value that is returned by a number of wide stream functions to indicate an end-of-file condition, or some other error situation. With the GNU library, WEOF is -1. In other libraries, its value may be some other negative number.

This symbol is declared in `wchar.h'.

Function: int feof (FILE *stream)
The feof function returns nonzero if and only if the end-of-file indicator for the stream stream is set.

This symbol is declared in `stdio.h'.

Function: int feof_unlocked (FILE *stream)
The feof_unlocked function is equivalent to the feof function except that it does not implicitly lock the stream.

This function is a GNU extension.

This symbol is declared in `stdio.h'.

Function: int ferror (FILE *stream)
The ferror function returns nonzero if and only if the error indicator for the stream stream is set, indicating that an error has occurred on a previous operation on the stream.

This symbol is declared in `stdio.h'.

Function: int ferror_unlocked (FILE *stream)
The ferror_unlocked function is equivalent to the ferror function except that it does not implicitly lock the stream.

This function is a GNU extension.

This symbol is declared in `stdio.h'.

In addition to setting the error indicator associated with the stream, the functions that operate on streams also set errno in the same way as the corresponding low-level functions that operate on file descriptors. For example, all of the functions that perform output to a stream--such as fputc, printf, and fflush---are implemented in terms of write, and all of the errno error conditions defined for write are meaningful for these functions. For more information about the descriptor-level I/O functions, see section Low-Level Input/Output.

Recovering from errors

You may explicitly clear the error and EOF flags with the clearerr function.

Function: void clearerr (FILE *stream)
This function clears the end-of-file and error indicators for the stream stream.

The file positioning functions (see section File Positioning) also clear the end-of-file indicator for the stream.

Function: void clearerr_unlocked (FILE *stream)
The clearerr_unlocked function is equivalent to the clearerr function except that it does not implicitly lock the stream.

This function is a GNU extension.

Note that it is not correct to just clear the error flag and retry a failed stream operation. After a failed write, any number of characters since the last buffer flush may have been committed to the file, while some buffered data may have been discarded. Merely retrying can thus cause lost or repeated data.

A failed read may leave the file pointer in an inappropriate position for a second try. In both cases, you should seek to a known position before retrying.

Most errors that can happen are not recoverable -- a second try will always fail again in the same way. So usually it is best to give up and report the error to the user, rather than install complicated recovery logic.

One important exception is EINTR (see section Primitives Interrupted by Signals). Many stream I/O implementations will treat it as an ordinary error, which can be quite inconvenient. You can avoid this hassle by installing all signals with the SA_RESTART flag.

For similar reasons, setting nonblocking I/O on a stream's file descriptor is not usually advisable.

Text and Binary Streams

The GNU system and other POSIX-compatible operating systems organize all files as uniform sequences of characters. However, some other systems make a distinction between files containing text and files containing binary data, and the input and output facilities of ISO C provide for this distinction. This section tells you how to write programs portable to such systems.

When you open a stream, you can specify either a text stream or a binary stream. You indicate that you want a binary stream by specifying the `b' modifier in the opentype argument to fopen; see section Opening Streams. Without this option, fopen opens the file as a text stream.

Text and binary streams differ in several ways:

Since a binary stream is always more capable and more predictable than a text stream, you might wonder what purpose text streams serve. Why not simply always use binary streams? The answer is that on these operating systems, text and binary streams use different file formats, and the only way to read or write "an ordinary file of text" that can work with other text-oriented programs is through a text stream.

In the GNU library, and on all POSIX systems, there is no difference between text streams and binary streams. When you open a stream, you get the same kind of stream regardless of whether you ask for binary. This stream can handle any file content, and has none of the restrictions that text streams sometimes have.

File Positioning

The file position of a stream describes where in the file the stream is currently reading or writing. I/O on the stream advances the file position through the file. In the GNU system, the file position is represented as an integer, which counts the number of bytes from the beginning of the file. See section File Position.

During I/O to an ordinary disk file, you can change the file position whenever you wish, so as to read or write any portion of the file. Some other kinds of files may also permit this. Files which support changing the file position are sometimes referred to as random-access files.

You can use the functions in this section to examine or modify the file position indicator associated with a stream. The symbols listed below are declared in the header file `stdio.h'.

Function: long int ftell (FILE *stream)
This function returns the current file position of the stream stream.

This function can fail if the stream doesn't support file positioning, or if the file position can't be represented in a long int, and possibly for other reasons as well. If a failure occurs, a value of -1 is returned.

Function: off_t ftello (FILE *stream)
The ftello function is similar to ftell, except that it returns a value of type off_t. Systems which support this type use it to describe all file positions, unlike the POSIX specification which uses a long int. The two are not necessarily the same size. Therefore, using ftell can lead to problems if the implementation is written on top of a POSIX compliant low-level I/O implementation, and using ftello is preferable whenever it is available.

If this function fails it returns (off_t) -1. This can happen due to missing support for file positioning or internal errors. Otherwise the return value is the current file position.

The function is an extension defined in the Unix Single Specification version 2.

When the sources are compiled with _FILE_OFFSET_BITS == 64 on a 32 bit system this function is in fact ftello64. I.e., the LFS interface transparently replaces the old interface.

Function: off64_t ftello64 (FILE *stream)
This function is similar to ftello with the only difference that the return value is of type off64_t. This also requires that the stream stream was opened using either fopen64, freopen64, or tmpfile64 since otherwise the underlying file operations to position the file pointer beyond the @math{2^31} bytes limit might fail.

If the sources are compiled with _FILE_OFFSET_BITS == 64 on a 32 bits machine this function is available under the name ftello and so transparently replaces the old interface.

Function: int fseek (FILE *stream, long int offset, int whence)
The fseek function is used to change the file position of the stream stream. The value of whence must be one of the constants SEEK_SET, SEEK_CUR, or SEEK_END, to indicate whether the offset is relative to the beginning of the file, the current file position, or the end of the file, respectively.

This function returns a value of zero if the operation was successful, and a nonzero value to indicate failure. A successful call also clears the end-of-file indicator of stream and discards any characters that were "pushed back" by the use of ungetc.

fseek either flushes any buffered output before setting the file position or else remembers it so it will be written later in its proper place in the file.

Function: int fseeko (FILE *stream, off_t offset, int whence)
This function is similar to fseek but it corrects a problem with fseek in a system with POSIX types. Using a value of type long int for the offset is not compatible with POSIX. fseeko uses the correct type off_t for the offset parameter.

For this reason it is a good idea to prefer ftello whenever it is available since its functionality is (if different at all) closer the underlying definition.

The functionality and return value is the same as for fseek.

The function is an extension defined in the Unix Single Specification version 2.

When the sources are compiled with _FILE_OFFSET_BITS == 64 on a 32 bit system this function is in fact fseeko64. I.e., the LFS interface transparently replaces the old interface.

Function: int fseeko64 (FILE *stream, off64_t offset, int whence)
This function is similar to fseeko with the only difference that the offset parameter is of type off64_t. This also requires that the stream stream was opened using either fopen64, freopen64, or tmpfile64 since otherwise the underlying file operations to position the file pointer beyond the @math{2^31} bytes limit might fail.

If the sources are compiled with _FILE_OFFSET_BITS == 64 on a 32 bits machine this function is available under the name fseeko and so transparently replaces the old interface.

Portability Note: In non-POSIX systems, ftell, ftello, fseek and fseeko might work reliably only on binary streams. See section Text and Binary Streams.

The following symbolic constants are defined for use as the whence argument to fseek. They are also used with the lseek function (see section Input and Output Primitives) and to specify offsets for file locks (see section Control Operations on Files).

Macro: int SEEK_SET
This is an integer constant which, when used as the whence argument to the fseek or fseeko function, specifies that the offset provided is relative to the beginning of the file.

Macro: int SEEK_CUR
This is an integer constant which, when used as the whence argument to the fseek or fseeko function, specifies that the offset provided is relative to the current file position.

Macro: int SEEK_END
This is an integer constant which, when used as the whence argument to the fseek or fseeko function, specifies that the offset provided is relative to the end of the file.

Function: void rewind (FILE *stream)
The rewind function positions the stream stream at the beginning of the file. It is equivalent to calling fseek or fseeko on the stream with an offset argument of 0L and a whence argument of SEEK_SET, except that the return value is discarded and the error indicator for the stream is reset.

These three aliases for the `SEEK_...' constants exist for the sake of compatibility with older BSD systems. They are defined in two different header files: `fcntl.h' and `sys/file.h'.

L_SET
An alias for SEEK_SET.
L_INCR
An alias for SEEK_CUR.
L_XTND
An alias for SEEK_END.

Portable File-Position Functions

On the GNU system, the file position is truly a character count. You can specify any character count value as an argument to fseek or fseeko and get reliable results for any random access file. However, some ISO C systems do not represent file positions in this way.

On some systems where text streams truly differ from binary streams, it is impossible to represent the file position of a text stream as a count of characters from the beginning of the file. For example, the file position on some systems must encode both a record offset within the file, and a character offset within the record.

As a consequence, if you want your programs to be portable to these systems, you must observe certain rules:

But even if you observe these rules, you may still have trouble for long files, because ftell and fseek use a long int value to represent the file position. This type may not have room to encode all the file positions in a large file. Using the ftello and fseeko functions might help here since the off_t type is expected to be able to hold all file position values but this still does not help to handle additional information which must be associated with a file position.

So if you do want to support systems with peculiar encodings for the file positions, it is better to use the functions fgetpos and fsetpos instead. These functions represent the file position using the data type fpos_t, whose internal representation varies from system to system.

These symbols are declared in the header file `stdio.h'.

Data Type: fpos_t
This is the type of an object that can encode information about the file position of a stream, for use by the functions fgetpos and fsetpos.

In the GNU system, fpos_t is equivalent to off_t or long int. In other systems, it might have a different internal representation.

When compiling with _FILE_OFFSET_BITS == 64 on a 32 bit machine this type is in fact equivalent to off64_t since the LFS interface transparently replaced the old interface.

Data Type: fpos64_t
This is the type of an object that can encode information about the file position of a stream, for use by the functions fgetpos64 and fsetpos64.

In the GNU system, fpos64_t is equivalent to off64_t or long long int. In other systems, it might have a different internal representation.

Function: int fgetpos (FILE *stream, fpos_t *position)
This function stores the value of the file position indicator for the stream stream in the fpos_t object pointed to by position. If successful, fgetpos returns zero; otherwise it returns a nonzero value and stores an implementation-defined positive value in errno.

When the sources are compiled with _FILE_OFFSET_BITS == 64 on a 32 bit system the function is in fact fgetpos64. I.e., the LFS interface transparently replaced the old interface.

Function: int fgetpos64 (FILE *stream, fpos64_t *position)
This function is similar to fgetpos but the file position is returned in a variable of type fpos64_t to which position points.

If the sources are compiled with _FILE_OFFSET_BITS == 64 on a 32 bits machine this function is available under the name fgetpos and so transparently replaces the old interface.

Function: int fsetpos (FILE *stream, const fpos_t *position)
This function sets the file position indicator for the stream stream to the position position, which must have been set by a previous call to fgetpos on the same stream. If successful, fsetpos clears the end-of-file indicator on the stream, discards any characters that were "pushed back" by the use of ungetc, and returns a value of zero. Otherwise, fsetpos returns a nonzero value and stores an implementation-defined positive value in errno.

When the sources are compiled with _FILE_OFFSET_BITS == 64 on a 32 bit system the function is in fact fsetpos64. I.e., the LFS interface transparently replaced the old interface.

Function: int fsetpos64 (FILE *stream, const fpos64_t *position)
This function is similar to fsetpos but the file position used for positioning is provided in a variable of type fpos64_t to which position points.

If the sources are compiled with _FILE_OFFSET_BITS == 64 on a 32 bits machine this function is available under the name fsetpos and so transparently replaces the old interface.

Stream Buffering

Characters that are written to a stream are normally accumulated and transmitted asynchronously to the file in a block, instead of appearing as soon as they are output by the application program. Similarly, streams often retrieve input from the host environment in blocks rather than on a character-by-character basis. This is called buffering.

If you are writing programs that do interactive input and output using streams, you need to understand how buffering works when you design the user interface to your program. Otherwise, you might find that output (such as progress or prompt messages) doesn't appear when you intended it to, or displays some other unexpected behavior.

This section deals only with controlling when characters are transmitted between the stream and the file or device, and not with how things like echoing, flow control, and the like are handled on specific classes of devices. For information on common control operations on terminal devices, see section Low-Level Terminal Interface.

You can bypass the stream buffering facilities altogether by using the low-level input and output functions that operate on file descriptors instead. See section Low-Level Input/Output.

Buffering Concepts

There are three different kinds of buffering strategies:

Newly opened streams are normally fully buffered, with one exception: a stream connected to an interactive device such as a terminal is initially line buffered. See section Controlling Which Kind of Buffering, for information on how to select a different kind of buffering. Usually the automatic selection gives you the most convenient kind of buffering for the file or device you open.

The use of line buffering for interactive devices implies that output messages ending in a newline will appear immediately--which is usually what you want. Output that doesn't end in a newline might or might not show up immediately, so if you want them to appear immediately, you should flush buffered output explicitly with fflush, as described in section Flushing Buffers.

Flushing Buffers

Flushing output on a buffered stream means transmitting all accumulated characters to the file. There are many circumstances when buffered output on a stream is flushed automatically:

If you want to flush the buffered output at another time, call fflush, which is declared in the header file `stdio.h'.

Function: int fflush (FILE *stream)
This function causes any buffered output on stream to be delivered to the file. If stream is a null pointer, then fflush causes buffered output on all open output streams to be flushed.

This function returns EOF if a write error occurs, or zero otherwise.

Function: int fflush_unlocked (FILE *stream)
The fflush_unlocked function is equivalent to the fflush function except that it does not implicitly lock the stream.

The fflush function can be used to flush all streams currently opened. While this is useful in some situations it does often more than necessary since it might be done in situations when terminal input is required and the program wants to be sure that all output is visible on the terminal. But this means that only line buffered streams have to be flushed. Solaris introduced a function especially for this. It was always available in the GNU C library in some form but never officially exported.

Function: void _flushlbf (void)
The _flushlbf function flushes all line buffered streams currently opened.

This function is declared in the `stdio_ext.h' header.

Compatibility Note: Some brain-damaged operating systems have been known to be so thoroughly fixated on line-oriented input and output that flushing a line buffered stream causes a newline to be written! Fortunately, this "feature" seems to be becoming less common. You do not need to worry about this in the GNU system.

In some situations it might be useful to not flush the output pending for a stream but instead simply forget it. If transmission is costly and the output is not needed anymore this is valid reasoning. In this situation a non-standard function introduced in Solaris and available in the GNU C library can be used.

Function: void __fpurge (FILE *stream)
The __fpurge function causes the buffer of the stream stream to be emptied. If the stream is currently in read mode all input in the buffer is lost. If the stream is in output mode the buffered output is not written to the device (or whatever other underlying storage) and the buffer the cleared.

This function is declared in `stdio_ext.h'.

Controlling Which Kind of Buffering

After opening a stream (but before any other operations have been performed on it), you can explicitly specify what kind of buffering you want it to have using the setvbuf function.

The facilities listed in this section are declared in the header file `stdio.h'.

Function: int setvbuf (FILE *stream, char *buf, int mode, size_t size)
This function is used to specify that the stream stream should have the buffering mode mode, which can be either _IOFBF (for full buffering), _IOLBF (for line buffering), or _IONBF (for unbuffered input/output).

If you specify a null pointer as the buf argument, then setvbuf allocates a buffer itself using malloc. This buffer will be freed when you close the stream.

Otherwise, buf should be a character array that can hold at least size characters. You should not free the space for this array as long as the stream remains open and this array remains its buffer. You should usually either allocate it statically, or malloc (see section Unconstrained Allocation) the buffer. Using an automatic array is not a good idea unless you close the file before exiting the block that declares the array.

While the array remains a stream buffer, the stream I/O functions will use the buffer for their internal purposes. You shouldn't try to access the values in the array directly while the stream is using it for buffering.

The setvbuf function returns zero on success, or a nonzero value if the value of mode is not valid or if the request could not be honored.

Macro: int _IOFBF
The value of this macro is an integer constant expression that can be used as the mode argument to the setvbuf function to specify that the stream should be fully buffered.

Macro: int _IOLBF
The value of this macro is an integer constant expression that can be used as the mode argument to the setvbuf function to specify that the stream should be line buffered.

Macro: int _IONBF
The value of this macro is an integer constant expression that can be used as the mode argument to the setvbuf function to specify that the stream should be unbuffered.

Macro: int BUFSIZ
The value of this macro is an integer constant expression that is good to use for the size argument to setvbuf. This value is guaranteed to be at least 256.

The value of BUFSIZ is chosen on each system so as to make stream I/O efficient. So it is a good idea to use BUFSIZ as the size for the buffer when you call setvbuf.

Actually, you can get an even better value to use for the buffer size by means of the fstat system call: it is found in the st_blksize field of the file attributes. See section The meaning of the File Attributes.

Sometimes people also use BUFSIZ as the allocation size of buffers used for related purposes, such as strings used to receive a line of input with fgets (see section Character Input). There is no particular reason to use BUFSIZ for this instead of any other integer, except that it might lead to doing I/O in chunks of an efficient size.

Function: void setbuf (FILE *stream, char *buf)
If buf is a null pointer, the effect of this function is equivalent to calling setvbuf with a mode argument of _IONBF. Otherwise, it is equivalent to calling setvbuf with buf, and a mode of _IOFBF and a size argument of BUFSIZ.

The setbuf function is provided for compatibility with old code; use setvbuf in all new programs.

Function: void setbuffer (FILE *stream, char *buf, size_t size)
If buf is a null pointer, this function makes stream unbuffered. Otherwise, it makes stream fully buffered using buf as the buffer. The size argument specifies the length of buf.

This function is provided for compatibility with old BSD code. Use setvbuf instead.

Function: void setlinebuf (FILE *stream)
This function makes stream be line buffered, and allocates the buffer for you.

This function is provided for compatibility with old BSD code. Use setvbuf instead.

It is possible to query whether a given stream is line buffered or not using a non-standard function introduced in Solaris and available in the GNU C library.

Function: int __flbf (FILE *stream)
The __flbf function will return a nonzero value in case the stream stream is line buffered. Otherwise the return value is zero.

This function is declared in the `stdio_ext.h' header.

Two more extensions allow to determine the size of the buffer and how much of it is used. These functions were also introduced in Solaris.

Function: size_t __fbufsize (FILE *stream)
The __fbufsize function return the size of the buffer in the stream stream. This value can be used to optimize the use of the stream.

This function is declared in the `stdio_ext.h' header.

Function: size_t __fpending (FILE *stream) The __fpending
function returns the number of bytes currently in the output buffer. For wide-oriented stream the measuring unit is wide characters. This function should not be used on buffers in read mode or opened read-only.

This function is declared in the `stdio_ext.h' header.

Other Kinds of Streams

The GNU library provides ways for you to define additional kinds of streams that do not necessarily correspond to an open file.

One such type of stream takes input from or writes output to a string. These kinds of streams are used internally to implement the sprintf and sscanf functions. You can also create such a stream explicitly, using the functions described in section String Streams.

More generally, you can define streams that do input/output to arbitrary objects using functions supplied by your program. This protocol is discussed in section Programming Your Own Custom Streams.

Portability Note: The facilities described in this section are specific to GNU. Other systems or C implementations might or might not provide equivalent functionality.

String Streams

The fmemopen and open_memstream functions allow you to do I/O to a string or memory buffer. These facilities are declared in `stdio.h'.

Function: FILE * fmemopen (void *buf, size_t size, const char *opentype)
This function opens a stream that allows the access specified by the opentype argument, that reads from or writes to the buffer specified by the argument buf. This array must be at least size bytes long.

If you specify a null pointer as the buf argument, fmemopen dynamically allocates an array size bytes long (as with malloc; see section Unconstrained Allocation). This is really only useful if you are going to write things to the buffer and then read them back in again, because you have no way of actually getting a pointer to the buffer (for this, try open_memstream, below). The buffer is freed when the stream is open.

The argument opentype is the same as in fopen (see section Opening Streams). If the opentype specifies append mode, then the initial file position is set to the first null character in the buffer. Otherwise the initial file position is at the beginning of the buffer.

When a stream open for writing is flushed or closed, a null character (zero byte) is written at the end of the buffer if it fits. You should add an extra byte to the size argument to account for this. Attempts to write more than size bytes to the buffer result in an error.

For a stream open for reading, null characters (zero bytes) in the buffer do not count as "end of file". Read operations indicate end of file only when the file position advances past size bytes. So, if you want to read characters from a null-terminated string, you should supply the length of the string as the size argument.

Here is an example of using fmemopen to create a stream for reading from a string:

#include <stdio.h>

static char buffer[] = "foobar";

int
main (void)
{
  int ch;
  FILE *stream;

  stream = fmemopen (buffer, strlen (buffer), "r");
  while ((ch = fgetc (stream)) != EOF)
    printf ("Got %c\n", ch);
  fclose (stream);

  return 0;
}

This program produces the following output:

Got f
Got o
Got o
Got b
Got a
Got r

Function: FILE * open_memstream (char **ptr, size_t *sizeloc)
This function opens a stream for writing to a buffer. The buffer is allocated dynamically (as with malloc; see section Unconstrained Allocation) and grown as necessary.

When the stream is closed with fclose or flushed with fflush, the locations ptr and sizeloc are updated to contain the pointer to the buffer and its size. The values thus stored remain valid only as long as no further output on the stream takes place. If you do more output, you must flush the stream again to store new values before you use them again.

A null character is written at the end of the buffer. This null character is not included in the size value stored at sizeloc.

You can move the stream's file position with fseek or fseeko (see section File Positioning). Moving the file position past the end of the data already written fills the intervening space with zeroes.

Here is an example of using open_memstream:

#include <stdio.h>

int
main (void)
{
  char *bp;
  size_t size;
  FILE *stream;

  stream = open_memstream (&bp, &size);
  fprintf (stream, "hello");
  fflush (stream);
  printf ("buf = `%s', size = %d\n", bp, size);
  fprintf (stream, ", world");
  fclose (stream);
  printf ("buf = `%s', size = %d\n", bp, size);

  return 0;
}

This program produces the following output:

buf = `hello', size = 5
buf = `hello, world', size = 12

Obstack Streams

You can open an output stream that puts it data in an obstack. See section Obstacks.

Function: FILE * open_obstack_stream (struct obstack *obstack)
This function opens a stream for writing data into the obstack obstack. This starts an object in the obstack and makes it grow as data is written (see section Growing Objects).

Calling fflush on this stream updates the current size of the object to match the amount of data that has been written. After a call to fflush, you can examine the object temporarily.

You can move the file position of an obstack stream with fseek or fseeko (see section File Positioning). Moving the file position past the end of the data written fills the intervening space with zeros.

To make the object permanent, update the obstack with fflush, and then use obstack_finish to finalize the object and get its address. The following write to the stream starts a new object in the obstack, and later writes add to that object until you do another fflush and obstack_finish.

But how do you find out how long the object is? You can get the length in bytes by calling obstack_object_size (see section Status of an Obstack), or you can null-terminate the object like this:

obstack_1grow (obstack, 0);

Whichever one you do, you must do it before calling obstack_finish. (You can do both if you wish.)

Here is a sample function that uses open_obstack_stream:

char *
make_message_string (const char *a, int b)
{
  FILE *stream = open_obstack_stream (&message_obstack);
  output_task (stream);
  fprintf (stream, ": ");
  fprintf (stream, a, b);
  fprintf (stream, "\n");
  fclose (stream);
  obstack_1grow (&message_obstack, 0);
  return obstack_finish (&message_obstack);
}

Programming Your Own Custom Streams

This section describes how you can make a stream that gets input from an arbitrary data source or writes output to an arbitrary data sink programmed by you. We call these custom streams. The functions and types described here are all GNU extensions.

Custom Streams and Cookies

Inside every custom stream is a special object called the cookie. This is an object supplied by you which records where to fetch or store the data read or written. It is up to you to define a data type to use for the cookie. The stream functions in the library never refer directly to its contents, and they don't even know what the type is; they record its address with type void *.

To implement a custom stream, you must specify how to fetch or store the data in the specified place. You do this by defining hook functions to read, write, change "file position", and close the stream. All four of these functions will be passed the stream's cookie so they can tell where to fetch or store the data. The library functions don't know what's inside the cookie, but your functions will know.

When you create a custom stream, you must specify the cookie pointer, and also the four hook functions stored in a structure of type cookie_io_functions_t.

These facilities are declared in `stdio.h'.

Data Type: cookie_io_functions_t
This is a structure type that holds the functions that define the communications protocol between the stream and its cookie. It has the following members:

cookie_read_function_t *read
This is the function that reads data from the cookie. If the value is a null pointer instead of a function, then read operations on this stream always return EOF.
cookie_write_function_t *write
This is the function that writes data to the cookie. If the value is a null pointer instead of a function, then data written to the stream is discarded.
cookie_seek_function_t *seek
This is the function that performs the equivalent of file positioning on the cookie. If the value is a null pointer instead of a function, calls to fseek or fseeko on this stream can only seek to locations within the buffer; any attempt to seek outside the buffer will return an ESPIPE error.
cookie_close_function_t *close
This function performs any appropriate cleanup on the cookie when closing the stream. If the value is a null pointer instead of a function, nothing special is done to close the cookie when the stream is closed.

Function: FILE * fopencookie (void *cookie, const char *opentype, cookie_io_functions_t io-functions)
This function actually creates the stream for communicating with the cookie using the functions in the io-functions argument. The opentype argument is interpreted as for fopen; see section Opening Streams. (But note that the "truncate on open" option is ignored.) The new stream is fully buffered.

The fopencookie function returns the newly created stream, or a null pointer in case of an error.

Custom Stream Hook Functions

Here are more details on how you should define the four hook functions that a custom stream needs.

You should define the function to read data from the cookie as:

ssize_t reader (void *cookie, char *buffer, size_t size)

This is very similar to the read function; see section Input and Output Primitives. Your function should transfer up to size bytes into the buffer, and return the number of bytes read, or zero to indicate end-of-file. You can return a value of -1 to indicate an error.

You should define the function to write data to the cookie as:

ssize_t writer (void *cookie, const char *buffer, size_t size)

This is very similar to the write function; see section Input and Output Primitives. Your function should transfer up to size bytes from the buffer, and return the number of bytes written. You can return a value of -1 to indicate an error.

You should define the function to perform seek operations on the cookie as:

int seeker (void *cookie, fpos_t *position, int whence)

For this function, the position and whence arguments are interpreted as for fgetpos; see section Portable File-Position Functions. In the GNU library, fpos_t is equivalent to off_t or long int, and simply represents the number of bytes from the beginning of the file.

After doing the seek operation, your function should store the resulting file position relative to the beginning of the file in position. Your function should return a value of 0 on success and -1 to indicate an error.

You should define the function to do cleanup operations on the cookie appropriate for closing the stream as:

int cleaner (void *cookie)

Your function should return -1 to indicate an error, and 0 otherwise.

Data Type: cookie_read_function
This is the data type that the read function for a custom stream should have. If you declare the function as shown above, this is the type it will have.

Data Type: cookie_write_function
The data type of the write function for a custom stream.

Data Type: cookie_seek_function
The data type of the seek function for a custom stream.

Data Type: cookie_close_function
The data type of the close function for a custom stream.

Formatted Messages

On systems which are based on System V messages of programs (especially the system tools) are printed in a strict form using the fmtmsg function. The uniformity sometimes helps the user to interpret messages and the strictness tests of the fmtmsg function ensure that the programmer follows some minimal requirements.

Printing Formatted Messages

Messages can be printed to standard error and/or to the console. To select the destination the programmer can use the following two values, bitwise OR combined if wanted, for the classification parameter of fmtmsg:

MM_PRINT
Display the message in standard error.
MM_CONSOLE
Display the message on the system console.

The erroneous piece of the system can be signalled by exactly one of the following values which also is bitwise ORed with the classification parameter to fmtmsg:

MM_HARD
The source of the condition is some hardware.
MM_SOFT
The source of the condition is some software.
MM_FIRM
The source of the condition is some firmware.

A third component of the classification parameter to fmtmsg can describe the part of the system which detects the problem. This is done by using exactly one of the following values:

MM_APPL
The erroneous condition is detected by the application.
MM_UTIL
The erroneous condition is detected by a utility.
MM_OPSYS
The erroneous condition is detected by the operating system.

A last component of classification can signal the results of this message. Exactly one of the following values can be used:

MM_RECOVER
It is a recoverable error.
MM_NRECOV
It is a non-recoverable error.

Function: int fmtmsg (long int classification, const char *label, int severity, const char *text, const char *action, const char *tag)
Display a message described by its parameters on the device(s) specified in the classification parameter. The label parameter identifies the source of the message. The string should consist of two colon separated parts where the first part has not more than 10 and the second part not more than 14 characters. The text parameter describes the condition of the error, the action parameter possible steps to recover from the error and the tag parameter is a reference to the online documentation where more information can be found. It should contain the label value and a unique identification number.

Each of the parameters can be a special value which means this value is to be omitted. The symbolic names for these values are:

MM_NULLLBL
Ignore label parameter.
MM_NULLSEV
Ignore severity parameter.
MM_NULLMC
Ignore classification parameter. This implies that nothing is actually printed.
MM_NULLTXT
Ignore text parameter.
MM_NULLACT
Ignore action parameter.
MM_NULLTAG
Ignore tag parameter.

There is another way certain fields can be omitted from the output to standard error. This is described below in the description of environment variables influencing the behaviour.

The severity parameter can have one of the values in the following table:

MM_NOSEV
Nothing is printed, this value is the same as MM_NULLSEV.
MM_HALT
This value is printed as HALT.
MM_ERROR
This value is printed as ERROR.
MM_WARNING
This value is printed as WARNING.
MM_INFO
This value is printed as INFO.

The numeric value of these five macros are between 0 and 4. Using the environment variable SEV_LEVEL or using the addseverity function one can add more severity levels with their corresponding string to print. This is described below (see section Adding Severity Classes).

If no parameter is ignored the output looks like this:

label: severity-string: text
TO FIX: action tag

The colons, new line characters and the TO FIX string are inserted if necessary, i.e., if the corresponding parameter is not ignored.

This function is specified in the X/Open Portability Guide. It is also available on all systems derived from System V.

The function returns the value MM_OK if no error occurred. If only the printing to standard error failed, it returns MM_NOMSG. If printing to the console fails, it returns MM_NOCON. If nothing is printed MM_NOTOK is returned. Among situations where all outputs fail this last value is also returned if a parameter value is incorrect.

There are two environment variables which influence the behaviour of fmtmsg. The first is MSGVERB. It is used to control the output actually happening on standard error (not the console output). Each of the five fields can explicitly be enabled. To do this the user has to put the MSGVERB variable with a format like the following in the environment before calling the fmtmsg function the first time:

MSGVERB=keyword[:keyword[:...]]

Valid keywords are label, severity, text, action, and tag. If the environment variable is not given or is the empty string, a not supported keyword is given or the value is somehow else invalid, no part of the message is masked out.

The second environment variable which influences the behaviour of fmtmsg is SEV_LEVEL. This variable and the change in the behaviour of fmtmsg is not specified in the X/Open Portability Guide. It is available in System V systems, though. It can be used to introduce new severity levels. By default, only the five severity levels described above are available. Any other numeric value would make fmtmsg print nothing.

If the user puts SEV_LEVEL with a format like

SEV_LEVEL=[description[:description[:...]]]

in the environment of the process before the first call to fmtmsg, where description has a value of the form

severity-keyword,level,printstring

The severity-keyword part is not used by fmtmsg but it has to be present. The level part is a string representation of a number. The numeric value must be a number greater than 4. This value must be used in the severity parameter of fmtmsg to select this class. It is not possible to overwrite any of the predefined classes. The printstring is the string printed when a message of this class is processed by fmtmsg (see above, fmtsmg does not print the numeric value but instead the string representation).

Adding Severity Classes

There is another possibility to introduce severity classes besides using the environment variable SEV_LEVEL. This simplifies the task of introducing new classes in a running program. One could use the setenv or putenv function to set the environment variable, but this is toilsome.

Function: int addseverity (int severity, const char *string)
This function allows the introduction of new severity classes which can be addressed by the severity parameter of the fmtmsg function. The severity parameter of addseverity must match the value for the parameter with the same name of fmtmsg, and string is the string printed in the actual messages instead of the numeric value.

If string is NULL the severity class with the numeric value according to severity is removed.

It is not possible to overwrite or remove one of the default severity classes. All calls to addseverity with severity set to one of the values for the default classes will fail.

The return value is MM_OK if the task was successfully performed. If the return value is MM_NOTOK something went wrong. This could mean that no more memory is available or a class is not available when it has to be removed.

This function is not specified in the X/Open Portability Guide although the fmtsmg function is. It is available on System V systems.

How to use fmtmsg and addseverity

Here is a simple example program to illustrate the use of the both functions described in this section.

#include <fmtmsg.h>

int
main (void)
{
  addseverity (5, "NOTE2");
  fmtmsg (MM_PRINT, "only1field", MM_INFO, "text2", "action2", "tag2");
  fmtmsg (MM_PRINT, "UX:cat", 5, "invalid syntax", "refer to manual",
          "UX:cat:001");
  fmtmsg (MM_PRINT, "label:foo", 6, "text", "action", "tag");
  return 0;
}

The second call to fmtmsg illustrates a use of this function as it usually occurs on System V systems, which heavily use this function. It seems worthwhile to give a short explanation here of how this system works on System V. The value of the label field (UX:cat) says that the error occured in the Unix program cat. The explanation of the error follows and the value for the action parameter is "refer to manual". One could be more specific here, if necessary. The tag field contains, as proposed above, the value of the string given for the label parameter, and additionally a unique ID (001 in this case). For a GNU environment this string could contain a reference to the corresponding node in the Info page for the program.

Running this program without specifying the MSGVERB and SEV_LEVEL function produces the following output:

UX:cat: NOTE2: invalid syntax
TO FIX: refer to manual UX:cat:001

We see the different fields of the message and how the extra glue (the colons and the TO FIX string) are printed. But only one of the three calls to fmtmsg produced output. The first call does not print anything because the label parameter is not in the correct form. The string must contain two fields, separated by a colon (see section Printing Formatted Messages). The third fmtmsg call produced no output since the class with the numeric value 6 is not defined. Although a class with numeric value 5 is also not defined by default, the call to addseverity introduces it and the second call to fmtmsg produces the above output.

When we change the environment of the program to contain SEV_LEVEL=XXX,6,NOTE when running it we get a different result:

UX:cat: NOTE2: invalid syntax
TO FIX: refer to manual UX:cat:001
label:foo: NOTE: text
TO FIX: action tag

Now the third call to fmtmsg produced some output and we see how the string NOTE from the environment variable appears in the message.

Now we can reduce the output by specifying which fields we are interested in. If we additionally set the environment variable MSGVERB to the value severity:label:action we get the following output:

UX:cat: NOTE2
TO FIX: refer to manual
label:foo: NOTE
TO FIX: action

I.e., the output produced by the text and the tag parameters to fmtmsg vanished. Please also note that now there is no colon after the NOTE and NOTE2 strings in the output. This is not necessary since there is no more output on this line because the text is missing.


Go to the first, previous, next, last section, table of contents.