Formatted input/output with C functions

Formatted Output

The standard C library functions used for output are as follows. The methods take as arguments a format string and an optional list of variables or literals to output. The variables and literals are output according to the specifications in the format string. Here is the prototypes of the those functions:

int printf(const char *format, arg_1, arg_2, ... , arg_n);
int fprintf(FILE *stream, const char *format, arg_1, arg_2, ... , arg_n);
int sprintf(char *str, const char *format, arg_1, arg_2, ... , arg_n);
int snprintf(char *str, size_t size, const char *format, arg_1, arg_2, ... , arg_n);

The function printf() outputs to the standard output, the function fprintf() outputs to the stream given as the first parameter. sprintf() composes a zero-terminated string passed by reference as the first parameter. You must allocate enough space in it in advance. The function snprintf() outputs at most size characters to the string. The return value of these functions is the number of characters actually output (not including the trailing '\0' used to end output to strings).

The format string may consists both of characters and literals, and determines how the arguments arg_1 - arg_n will be formatted. Each of those arguments requires a format specifier, that begins with the character % and has the following syntax:

%[-][+][0][x[.y]]conv

Here the terms put in brackets are optional. The meaning of these terms is as follows:

term meaning
- left justify the argument
+ print a number with its sign, + or -
0 pads a number with leading zeros
x minimum field width
.y number of decimal places

The conv is a character from the following list:

conv meaning
b boolean (true/false)
d integer
e exponential notation
f floating point number
lf double precision f.p. number
o unsigned octal
s string of characters
x unsigned lower-case hexadecimal
X unsigned upper-case hexadecimal

The format string, or strings to be output, can contain special control sequences (escape sequences):

ECS sequence meaning
\' single quote
\" double quote
\\ backslash
\b backspace
\f form feed
\n new line
\r carriage return
\t horizontal tab

Formatted Input

The scanf() family of functions scans input according to format as described below. This format may contain conversion specifications. The results from such conversions, if any, are stored in the locations pointed to by the pointer arguments that follow format. Each pointer argument must be of a type that is appropriate for the value returned by the corresponding conversion specification.

int scanf(const char *format, &arg_1, &arg_2, ... , &arg_n);
int fscanf(FILE *stream, const char *format, &arg_1, &arg_2, ... , &arg_n);
int sscanf(const char *str, const char *format, &arg_1, &arg_2, ... , &arg_n);

scanf scans the standard input, fscanf scans the stream passed as the first parameter, and sscanf scans a string passed as the first parameter. The syntax for the format is the same as for the output. The output value is the number of successful conversions according to the format.

Each of the input functions ignores white space between the arguments in the input stream including the end of line characters. If you need to read a line of text, use fgets() function:

char *fgets(char *s, int size, FILE *stream);

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A '\0' is stored after the last character in the buffer.

fgets() return s on success, and NULL on error or when end of file occurs while no characters have been read. If you need to read from the standard input, the last parameter should be stdin.