Stdio.h in C
Header files are used to make the programmer’s efforts a lot easier. In order to make the programming simple, there are a number of libraries which are included as predefined functions in C language. Header files are used to contain these predefined standard library functions.
Each and every one of the header files has a particular function. Any of the header files can be included in a program. Stdio.h is a header file provided by the C language. It stands for standard input output. Functions that are used to deal with input and output are declared in the stdio.h header file.
Syntax of stdio.h in C
#include<stdio.h>
Here, the “#include” is the preprocessing directive which is used to insert a particular header file from another file. The “.h” is an extension that can be found on all the header files. The content of a header file can be used by including it in a program.
Functions that are used to deal with the standard input and output are listed in the stdio.h header file. fdopen() is one of these functions which is only supported in POSIX program.
There are more than 40 functions contained in the stdio.h header file that are used to perform input and output operations, due to this we get a lot of different methods to input and display data in C. It is also one of the most commonly used header files in C language. It is a built-in header file.
For example, we can use the functions printf() and scanf() to display output and get input from the user. The communication with users becomes easier due to this library.
A communication channel between the program and the actual input and output devices is known as stream. In the stdio.h library these streams are used to communicate with the physical devices. A stream that is used to display output on a monitor is called as standard output (stdout).
There are a great number of functions declared in this library. Some of these are given in the following table:
fopen() | getc() | perror() | ferror() | feof() |
freopen() | rename() | fseek() | clearer() | setbuf() |
gets() | fflush() | fupdate() | fldata() | tmpfile() |
putc() | flocate() | sprintf() | fgetpos() | tmpnam() |
Defined types in stdio.h
To gain access to a particular stream, stream functions make use of a pointer to the FILE type. Stdio.h is where the FILE type is defined. The information provided in the FILE structure is used by the system to maintain the stream. Some of the C standard streams are also defined in the stdio.h, like stdin(), stdout() and stderr().
In order to determining error information in the event of an I/O function failure stdio.h defines the type’s__amrc_type and __amrc2_type.
In stdio.h, the type fldata_t is defined for the fldata() function.
Other types like __S99parms, __S99rbx_t, and __S99emparms_t are defined in stdio.h for use with the svc99() function.
For use with fgetpos() and fsetpos(), stdio.h defines the type fpos_t.
Library variables in stdio.h
In the stdio.h library, there we have three variables (or types). These 3 variables are given in the following table:
Variable | Description |
FILE | The information about controlling streams is stored in this data object. |
size_t | This is the return type of sizeof operator and is an unassigned integral data type. |
fpos_t | It is used to represent any position in the file. |
A basic example of use of stdio.h in C is shown below:
Example:
#include<stdio.h>
int main(){
intnum;
printf(“Enter the number:”);
scanf(“%d”,&num);
printf(“The entered number is : %d “,num);
return 0;
}
Output:
Enter the number: 5
The entered number is: 5
Stdio.h Macros
Just like the library variables, the stdio.h library also contains macros. Macros are the name given to a small piece of code. Some of stdio.h library’s commonly used macros are given below:
S.NO. | Macro | Description |
1. | FOPEN_MAX() | This macro is an integer that represents the maximum number of files that the system can guarantee to be opened simultaneously. |
2. | FILENAME_MAX() | A filename's maximum number of characters. It can be used to specify an array's size (for holding, for instance, the filename returned by fldata()). |
3. | EOF() | It specifies the value that an I/O function returns when it finds the End of File (EOF) or, in some cases, an error. |
4. | BUFSIZ() | When allocating buffers for stream I/O, this value is the expected size of the user's buffer that is supplied to setbuf(). It is the buffer size that will be used by the setbuf() library function. z/OS XL C/C++ applications will not use the user's buffer if a larger buffer is required. |
5. | NULL() | A NULL Pointer i.e. a pointer that does not point to anything. |
6. | L_tmpnam() | It contains an integer that represents the size of the longest temporary name that can be generated by the tmpnam() function. |
7. | TMP_MAX() | This macro is the maximum unique filenames the tmpnam() function can generate. |