Overview of stdio.h Header File in C: Functions & Examples (Unit 3)
BCA Sem 1 | Unit 3 | Computer Concepts & C Programming
Overview of stdio.h Header File in C — BKNMU Junagadh
Standard Input/Output Functions, Formatted I/O, Character Streams, File Operations, and Constants
stdio.h header file stands for Standard Input Output Header. It is the most vital header file in C programming, containing declarations for functions, macro definitions, data types, and stream objects required to perform keyboard input, terminal output, and file management operations.
The stdio.h library organizes standard input/output routines into four major categories:
1. Formatted I/O
Reads and writes structured data types (integers, floats, strings) using format specifiers like %d and %f.
2. Character I/O
Processes individual character data directly from standard input (keyboard) or output (screen) streams.
3. String Line I/O
Reads entire lines of text up to a newline character or outputs complete string arrays to the console terminal.
4. File Handling
Manages external file streams using the FILE structure for reading, writing, and manipulating files on disk.
Here is a detailed breakdown of the primary functions supplied by the stdio.h standard library:
| Function Name | Functional Description | Standard Parameters / Return Value |
|---|---|---|
printf() | Prints formatted text, variables, and constants onto the standard output screen. | Format string and optional variable arguments. Returns character count printed. |
scanf() | Reads formatted user input from the keyboard into variable memory addresses. | Format string and variable pointer addresses (&var). Returns matched count. |
getchar() | Reads a single character from the standard keyboard input stream. | Takes no parameters. Returns the character read as an integer ASCII value. |
putchar() | Writes a single character parameter to the output console terminal. | Accepts character variable or ASCII value. Returns character written. |
sprintf() | Writes formatted output data into a string character array instead of console screen. | Target char array buffer, format string, and data arguments. |
stdio.h automatically grants access to standard system macros such as EOF (End Of File, usually -1), NULL (null pointer constant), and standard streams like stdin, stdout, and stderr.
1. Formatted Input/Output via stdio.h:
#include <stdio.h> // Required for printf and scanf
int main() {
int roll_no;
float marks;
printf("Enter Roll Number & Marks: ");
scanf("%d %f", &roll_no, &marks);
printf("Student Roll No: %d | Marks: %.2f\n", roll_no, marks);
return 0;
}
2. Single Character I/O (getchar and putchar):
#include <stdio.h>
int main() {
char ch;
printf("Enter any single character: ");
ch = getchar(); // Reads single char from stdin
printf("You entered: ");
putchar(ch); // Outputs single char to stdout
putchar('\n');
return 0;
}
Step 1: Preprocessor Directive
The compiler sees #include <stdio.h> and pastes function prototypes into your file before compiling.
Step 2: Stream Allocation
Standard streams (stdin, stdout, stderr) are automatically initialized when execution starts.
Step 3: Linker Resolution
The linker connects function calls like printf() to their compiled object code in standard C runtime libraries.
<>? Enclosing stdio.h inside angle brackets (#include <stdio.h>) tells the compiler preprocessor to search for the header file in standard system include directories rather than the current working folder.
stdio.h will trigger implicit function declaration warnings or compilation errors when using basic operations like printf() or scanf() in GCC compilers!
- Full Header Name: Standard Input Output Header (
stdio.h). - Primary Purpose: Declarations for console I/O, string stream formatting, and file streams.
- Key Functions:
printf(),scanf(),getchar(),putchar(),sprintf(),fopen(). - Built-in Streams:
stdin(Keyboard),stdout(Screen),stderr(Error output). - Built-in Macros:
EOF,NULL,BUFSIZ,FILENAME_MAX.