For working professionals
For fresh graduates
More
5. Array in C
13. Boolean in C
18. Operators in C
33. Comments in C
38. Constants in C
41. Data Types in C
49. Double In C
58. For Loop in C
60. Functions in C
70. Identifiers in C
81. Linked list in C
83. Macros in C
86. Nested Loop in C
97. Pseudo-Code In C
100. Recursion in C
103. Square Root in C
104. Stack in C
106. Static function in C
107. Stdio.h in C
108. Storage Classes in C
109. strcat() in C
110. Strcmp in C
111. Strcpy in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
C programming relies on header files to access predefined functions, macros, and variables. Stdio.h in C is the widely used header file, which stands for standard input-output header. It serves as a vital component for input and output operations in C programs.
In addition to basic console I/O, stdio.h in C supports advanced features like file handling, error reporting, and stream manipulation. These capabilities make it indispensable not only for beginners learning the language but also for experienced developers building efficient and portable applications.
Explore our in-depth Software engineering courses to get a broader perspective.
Stdio.h is a standard library header file in C with a set of input and output functions, macros, and variables. It is essential for performing various input and output operations in C programs.
1. Simplicity: The stdio.h functions are easy to understand and use, and specifically simple for beginners to learn C programming.
2. Portability: The functions in stdio.h work across different platforms and operating systems to make code more portable.
3. Efficiency: The functions and macros in stdio.h are optimized for performance to write efficient code without worrying about low-level details.
4. Flexibility: stdio.h functions handle different input and output scenarios, it is a versatile alternative for different programming tasks.
5. Standardization: As part of the C standard library, stdio.h is widely supported and maintained by various compiler vendors, ensuring a consistent and reliable programming experience.
Header files included with the #include directives from the system or user specified.
If you are new to C programming, then you must explore the Features of C Language article.
Specify the preprocessor where to look for a suitable stdio h in c header file before using the #include directive.
1. #include
This method is used to include system Stdio h in c header file present in the predetermined path of the system directories.
#include <stdio.h>
void main() {
// Your C code here
}
2. #include "filename"
This method is used to include user-defined header files, located in the same directory as the source file.
#include "myheader.h"
void main() {
// Your C program code here
}
Importance of Include Stdio in C
Including stdio.h in a program, a programmer can utilize various functions, such as printf() and scanf(), to perform input and output operations.
If stdio.h is not included in the beginning it leads to an error during compilation, as the compiler will not recognize the functions or macros used in the program.
Role of the stdio.h Header File in C
The stdio.h header file plays a vital role in input and output operations within a C program. It enables transmission between the program and the environment with a set of stdio.h functions and macros working with input and output streams.
The common tasks enabled by stdio.h include reading data from a keyboard, writing data to a screen and working with files.
Also Explore: Functions in C
Example 1: Basic input and output
#include <stdio.h>
int main() {
int num;
printf("Enter an int: ");
scanf("%d", &num);
printf("Entered: %d\n", num);
return 0;
}
Output:
Enter an int: 41
Entered: 41
Explanation: Here, printf() is used to display a prompt from the user to enter an integer.
Then, scanf() to read the user's input and store it in the num variable.
And, printf() to display the entered number.
Example 2: Reading and writing to a file
#include <stdio.h>
int main() {
FILE *file;
int num;
file = fopen("numbers.txt", "w");
fprintf(file, "42\n");
fclose(file);
file = fopen("numbers.txt", "r");
fscanf(file, "%d", &num);
fclose(file);
printf("The number in the file is: %d\n", num);
return 0;
}
Output:
The number in the file is: 42
Explanation:
Here, fopen() is used to open a file named "numbers.txt" in write mode ("w").
Then fprintf() to write the number to the file and fclose() to close it.
The file is opened again in read mode ("r") and fscanf() to read the number from the file into the num variable and close the file.
Then printf() to display the number read from the file.
Example 3: File error handling
#include <stdio.h>
int main() {
FILE *file;
file = fopen("non_existent_file.txt", "r");
if (file == NULL) {
perror("Error in opening file");
return 1;
}
// File processing code in this section
fclose(file);
return 0;
}
Output:
Error in opening file: No such file or directory present
Explanation: Here, we test to open a non-existent file in read mode with fopen(). The function returns NULL if there is an error, so check the condition and use perror() to display an error message before passing a non-zero exit code.Must Explore: History of C Language
Example 4: Reading a line from a file
#include <stdio.h>
int main() {
FILE *file;
char line[100];
file = fopen("example.txt", "r");
fgets(line, sizeof(line), file);
fclose(file);
printf("The number one line of the file is: %s\n", line);
return 0;
}
Output:
The number one line of the file is: This is an example.
Example 5: Using stdin and stdout
#include <stdio.h>
int main() {
char name[50];
fprintf(stdout, "Enter the name: ");
fgets(name, sizeof(name), stdin);
fprintf(stdout, "Hey, %s", name);
return 0;
}
Output:
Enter the name: Joeil John
Hey, Joeil John
The stdio.h header file includes various built-in functions that facilitate I/O operations. These functions help display outputs to the screen, take input from the user, and work with files.
Example: Using printf(), scanf(), and fopen()
#include <stdio.h>
int main() {
char name[30];
int age;
// Taking input from user
printf("Enter your name: ");
scanf("%s", name);
printf("Enter your age: ");
scanf("%d", &age);
// Writing data to file
FILE *file = fopen("user.txt", "w");
fprintf(file, "Name: %s\nAge: %d", name, age);
fclose(file);
return 0;
}
Output:
Enter your name: Raj
Enter your age: 25
Explanation:
This program collects user input using scanf() and stores it in a file using fprintf().
For in-depth explanation read the C Language Download article!
Stdio.h in C Use
The stdio.h library plays a crucial role in file management. It provides functions like fopen() for opening files, fclose() for closing them, and fread() and fwrite() for binary file processing.
Example: Binary File Write and Read
#include <stdio.h>
int main() {
FILE *file;
int numbers[] = {10, 20, 30};
// Write binary data to file
file = fopen("data.bin", "wb");
fwrite(numbers, sizeof(int), 3, file);
fclose(file);
// Read binary data from file
int read_nums[3];
file = fopen("data.bin", "rb");
fread(read_nums, sizeof(int), 3, file);
fclose(file);
for (int i = 0; i < 3; i++) {
printf("Number %d: %d\n", i+1, read_nums[i]);
}
return 0;
}
Output:
Number 1: 10
Number 2: 20
Number 3: 30
Buffering temporarily stores data before it's read or written. This helps minimize disk I/O and improves speed.
Example: Setting Buffering Mode
#include <stdio.h>
int main() {
FILE *file = fopen("log.txt", "w");
// Set full buffering
char buffer[BUFSIZ];
setvbuf(file, buffer, _IOFBF, BUFSIZ);
fprintf(file, "Buffered message\n");
fclose(file);
return 0;
}
Output:
Content is written to log.txt using full buffering.
Explanation:
setvbuf() sets the buffering mode for file operations, improving performance for large writes.
Here are some of the main guidelines for including stdio.h in C:
Example: Bad vs Good Practice
Bad:
int scanf(); // Incorrect: Redefining a standard function
Good:
#include <stdio.h>
int main() {
int a;
if (scanf("%d", &a) != 1) {
printf("Input error!\n");
}
return 0;
}
The stdio.h header file defines several library variables in C:
1.FILE: A data object that stores information about how to control input/output streams.
2. size_t: An unsigned integral data type representing the output of the sizeof operator.
3. fpos_t: A data object representing any position within a file.
Stdio.h contains several library macros:
1. NULL: Represents a null pointer constant
2. EOF: A negative integer signifying the end of a file.
3. BUFSIZ: An integer representing the buffer size used by the setbuf function.
4. L_tmpnam: An integer indicating the maximum length of a character array that can hold the longest temporary filename generated by the tmpnam function.
5. FOPEN_MAX: indicates the maximum number of files opened concurrently by the system.
6. FILENAME_MAX: represents the maximum length of a character array holding the longest possible filename.
7. TMP_MAX: represents the maximum unique filenames the tmpnam function generates.
8. stdin, stdout, stderr: macros for standard input, standard output, and standard error. They are pointers to the FILE object.
9. _IOFBF, _IOLBF, _IONBF: macros for Input Output Fully Buffered, Line Buffered, and Unbuffered. They specify the mode for file buffering.
10. SEEK_END, SEEK_SET, SEEK_CUR: macros used to locate different file positions.
Must Explore: 29 C Programming Projects in 2025 for All Levels [Source Code Included]
The stdio.h library has numerous functions for performing various input and output tasks.
1. printf(): Prints formatted output to the console.
2. scanf(): Reads formatted input from the console.
3. fgets(): Reads a string from a file stream.
4. fputs(): Writes a string to a file stream.
5. fgetc(): Reads a character from a file stream.
6. fputc(): Writes a character to a file stream.
7. fread(): Reads data from a file stream into a buffer.
8. fwrite(): Writes data from a buffer into a file stream.
9. ftell(): Reports the current file position indicator for a file stream.
10.fopen(): Opens a file and returns a pointer to a file stream.
11. fclose(): Closes a file stream.
12. fflush(): Flushes the output buffer of a file stream.
13. setbuf(): Sets the buffer for a file stream.
14. setvbuf(): Sets the buffering mode for a file stream.
15. setlocale() - sets the locale for formatted input/output functions
16. fprintf(): Prints formatted output to a file stream.
17. fscanf(): Reads formatted input from a file stream.
18. tmpfile(): Creates a temporary file.
19. tmpnam(): Generates a unique temporary file name.
20. remove(): Deletes a file.
21. rename(): Renames a file.
22. freopen(): Reopens a file stream with a specified mode.
23. getchar(): Reads a character from the standard input.
24. putchar(): Writes a character to the standard output.
25. gets(): Reads a string from the standard input.
26. puts(): Writes a string to the standard output.
27. feof(): Checks if the end-of-file indicator is set for a file stream.
28. ferror(): Checks if the error indicator is set for a file stream.
29. clearerr(): Clears the error and end-of-file indicators for a file stream.
30. fseek() - sets the file position indicator to a location in a file stream.
31. ftell() - returns the current value of the file position indicator for a stream.
32. fgetc() - reads the next character from a stream.
33. vprintf() - prints formatted output with a variable argument list to stdout
34. vfprintf() - writes formatted output with a variable argument list to a file
35. vsprintf() - writes formatted output with a variable argument list to a string
36. vscanf() - reads formatted input with a variable argument list from stdin
37. vfscanf() - reads formatted input with a variable argument list from a file
38. vsscanf() - reads formatted input with a variable argument list from a string
The stdio.h header file is an essential component of C programming with a comprehensive set of functions for input and output operations. By comprehending the stdio.h in c source code, functions, macros, variables and stdio.h in c use, programmers can efficiently perform the reading and writing of data, handling files and manage errors related tasks.
If you forget to include stdio.h, the compiler won’t recognize standard I/O functions like printf() or scanf(). This results in compilation errors, as those function declarations are defined within the stdio.h header file.
fgets() is safer than gets() because it limits the number of characters read, preventing buffer overflows. While gets() reads input until a newline, fgets() lets you specify a maximum size, making it more reliable for input operations.
fprintf() works like printf() but writes formatted output to a file stream instead of the console. It’s helpful when you need to write structured text to a file, such as logs or reports, with control over formatting.
stdin refers to the standard input stream, typically the keyboard. stdout is the standard output stream, usually the screen. These macros help control where input comes from and where output is directed, making programs more flexible.
If fopen() fails to open a file, it returns NULL. This could happen due to permission issues or if the file doesn’t exist. Checking for NULL prevents undefined behavior and lets you handle the error using functions like perror().
Buffering temporarily stores data in memory before reading or writing it. This reduces the number of disk operations, making I/O faster and more efficient. You can manage buffering using functions like setbuf() or setvbuf() depending on your needs.
EOF stands for End Of File and is returned when no more data can be read from a file. It is typically used in loops to determine when to stop reading. Functions like fgetc() return EOF on reaching the file end.
Yes, fscanf() allows formatted input from a file just like scanf() does from the console. You can specify format specifiers to read integers, strings, or floats from files, making structured data parsing easy and efficient in C.
These macros help position the file pointer using fseek(). SEEK_SET moves to a specific byte from the beginning, SEEK_CUR moves relative to the current position, and SEEK_END moves relative to the file’s end. They're useful in file navigation.
You should check if file pointers are NULL after opening files. Use functions like ferror() to detect stream errors and perror() to display system-level error messages. This helps ensure your program can recover from or report file I/O failures.
The printf() function is used. You can use format specifiers such as %d, %f, and %s to display integers, floating-point numbers and strings respectively.
A file pointer is a variable pointer. In stdio.h, you use file pointers to read and write data to files. The most used file pointer functions are fopen(), fclose(), fgets(), and fputs().
fread() and fwrite() functions are used to read and write binary files respectively. Use "rb" or "wb" as the method parameter when opening the file.
Take a Free C Programming Quiz
Answer quick questions and assess your C programming knowledge
Author
Start Learning For Free
Explore Our Free Software Tutorials and Elevate your Career.
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918068792934
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.