YouTip LogoYouTip

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.

<stdio.h> is a header file in the C standard library that defines various functions and types for handling files and standard input/output streams.

Library Variables

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

No. Variable & Description
1 size_t This is an unsigned integer type, which is the result of the sizeof keyword and represents the size of an object.
2 FILE File stream type, an object type suitable for storing file stream information.
3 fpos_t File position type, an object type suitable for storing any position within a file.

Library Macros

The following macros are defined in the stdio.h header file:

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

Library Functions

The following functions are defined in the stdio.h header file:

To better understand these functions, study them in the order listed below, as the file created in the first function will be used in subsequent functions.

No. Function & Description
1 int fclose(FILE *stream) Closes the stream stream. Flushes all buffers.
2 void clearerr(FILE *stream) Clears the end-of-file and error indicators for the given stream stream.
3 int feof(FILE *stream) Tests the end-of-file indicator for the given stream stream.
4 int ferror(FILE *stream) Tests the error indicator for the given stream stream.
5 int fflush(FILE *stream) Flushes the output buffer of the stream stream.
6 int fgetpos(FILE *stream, fpos_t *pos) Gets the current file position of the stream stream and writes it into pos.
7 FILE *fopen(const char *filename, const char *mode) Opens the file pointed to by filename using the given mode.
8 size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) Reads data from the given stream stream into the array pointed to by ptr.
9 FILE *freopen(const char *filename, const char *mode, FILE *stream) Associates a new filename filename with the given open stream stream, closing the old file in the stream.
10 int fseek(FILE *stream, long int offset, int whence) Sets the file position of the stream stream to the given offset. The parameter offset specifies the number of bytes to seek from the position indicated by whence.
11 int fsetpos(FILE *stream, const fpos_t *pos) Sets the file position of the given stream stream to the specified position. The parameter pos is a position previously obtained by the fgetpos function.
12 long int ftell(FILE *stream) Returns the current file position of the given stream stream.
13 size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) Writes data from the array pointed to by ptr into the given stream stream.
14 int remove(const char *filename) Deletes the file specified by filename so it can no longer be accessed.
15 int rename(const char *old_filename, const char *new_filename) Renames the file pointed to by old_filename to new_filename.
16 void rewind(FILE *stream) Sets the file position to the beginning of the file associated with the given stream stream.
17 void setbuf(FILE *stream, char *buffer) Defines how the stream stream should be buffered.
18 int setvbuf(FILE *stream, char *buffer, int mode, size_t size) Another function that defines how the stream stream should be buffered.
19 FILE *tmpfile(void) Creates a temporary file in binary update mode (wb+).
20 char *tmpnam(char *str) Generates and returns a valid temporary filename that did not previously exist.
21 int fprintf(FILE *stream, const char *format, ...) Sends formatted output to the stream stream.
22 int printf(const char *format, ...) Sends formatted output to the standard output stdout.
23 int sprintf(char *str, const char *format, ...) Sends formatted output to a string.
24 int vfprintf(FILE *stream, const char *format, va_list arg) Sends formatted output to the stream stream using a variable argument list.
25 int vprintf(const char *format, va_list arg) Sends formatted output to the standard output stdout using a variable argument list.
26 int vsprintf(char *str, const char *format, va_list arg) Sends formatted output to a string using a variable argument list.
27 int fscanf(FILE *stream, const char *format, ...) Reads formatted input from the stream stream.
28 int scanf(const char *format, ...) Reads formatted input from the standard input stdin.
29 int sscanf(const char *str, const char *format, ...) Reads formatted input from a string.
30 int fgetc(FILE *stream) Gets the next character (an unsigned char) from the specified stream stream and advances the position indicator.
31 char *fgets(char *str, int n, FILE *stream) Reads a line from the specified stream stream and stores it in the string pointed to by str. It stops when it reads (n-1) characters, encounters a newline character, or reaches end-of-fileβ€”whichever occurs first.
32 int fputc(int char, FILE *stream) Writes the character specified by char (an unsigned char) to the specified stream stream and advances the position indicator.
33 int fputs(const char *str, FILE *stream) Writes a string to the specified stream stream, excluding the null character.
← C Macro AssertC Standard Library Stddef H β†’