stdio.h

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

The keyword stdio.h in C is a header file. The reason why we use stdio.h in C is that this header file imports different variables, macros, and functions to perform input and output operations. To perform input and output operations in our C program, we need to import the stdio.h header file into our program.

Introduction

The stdio.h is one of the most commonly used header file in C. This file enables us to use the input and output functionality in C. As stdio.h contains more than 40 functions that are used to perform input and output operations, we get a lot of different ways to get and display data in C.

What is stdio.h in C?

The file stdio.h is a built-in header file in C. The acronym stdio stands for Standard Input and Output. This header file contains 3 variables, multiple function definitions, and multiple macro definitions. These variables, functions, and macros are related to the input and output operations in C.

Syntax to Include stdio.h in C

#include is a preprocessor directory in C. This keyword is used to include (or import) a header file into our program.

Use of stdio.h

The stdio.h header file allows us to perform input and output operations in C. The functions like printf() and scanf() are used to display output and take input from a user, respectively. This library allows us to communicate with users easily.

The stdio.h library uses streams to communicate with physical devices. A stream is a communication channel between a program and the physical input and output devices. For example, a standard input (stdin) is a stream used to read input data from a keyboard.

Stdio.h Library Variables

There are 3 variables (or types) in the stdio.h library. These are:

S. No.Variable NameVariable Description
1FILEIt is a data object that stores the information about controlling streams.
2size_tIt is an unsigned integral data type. It is the return type of the sizeof operator.
3fpos_tIt is a data object that is used to represent any position in a file.

Stdio.h Library Macros

The name given to a small piece of code is called a macro. Following are some of the commonly used stdio.h library's macros:

S. No.Macro NameMacro Description
1EOFIt stands for End Of File. It contains a negative integer that indicates the end of a file
2NULLIt represents a Null Pointer (a pointer that does not point to anything)
3FOPEN_MAXIt contains an integer that represents the number of files that can be opened simultaneously
4FILENAME_MAXIt contains an integer that represents the maximum size (in bytes) of a filename string
5L_tmpnamIt contains an integer that represents the maximum size of a temporary filename string
6BUFSIZIt contains an integer that represents the buffer size used by setbuf function
7TMP_MAXIt contains an integer that represents the maximum unique filenames the tmpnam function can generate
8stdin, stdout, stderrThese macros stand for standard input, standard output, and standard error, respectively. They are pointers to the FILE object
9_IOFBF, _IOLBF, _IONBFThese macros stand for Input Output Fully Buffered, Line Buffered, and Unbuffered, respectively. They specify the mode for file buffering
10SEEK_END, SEEK_SET, SEEK_CURThese macros are used to locate different file positions

Stdio.h Library Functions

There are several stdio.h library functions in C. These functions are used to perform different input and output functions.

Following are the stdio.h functions:

S. No.Function NameFunction Description
1printfDisplays data to stdout
2scanfReads input data from stdin
3fprintfDisplays data to the stream
4fscanfReads input data from the stream
5vprintfDisplays data from an argument list to stdout
6vscanfReads input data into an argument list
7sprintfWrites output to a string
8sscanfReads data from a string input
9snprintfWrites output to a buffer of given size
10vsnprintfDisplays data from an argument list to a buffer of given size
11vsprintfDisplays data from an argument list to a string
12vsscanfReads data from a string into an argument list
13vfprintfDisplays data from an argument list to the stream
14vfscanfReads input data from the stream to an argument list
15tmpnamCreates a new temporary file
16tmpfileOpens a temporary file in binary update mode
17renameRenames the given file
18removeDeletes the given file
19fopenOpens the given file
20fcloseCloses an open file
21fflushFlushes the output of the given stream
22setbufSpecifies the buffer of a stream
23setvbufUpdate the buffer of a stream
24freopenRestarts the stream to open a new file or open the existing file with a different access mode
25fgetcFetches the next character from the given stream
26fputcWrites a new character to the given stream
27fgetsFetches a line (string) from the given stream
28fputsWrites a new string to the given stream
29getcFetches the next character from the given stream
30getsFetches a string from stdin
31getcharFetches the next character from stdin
32putcharWrites a new character to stdout
33putcWrites a new character to the given stream
34putsWrites a new string to stdout
35ungetcPushes a character in the given stream in order to read the next character
36freadReads data from a stream
37fwriteWrites data to a stream
38rewindSets the file position of a given stream to the beginning
39fgetposFetches the current file position in a given stream
40fseekAdvances the position of a given stream to the specified number of bytes
41fsetposSets the position of a given stream to the position given by fgetpos function
42ftellFetches the current file position in a given stream
43ferrorChecks error indicators for a given stream
44feofChecks the EOF indicator for a given stream
45perrorDisplays the error message
46clearerrDeletes the error indicators for a given stream

Let us look at a simple example to understand a couple of commonly used stdio.h functions.

Output:

In the above example, we defined a string str and declared a variable extracted_value. Then, we used the sscanf() function (it comes from the stdio.h header file) to extract (or read) all the characters from the string str and store them in the variable extracted_value. Finally, we used another stdio.h function, i.e. printf(), to print the value stored in extracted_value.

Conclusion

  • The keyword stdio.h is a header file in C.
  • The stdio.h library imports variables, functions, and macros into our program.
  • The variables, functions, and macros imported by stdio.h are used to perform input and output operations.
  • printf() and scanf() are two of the most commonly used stdio.h functions.