Easy Tutorial
❮ C Function Strcspn C Quiz ❯

C Standard Library - <stdio.h>

Introduction

The stdio.h header file defines three variable types, several macros, and various functions for performing input and output operations.

Library Variables

Below are the variable types defined in the stdio.h header file:

No. Variable & Description
1 size_t <br>This is an unsigned integer type, which is the result of the sizeof keyword.
2 FILE <br>This is a type suitable for storing information for a file stream.
3 fpos_t <br>This is a type suitable for storing any location within a file.

Library Macros

Below are the macros defined in the stdio.h header file:

No. Macro & Description
1 NULL <br>This macro is the value of a null pointer constant.
2 _IOFBF, _IOLBF, and _IONBF <br>These macros expand to integer constant expressions with distinct values and are used as the third argument to the setvbuf function.
3 BUFSIZ <br>This macro is an integer that represents the buffer size used by the setbuf function.
4 EOF <br>This macro is a negative integer indicating that the end-of-file has been reached.
5 FOPEN_MAX <br>This macro is an integer that represents the maximum number of files that the system can open simultaneously.
6 FILENAME_MAX <br>This macro is an integer that represents the maximum length of a character array that can store a file name. If there is no limit, this value should be the recommended maximum.
7 L_tmpnam <br>This macro is an integer that represents the maximum length of a character array that can store a temporary file name created by the tmpnam function.
8 SEEK_CUR, SEEK_END, and SEEK_SET <br>These macros are used in the fseek function to locate different positions within a file.
9 TMP_MAX <br>This macro is the maximum number of unique file names that the tmpnam function can generate.
10 stderr, stdin, and stdout <br>These macros are pointers to FILE type, corresponding to the standard error, standard input, and standard output streams, respectively.

Library Functions

Below are the functions defined in the stdio.h header file:

To better understand the functions, study them in the following sequence, as files created by the first function will be used in subsequent functions.

No. Function & Description
1 int fclose(FILE *stream) <br>Closes the stream. All buffers are flushed.
2 void clearerr(FILE *stream) <br>Clears the end-of-file and error indicators for the given stream.
3 int feof(FILE *stream) <br>Tests the end-of-file indicator for the given stream.
4 int ferror(FILE *stream) <br>Tests the error indicator for the given stream.
5 int fflush(FILE *stream) <br>Flushes the output buffer of a stream.
6 int fgetpos(FILE *stream, fpos_t *pos) <br>Gets the current file position of the stream and writes it to pos.
7 FILE *fopen(const char *filename, const char *mode) <br>Opens the file specified by filename using the given mode.
8 size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) <br>Reads data from the given stream into the array pointed to by ptr.
9 FILE *freopen(const char *filename, const char *mode, FILE *stream) <br>Associates a new filename with the given open stream and closes the old file in the stream.
10 int fseek(FILE *stream, long int offset, int whence) <br>Sets the file position of the stream to the given offset, which is interpreted relative to the position specified by whence.
11 int fsetpos(FILE *stream, const fpos_t *pos) <br>Sets the file position for the stream to the position given by pos, which is obtained by the fgetpos function.
12 long int ftell(FILE *stream) <br>Returns the current file position of the given stream.
13 size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) <br>Writes data from the array pointed to by ptr to the given stream.
14 int remove(const char *filename) <br>Deletes the given filename so that it is no longer accessible.
15 int rename(const char *old_filename, const char *new_filename) <br>Changes the name of the file specified by old_filename to new_filename.
16 void rewind(FILE *stream) <br>Sets the file position to the beginning of the file of the given stream.
17 void setbuf(FILE *stream, char *buffer) <br>Defines how a stream should be buffered.
18 int setvbuf(FILE *stream, char *buffer, int mode, size_t size) <br>Another function to define how a stream should be buffered.
19 FILE *tmpfile(void) <br>Creates a temporary file in binary update mode (wb+).
20 char *tmpnam(char *str) <br>Generates and returns a valid temporary filename which does not exist.
21 int fprintf(FILE *stream, const char *format, ...) <br>Sends formatted output to a stream.
22 int printf(const char *format, ...) <br>Sends formatted output to the standard output stdout.
23 int sprintf(char *str, const char *format, ...) <br>Sends formatted output to a string.
24 int vfprintf(FILE *stream, const char *format, va_list arg) <br>Sends formatted output to a stream using an argument list.
25 int vprintf(const char *format, va_list arg) <br>Sends formatted output to the standard output stdout using an argument list.
26 int vsprintf(char *str, const char *format, va_list arg) <br>Sends formatted output to a string using an argument list.
27 int fscanf(FILE *stream, const char *format, ...) <br>Reads formatted input from a stream.
28 int scanf(const char *format, ...) <br>Reads formatted input from the standard input stdin.
29 int sscanf(const char *str, const char *format, ...) <br>Reads formatted input from a string.
30 int fgetc(FILE *stream) <br>Gets the next character (an unsigned char) from the specified stream and advances the position indicator for the stream.
31 char *fgets(char *str, int n, FILE *stream) <br>Reads a line from the specified stream and stores it into the string pointed to by str. It stops when either (n-1) characters are read, the newline character is read, or the end-of-file is reached, whichever comes first.
32 int fputc(int char, FILE *stream) <br>Writes a character (an unsigned char) specified by the argument char to the specified stream and advances the position indicator for the stream.
33 int fputs(const char *str, FILE *stream) <br>Writes a string to the specified stream up to but not including the null character.
34 int getc(FILE *stream) <br>Gets the next character (an unsigned char) from the specified stream and advances the position indicator for the stream.
35 int getchar(void) <br>Gets a character (an unsigned char) from the standard input stdin.
36 char *gets(char *str) <br>Reads a line from the standard input stdin and stores it into the string pointed to by str. It stops when the newline character is read or when the end-of-file is reached, whichever comes first.
37 int putc(int char, FILE *stream) <br>Writes the character specified by the argument char (an unsigned character) into the specified stream stream and advances the position indicator.
38 int putchar(int char) <br>Writes the character specified by the argument char (an unsigned character) to the standard output stdout.
39 int puts(const char *str) <br>Writes a string to the standard output stdout up to the null character, but not including it. A newline character is appended to the output.
40 int ungetc(int char, FILE *stream) <br>Pushes the character char (an unsigned character) back into the specified stream stream so that it is the next character read.
41 void perror(const char *str) <br>Outputs a descriptive error message to the standard error stderr. It first outputs the string str followed by a colon and then a space.
42 int snprintf(char *str, size_t size, const char *format, ...) <br>Formats the string into str.
❮ C Function Strcspn C Quiz ❯