The
C programming language
C (''pronounced'' '' – like the letter c'') is a general-purpose programming language. It was created in the 1970s by Dennis Ritchie and remains very widely used and influential. By design, C's features cleanly reflect the capabilities of ...
provides many
standard library
In computer programming, a standard library is the library (computing), library made available across Programming language implementation, implementations of a programming language. Often, a standard library is specified by its associated program ...
functions for
file input and output
In computing, input/output (I/O, i/o, or informally io or IO) is the communication between an information processing system, such as a computer, and the outside world, such as another computer system, peripherals, or a human operator. Inputs ar ...
. These functions make up the bulk of the
C standard library
The C standard library, sometimes referred to as libc, is the standard library for the C (programming language), C programming language, as specified in the ISO C standard.International Organization for Standardization, ISO/International Electrote ...
header . The functionality descends from a "portable I/O package" written by
Mike Lesk
Michael E. Lesk (born 1945) is an American computer scientist.
Biography
In the 1960s, Michael Lesk worked for the SMART Information Retrieval System project, wrote much of its retrieval code and did many of the retrieval experiments, as well as ...
at
Bell Labs
Nokia Bell Labs, commonly referred to as ''Bell Labs'', is an American industrial research and development company owned by Finnish technology company Nokia. With headquarters located in Murray Hill, New Jersey, Murray Hill, New Jersey, the compa ...
in the early 1970s, and officially became part of the
Unix
Unix (, ; trademarked as UNIX) is a family of multitasking, multi-user computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, a ...
operating system in
Version 7.
The I/O functionality of C is fairly low-level by modern standards; C abstracts all file operations into operations on
stream
A stream is a continuous body of water, body of surface water Current (stream), flowing within the stream bed, bed and bank (geography), banks of a channel (geography), channel. Depending on its location or certain characteristics, a strea ...
s of
byte
The byte is a unit of digital information that most commonly consists of eight bits. Historically, the byte was the number of bits used to encode a single character of text in a computer and for this reason it is the smallest addressable un ...
s, which may be "input streams" or "output streams". Unlike some earlier programming languages, C has no direct support for
random-access
Random access (also called direct access) is the ability to access an arbitrary element of a sequence in equal time or any datum from a population of Address space, addressable elements roughly as easily and efficiently as any other, no matter h ...
data files; to read from a record in the middle of a file, the programmer must create a stream,
seek
SEEK Limited is an Australian employment website for job listings, headquartered in Melbourne, Victoria. Seek also operates in China, Hong Kong, Indonesia, Malaysia, New Zealand, Philippines, Singapore and Thailand.
History
Seek was founded ...
to the middle of the file, and then read bytes in sequence from the stream.
The stream model of file I/O was popularized by Unix, which was developed concurrently with the C programming language itself. The vast majority of modern operating systems have inherited streams from Unix, and many languages in the
C programming language family
C, or c, is the third Letter (alphabet), letter of the Latin alphabet, used in the English alphabet, modern English alphabet, the alphabets of other western European languages and others worldwide. Its name in English is English alphabet#L ...
have inherited C's file I/O interface with few if any changes (for example,
PHP
PHP is a general-purpose scripting language geared towards web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementation is now produced by the PHP Group. ...
).
Overview
This library uses what are called streams to operate with physical devices such as keyboards, printers, terminals or with any other type of files supported by the system. Streams are an abstraction to interact with these in a uniform way. All streams have similar properties independent of the individual characteristics of the physical media they are associated with.
Functions
Most of the C file input/output functions are defined in (or in the
C++ header , which contains the standard C functionality but in the
namespace
In computing, a namespace is a set of signs (''names'') that are used to identify and refer to objects of various kinds. A namespace ensures that all of a given set of objects have unique names so that they can be easily identified.
Namespaces ...
).
Constants
Constants defined in the header include:
Variables

Variables defined in the header include:
Member types
Data types defined in the header include:
* – also known as a file
handle
A handle is a part of, or an attachment to, an object that allows it to be grasped and object manipulation, manipulated by hand. The design of each type of handle involves substantial ergonomics, ergonomic issues, even where these are dealt wi ...
or a , this is an
opaque pointer
In computer programming, an opaque pointer is a special case of an opaque data type, a data type declared to be a pointer to a record or data structure of some unspecified type.
Opaque pointers are present in several programming languages inclu ...
containing the information about a file or text stream needed to perform input or output operations on it, including:
**platform-specific identifier of the associated I/O device, such as a
file descriptor
In Unix and Unix-like computer operating systems, a file descriptor (FD, less frequently fildes) is a process-unique identifier (handle) for a file or other input/output resource, such as a pipe or network socket.
File descriptors typically h ...
**the buffer
**stream orientation indicator (unset, narrow, or wide)
**stream buffering state indicator (unbuffered, line buffered, fully buffered)
**I/O mode indicator (input stream, output stream, or update stream)
**binary/text mode indicator
**end-of-file indicator
**error indicator
**the current stream position and multibyte conversion state (an object of type mbstate_t)
**reentrant lock (required as of
C11)
* – a non-array type capable of uniquely identifying the position of every byte in a file and every conversion state that can occur in all supported multibyte character encodings
* – an
unsigned integer
In computer science, an integer is a datum of integral data type, a data type that represents some range of mathematical integers. Integral data types may be of different sizes and may or may not be allowed to contain negative values. Integers are ...
type which is the type of the result of the operator.
Extensions
The
POSIX
The Portable Operating System Interface (POSIX; ) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines application programming interfaces (APIs), along with comm ...
standard defines several extensions to in its Base Definitions, among which are a function that allocates memory, the and functions that establish the link between objects and
file descriptor
In Unix and Unix-like computer operating systems, a file descriptor (FD, less frequently fildes) is a process-unique identifier (handle) for a file or other input/output resource, such as a pipe or network socket.
File descriptors typically h ...
s, and a group of functions for creating objects that refer to in-memory buffers.
Example
The following C program opens a binary file called ''myfile'', reads five bytes from it, and then closes the file.
#include
#include
int main(void)
Alternatives to stdio
Several alternatives to have been developed. Among these is the
C++ library, part of the
ISO C++ standard. ISO C++ still requires the functionality.
Other alternatives include the Sfio (A Safe/Fast I/O Library) library from
AT&T Bell Laboratories
Nokia Bell Labs, commonly referred to as ''Bell Labs'', is an American industrial research and development company owned by Finnish technology company Nokia. With headquarters located in Murray Hill, New Jersey, Murray Hill, New Jersey, the compa ...
. This library, introduced in 1991, aimed to avoid inconsistencies, unsafe practices and inefficiencies in the design of . Among its features is the possibility to insert
callback functions into a stream to customize the handling of data read from or written to the stream. It was released to the outside world in 1997, and the last release was 1 February 2005.
See also
*
printf format string
printf is a C standard library function that formats text and writes it to standard output. The function accepts a format c-string argument and a variable number of value arguments that the function serializes per the format string. Mism ...
*
scanf format string
scanf, short for scan formatted, is a C (programming language), C standard library Function (computer programming), function that reads and parsing, parses text from standard input.
The function accepts a format string parameter that specifie ...
References
External links
*
{{DEFAULTSORT:C file input output
C standard library
Input/output
Articles with example C code