Stdio.h

From Wikipedia, the free encyclopedia

C++ Standard Library headers
C Standard Library headers
assert.h
ctype.h
errno.h
float.h
limits.h
locale.h
math.h
setjmp.h
signal.h
stdarg.h
stddef.h
stdio.h
string.h
stdlib.h
time.h
The correct title of this article is stdio.h. The initial letter is shown capitalized due to technical restrictions.

stdio.h, which stands for "standard input-output header," is the header in the C standard library for the C programming language which contains macro definitions, constants, and declarations of functions and types used for various standard input and output operations. C++ also has its own implementation of the functions from C for compatibility reasons, which are declared in the header cstdio.

Functions declared in stdio.h are extremely popular, since as a part of the C standard library, they are guaranteed to work on any platform which supports C. Applications on a particular platform might, however, have reasons to use the platform's I/O routines, rather than the stdio.h routines.

Contents

[edit] Example usage

All functions in C and its derivatives are declared in from header files. Thus, programmers have to include the stdio.h header into the source code to utilize the functions declared there.

 #include <stdio.h>

 int main(void)
 {
   int ch;

   while ((ch = getchar()) != EOF)
     putchar(ch);
   putchar('\n');
   
   return 0;
 }


The above program reads all input from the standard input and echo it onto the standard output, line by line.

[edit] Member functions

Functions declared in stdio.h can generally be divided into two categories: the functions for file manipulation and the functions for input-output manipulation.

Name Notes
File manipulation functions
fclose closes a file associated with the FILE * value passed to it
fopen, freopen, fdopen opens a file for certain types of reading or writing
remove removes a file (deletes it)
rename renames a file
rewind acts as if fseek(stream, 0L, SEEK_SET) was called for the stream passed, and then its error indicator cleared
tmpfile creates and open a temporary file, which is deleted when closed with fclose()
Input-output manipulation functions
clearerr clears end-of-file and error indicators for a given stream
feof checks whether an end-of-file indicator has been set for a given stream
ferror checks whether an error indicator has been set for a given stream
fflush forces any pending buffered output to be written to the file associated with a given stream
fgetpos stores the file position indicator of the stream associated by its first argument (a FILE *) to its second argument (a fpos_t *)
fgetc returns one character from a file
fgets gets a string from the file (ends at newline or null character)
fputc writes one character to a file
fputs writes a string to a file
ftell returns a file-position indicator which can then be passed to fseek
fseek seeks through a file
fsetpos sets the file position indicator of a stream associated by its first argument (a FILE *) as stored in its second argument (a fpos_t *)
fread reads different sized data from a file
fwrite writes different sized data to a file
getc reads and returns a character from a given stream and advances the file position indicator; it is allowed to be a macro with the same effects as fgetc, except that it may evaluate the stream more than once
getchar has the same effects as getc(stdin)
gets reads characters from stdin until a newline is encountered and stores them in its only argument
printf, fprintf, sprintf, snprintf used to print to stdout
vprintf also used to print to stdout
perror writes an error message to stderr
putc writes and returns a character to a stream and advances the file position indicator for it; it is allowed to be a macro with the same effects as fputc, except that it may evaluate the stream more than once
putchar, fputchar has the same effects as putc(stdout)
scanf, fscanf, sscanf used to input from stdin
vfscanf, vscanf, vsscanf also used to input from stdin
setbuf
setvbuf sets the buffering mode for a given stream
tmpnam creates a temporary filename and stores it in its first argument (a char *)
ungetc pushes a character back onto a stream
puts outputs a character string to stdout

[edit] Member constants

Constants defined in the stdio.h header include:

Name Notes
EOF a negative integer of type int used to indicate end-of-file conditions.
BUFSIZ an integer which is the size of the buffer used by the setbuf() function.
FILENAME_MAX the size of a char array which is large enough to store the name of any file that can be opened.
FOPEN_MAX the number of files that may be open simultaneously. Will be at least 8.
_IOFBF an abbreviation for "input/output fully buffered"; it is an integer which may be passed to the setvbuf() function to request block buffered input and output for an open stream.
_IOLBF an abbreviation for "input/output line buffered"; it is an integer which may be passed to the setvbuf() function to request line buffered input and output for an open stream.
_IONBF an abbreviation for "input/output not buffered"; it is an integer which may be passed to the setvbuf() function to request unbuffered input and output for an open stream.
L_tmpnam the size of a char array which is large enough to store a temporary filename generated by the tmpnam() function.
NULL a macro expanding to the null pointer constant; that is, a constant representing a pointer value which is guaranteed not to be a valid address of an object in memory.
SEEK_CUR an integer which may be passed to the fseek() function to request positioning relative to the current file position.
SEEK_END an integer which may be passed to the fseek() function to request positioning relative to the end of the file.
SEEK_SET an integer which may be passed to the fseek() function to request positioning relative to the beginning of the file.
TMP_MAX the maximum number of unique filenames generable by the tmpnam() function. Will be at least 25.

[edit] Member variables

Variables defined in the stdio.h header include:

Name Notes
stdin a pointer to FILE which refers to the standard input stream, usually a keyboard.
stdout a pointer to FILE which refers to the standard output stream, usually a display terminal.
stderr a pointer to FILE which refers to the standard error stream, often a display terminal.

[edit] Member Types

Data types defined in the stdio.h header include:

  • FILE - a structure containing the information about a file or text stream needed to perform input or output operations on it, including:
    • the current stream position
    • an end-of-file indicator
    • an error indicator
    • a pointer to the stream's buffer, if applicable
  • fpos_t - a non-array type capable of uniquely identifying the position of every byte in a file.
  • size_t - an unsigned integer type which is the type of the result of the sizeof operator.
In other languages