Previous | Table of Contents | Next |
3.10.1.2. Input and Output Operations
getchar, getc, fgetc
int getchar() int getc(FILE *fp) int fgetc(FILE *fp)
getc returns the next available character from the stream fp. It returns the special value EOF to indicate end-of-file; the EOF value is distinct from all normal character values, which is why getcs return value has type int rather than char, and must be stored in a variable of type int. getchar() is equivalent to getc(stdin). fgetc is equivalent to getc except that getc may be implemented as a preprocessor macro (see section 3.8.2) and may therefore be unsafe if invoked with side effects on its fp argument.
putchar, putc, fputc
int putchar(int c) int putc(int c, FILE *fp) int fputc(int c, FILE *fp)
putc outputs the character c to the stream fp. putchar(c) is equivalent to putc(c, stdout). fputc is equivalent to putc except that putc may be implemented as a preprocessor macro (see section 3.8.2) and may therefore be unsafe if invoked with side effects on its fp argument. (Both putc and putchar are safe, however, in the presence of side effects on their c argument.) All three functions return the character written, or EOF in the event of an error (although write errors are usually not immediately detected, due to buffering).
printf, fprintf
int printf(const char *fmt, ) int fprintf(FILE *fp, const char *fmt, )
fprintf prints formatted output to the stream fp, under control of its fmt string. The fmt string contains either characters to be written to the output directly, or format specifiers that indicate that one of the variable arguments is to have its value formatted and inserted into the output.
A format specifier consists of the percent sign character %, followed by optional flags, width, precision, and size modifiers, ending in a character representing the overall conversion to be performed. The complete specification of all allowable printf format specifiers and modifiers is extensive; the presentation here is somewhat simplified. See a complete C reference for all the details.
The flag characters and their meanings are as follow:
- | The formatted value should be left justified within its field width. (The default is right justification.) |
+ | Positive values are printed with an explicit sign. |
space | If the value is positive, an extra space is prepended (so that a column of numbers lines up if some are negative). |
0 | The value should be padded out to its field width with 0s rather than spaces. |
# | An alternative style of formatting is used, as described under the individual format character specifications later. |
The field width indicates the minimum width of the formatted field; it is padded if necessary. (A larger-than-expected value, however, effectively widens the field.) The width is either a string of digits, or a * character, indicating that the width will be taken (as an int value) from the variable argument list.
The precision indicates the number of digits past the decimal that are printed for floating-point fields, or the maximum number of characters copied and printed from the string for string fields, or the minimum number of digits printed for integer fields. The precision begins with a period, and is either a string of digits or a * character, indicating that the precision will be taken (as an int value) from the variable argument list. For styles e, f, and g, the default precision is 6.
The type size modifier is an additional character optionally appearing just before the overall formatting character, as follows:
h | The corresponding integer argument should be interpreted as short int or unsigned short int. |
l | The corresponding integer argument is long int or unsigned long int. (See also NA1 note under %c and %s.) |
L | The corresponding floating-point argument is long double. |
Previous | Table of Contents | Next |