C programming stdio.h


In C programming the <stdio.h> header declares three types,several macros and various functions to perform input and output operations in C.Here by input operation we mean writing a data to your files or to the console(the computer screen) or to any writable data storage(for instance an ‘array’) and by output we mean reading data from your files.While discussing input and output operation on files you will mainly come across two types of files:text file and binary file.A brief explanation of each type is given below.

i)Text file:Any file that support only the ASCII characters set are known as text file.So in text files you will see only the characters supported by the ‘unsigned char’ type.Usually this type of file has the extension of “.txt“.

ii)Binary file:Any files that supports the 256 characters set and more are known as binary file.To know if a file is a ‘binary file’ open the file with notepad++ and if you will see many unknown characters besides the ASCII character set then that file definitely is a binary file.Note any files containing unicode characters(with value greater than 127) or multi-byte characters like Japanese,Chinese or any native languages script(excluding the English Alphabet) will be treated as binary file.

Text stream

A stream associated with a text file is known as text stream.A text stream transfer the characters sequence as a lines.Each lines may have it’s ending character as a new-line character depending on the implementation.

Binary stream

In binary stream the sequence of bytes are transmitted without any interpretation.This means any control characters written to a file will be read back as it is.

A detail explanation of text stream and binary stream and also the difference between stream and buffer is given in another post,C stream and buffer

All the types,macros and functions found under <stdio.h> is given below.

Types

size_tAn ‘int’ type
FILEAn object capable of handling all the input and output stream including it’s file indicator , a pointer to its associated buffer,an error indicator that records whether a read/write error has occurred,and an end-of-file indicator that records whether the end of the file has been reached.
fpos_tAn object capable of specifying uniquely every position(including each character) within the file.

Note a file indicator is an object that determines the position of the next character to be read or written to or from a file.It more or less act like a pointer for a file.


Macors

NULLA null value.The integer value of this character correspond to the value 256
_IOFBF
_IOLBF
_IONBF
These macros expand to some distinct integer values and usable as third argument when ‘setvbuf’ function is called
BUFSIZAn integer constant that is the size of the buffer use by ‘setbuf’ function.
EOFA negative integer value that represents the end-of-file.
FOPEN_MAXAn integer value that represent the minimum number of files that can be opened simultaneously.
FILENAME_MAXAn integer value that represent the size needed for any array to hold the longest file name permitted by the implementation.
L_tmpnamAn integer value that represent the size needed for an array to hold the longest name for a temporary file
SEEK_CUR
SEEK_END
SEEK_SET
A distinct constant integer value usable a third argument by ‘fseek’ function.
TMP_MAXAn integer value that represent the maximum number of temporary files that can be generated by ‘tmpnam’ function.
stderr
stdin
stdout
A pointer to FILE objects that respectively that denotes an error stream, input stream and output stream.




Functions

All the functions declared in <stdio.h> header file is given below.

Return
type
Function name and
argument
Function definition
Operation on files
intremove(const char *file)It can delete a file.
intrename(const char *old, const char)Rename the file
FILE*tmpfile(void)Create temporary file
char*tmpnam(char *s)Generate a file name
File access function
intfclose(FILE *stream)Closes the file
intfflush(FILE *stream)flush the stream
FILE*fopen(const char * restrict filename , const char * restrict mode)Open the stream associated to the file
FILE*freopen(const char * restrict filename , const char * restrict mode , FILE * restrict stream)Reopens the file.
voidsetbuf(FILE * restrict stream , char * restrict buf)Determines the way of buffering the stream.
intsetvbuf(FILE * restrict stream , char * restrict buf, int mode , size_t size)Determines the way of buffering the stream.
Formatted input/output function
intfprintf(FILE * restrict stream , const char * restrict format, …)Writes output to stream
intfscanf(FILE * restrict stream , const char * restrict format, …)Reads input from stream.
intprintf(const char * restrict format, …)Writes output to console
intscanf(const char * restrict format, …)Reads input from console.
intsnprintf(char * restrict s, size_t n , const char * restrict format, …)Writes output to array
intsprintf(char * restrict s , const char * restrict format, …)Write output to array.
intsscanf(const char * restrict s , const char * restrict format, …)Reads input from string
intvfprintf(FILE * restrict stream , const char * restrict format , va_list arg)Equivalent to fprintf.
intvfscanf(FILE * restrict stream , const char * restrict format ,va_list arg)Equivalen to fscanf
intvprintf(const char * restrict format ,va_list arg)Somewhat equivalent to print
intvscanf(const char * restrict format , va_list arg)Somewhat equivalen to scanf
intvsnprintf(char * restrict s, size_t n ,const char * restrict format ,va_list arg)Somewhat equivalent to snprintf
intvsprintf(char * restrict s, const char * restrict format, va_list arg)Somewhat equivalent to sprintf
intvsscanf(const char * restrict s ,const char * restrict format, va_list arg)Somewhat equivalent to sscanf
Character input/output function
intfgetc(FILE *stream)Obtains character from the file
int*fgets(char * restrict s, int n ,FILE * restrict stream)Get string of specified length from the file
intfputc(int c, FILE *stream)Write character to file
intfputs(const char * restrict s , FILE * restrict stream)Write string to file
intgetc(FILE *stream)Get character from file
intgetchar(void)Get character from console
char*gets(char *s)Reads character from file to an array
intputc(int c, FILE *stream)Write character to file
intputchar(int c)Same as ‘putc’
intputs(const char *s)Write ‘s’ string to the console.
intungetc(int c, FILE *stream)Pushes back the character read into the input stream.
Direct input/output function
size_tfread(void * restrict ptr , size_t size, size_t nmemb , FILE * restrict stream)Reads character from stream into an array of specified size.
size_tfwrite(const void * restrict ptr, size_t size, size_t nmemb , FILE * restrict stream)Writes the string of specified size to the stream.
File positioning functions
intfgetpos(FILE * restrict stream,
fpos_t * restrict pos)
Stores the current value of parse state to ‘pos’
intfseek(FILE *stream, long int offset, int whence)Set the file indiator to specified position.
intfsetpos(FILE *stream, const fpos_t *pos)Set ‘pos’ position in stream.
long intftell(FILE *stream)Get the current value of file indicator.
voidrewind(FILE *stream)Set the file indicator to the beginning of the file.
Error handling functions
voidclearerr(FILE *stream)Clears the end-of-file and error indicators for the stream
intfeof(FILE *stream)Checks for EOF
intferror(FILE *stream)Checks for stream error.
voidperror(const char *s)Provide error message,

Related links

->C <stdlib.h> header