C stdio.h
<stdio.h> is the standard C header for many input and output features.
It provides declarations and related definitions needed for functions such as printf(), scanf(), and fopen().
Quick Answer
Use #include <stdio.h> when your C program needs standard input or output facilities such as printf(), scanf(), file functions, the FILE type, the EOF macro, or the standard streams stdin, stdout, and stderr.
Include stdio.h
#include <stdio.h>
stdio means standard input/output.
The angle-bracket form is used for a standard library header.
Output Functions
<stdio.h> declares several functions that write output.
printf("Score: %d\n", 92);
puts("Hello");
printf() supports formatted output, while puts() writes a string followed by a newline.
Input Functions
The header also declares functions that read input.
int age;
scanf("%d", &age);
Other examples include getchar() and fgets().
File Input and Output
FILE *file = fopen("data.txt", "r");
<stdio.h> is not only for keyboard and screen input/output.
It also declares file functions such as fopen(), fclose(), fread(), fwrite(), fprintf(), and fscanf().
The FILE Type
FILE *file;
FILE is a type defined by <stdio.h>.
A pointer to FILE is used to work with a stream.
Standard Streams
| Stream | Common Purpose |
|---|---|
stdin |
Standard input |
stdout |
Standard output |
stderr |
Standard diagnostic output |
These predefined streams are available through <stdio.h>.
The EOF Macro
int ch = getchar();
if (ch == EOF) {
/* end of input or read failure */
}
EOF is a negative integer constant expression used by several input functions to report end-of-file or failure conditions.
C stdio.h Example
#include <stdio.h>
int main(void) {
int score = 92;
printf("Score: %d\n", score);
puts("Program finished.");
return 0;
}
Output
Score: 92
Program finished.
The program includes <stdio.h> because it uses printf() and puts().
Why the Header Matters
A function declaration tells the compiler the function's interface.
#include <stdio.h>
By including the correct standard header, the compiler can check calls such as printf() using the declaration provided by the implementation.
The Header Is Not the Whole Library
The header provides declarations, types, and macros needed by source code.
The actual implementation of standard I/O functions is supplied by the C library and implementation environment.
Common stdio.h Mistakes
| Mistake | Correct Idea |
|---|---|
Calling printf() without including <stdio.h>. |
Include the header that declares the function. |
Thinking stdio.h only handles console output. |
It also supports input, files, streams, and related facilities. |
Writing #include "stdio.h" for normal standard-header use. |
Use #include <stdio.h>. |
Assuming EOF is a normal character value. |
EOF is a negative integer constant expression used as a special return value. |
- Which header declares
printf()? - Which type is used to represent a stream object through a pointer?
- Which predefined stream is intended for diagnostic output?
Practice Answers
1. Which header declares printf()?
Use <stdio.h>.
2. Which type is used for streams?
The type is FILE, commonly used as FILE *.
3. Which stream is for diagnostics?
The standard diagnostic stream is stderr.
FAQs
What is stdio.h in C?
stdio.h is a standard C header that declares many input and output functions and defines related types and macros.
Do I need stdio.h for printf()?
Yes. Include stdio.h before calling printf() so the compiler has its proper declaration.
What are stdin, stdout, and stderr?
They are predefined standard streams for input, normal output, and diagnostic output.
Does stdio.h contain only console functions?
No. stdio.h also declares file input and output functions such as fopen(), fclose(), fread(), fwrite(), fprintf(), and fscanf().
Summary
<stdio.h> is the standard C input/output header. It declares functions such as printf(), scanf(), and file I/O functions, and it provides related items such as FILE, EOF, stdin, stdout, and stderr. Include it whenever your program uses these standard I/O facilities.