Data Structures · Input and Output
Standard I/O Library and stdio.h Header File
Overview
The core C language has no built-in input/output facility; reading data into a program and writing data out of a program is provided by libraries that come with the compiler.
The functions that perform these operations live in the standard I/O library, and their declarations are made available through the header file stdio.h.
Key concepts
- No built-in I/O in C: the basic C language provides nothing for reading data into a program or writing data out of a program.
- Libraries: input/output is provided by libraries that are downloaded along with the compiler; the library contains the code used to read data as well as write data out of a program.
- Standard I/O library: the set of library functions that generally perform input/output operations.
- Header file: a file that contains declarations of functions.
stdio.his a header file. #include <stdio.h>: a directive meaning that all the declarations present in this header file are included into the program before compilation begins, so that no compiler error occurs.
How it works
- Rule in C: before any function is used, it must be declared — first declare it, then use it.
- If a function is used without being declared, an error results.
- Header files are the files that contain all these declarations.
- Writing
#include <stdio.h>includes all the declarations present in this header file into the program before compilation starts, so no compiler error is produced. - Therefore, to use
scanfandprintf,#include <stdio.h>must be written. #include <stdio.h>means including all the declarations from the standard I/O into the C program.
Revision summary
- C does not provide input/output by itself; it comes from libraries shipped with the compiler.
- All standard input/output functions live in the standard I/O library, accessed through the header file
stdio.h. - A header file contains declarations of functions, not the input/output code itself.
#include <stdio.h>pulls in all declarations from the standard I/O header before compilation, preventing compiler errors.- To use
scanfandprintf,#include <stdio.h>must be written; without it, using these functions gives an error. - The details of what the
#includedirective is and why and where to use it are covered later; for now, remember that it includes the standard I/O declarations into the program.
Previous-year exam insights
- In modern standard C (C99 and later), calling an undeclared function is an error (implicit declaration is not allowed); in older C89/C90 it was only a warning and the function was implicitly assumed to return int.
- The preprocessor handles #include before compilation: it textually inserts the contents of the named header file at that point in the source, so the compiler sees the declarations.
- Angle brackets <...> search the standard system include directories, while double quotes "..." search the current/project directory first and then the system directories.
- stdio.h declares (not defines) the standard I/O functions such as printf, scanf, getchar, putchar, gets, puts, fopen, fclose, fread, fwrite, fprintf, fscanf, and also defines types like FILE and macros like EOF, NULL, stdin, stdout, stderr.
- The actual code of the library functions is linked from the standard C library at link time; the header only supplies declarations (prototypes) and macros, so including a header does not by itself add code to the program.
- A declaration tells the compiler the function's name, return type and parameter types so calls can be type-checked; a definition supplies the actual body.
Missing declarations cause compile-time errors, while missing definitions cause link-time errors.