C Tutorial stdio.h
Standard Library Functions





Stdio.h is a very common file to #include -- it includes functions to print and read strings

from files and to open and close files in the file system.


FILE* fopen(const char* fname, const char* mode);

Open a file named in the filesystem and return a FILE* for it. Mode = "r" read,"w"

write,"a"append, returns NULL on error. The standard files stdout, stdin,

stderr are automatically opened and closed for you by the system.


int fclose(FILE* file);

Close a previously opened file. Returns EOF on error. The operating system closes all

of a program's files when it exits, but it's tidy to do it beforehand. Also, there is

typically a limit to the number of files which a program may have open



int fgetc(FILE* in);

Read and return the next unsigned char out of a file, or EOF if the file has been

exhausted. (detail) This and other file functions return ints instead of a chars because

the EOF constant they potentially is not a char, but is an int. getc() is an alternate, faster version implemented as a macro which may evaluate the FILE* expression

more than once.


char* fgets(char* dest, int n, FILE* in)

Reads the next line of text into a string supplied by the caller. Reads at most n-1

characters from the file, stopping at the first '\n' character. In any case, the string is '\0'

terminated. The '\n' is included in the string. Returns NULL on EOF or error.


int fputc(int ch, FILE* out);

Write the char to the file as an unsigned char. Returns ch, or EOF on err. putc() is an

alternate, faster version implemented as a macro which may evaluate the FILE*

expression more than once.


int ungetc(int ch, FILE* in);

Push the most recent fgetc() char back onto the file. EOF may not be pushed back.

Returns ch or EOF on error.


int printf(const char* format_string, ...);

Prints a string with values possibly inserted into it to standard output. Takes a variable

number of arguments -- first a format string followed by a number of matching

arguments. The format string contains text mixed with % directives which mark

things to be inserted in the output. %d = int, %Ld=long int, %s=string, %f=double,

%c=char. Every % directive must have a matching argument of the correct type after

the format string. Returns the number of characters written, or negative on error. If

the percent directives do not match the number and type of arguments, printf() tends

to crash or otherwise do the wrong thing at run time. fprintf() is a variant which takes

an additional FILE* argument which specifies the file to print to. Examples...

printf("hello\n");                                                             prints:  hello

printf("hello %d there %d\n", 13, 1+1);                         prints:  hello 13 there 2

printf("hello %c there %d %s\n", 'A', 42, "ok");             prints:  hello A there 42 ok


int scanf(const char* format, ...)

Opposite of printf() -- reads characters from standard input trying to match elements

in the format string. Each percent directive in the format string must have a matching

pointer in the argument list which scanf() uses to store the values it finds. scanf()

skips whitespace as it tries to read in each percent directive. Returns the number of

percent directives processed successfully, or EOF on error. scanf() is famously

sensitive to programmer errors. If scanf() is called with anything but the correct

pointers after the format string, it tends to crash or otherwise do the wrong thing at

run time. sscanf() is a variant which takes an additional initial string from which it

does its reading.  fscanf() is a variant which takes an additional initial FILE* from

which it does its reading. Example...


int num;

char s1[1000];

char s2[1000];


scanf("hello %d %s %s", &num, s1, s2);


Looks for the word "hello" followed by a number and two words (all separated by

whitespace). scanf() uses the pointers &num, s1, and s2 to store what it finds into the local variables.


Want To Know more with

Video ???

Contact for more learning: webmaster@freehost7com