C
Language | ||||
Standard Library | ||||
Type support | ||||
Dynamic memory management | ||||
Error handling | ||||
Program utilities | ||||
Date and time utilities | ||||
Strings library | ||||
Algorithms | ||||
Numerics | ||||
Input/output support | ||||
Localization support | ||||
Thread support (C11) | ||||
Atomic operations (C11) |
File input/output
Functions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
File access | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Direct input/output | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fread | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fwrite | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Unformatted input/output | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Formatted input/output | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
File positioning | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ftell | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fgetpos | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fseek | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fsetpos | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
rewind | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Error handling | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
clearerr | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
feof | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ferror | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
perror | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Operations on files | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
remove | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
rename | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
tmpfile | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
tmpnam |
Defined in header | ||
int printf( const char *format, ... ); | (1) | (until C99) |
int printf( const char *restrict format, ... ); | (1) | (since C99) |
int fprintf( FILE *stream, const char *format, ... ); | (2) | (until C99) |
int fprintf( FILE *restrict stream, const char *restrict format, ... ); | (2) | (since C99) |
int sprintf( char *buffer, const char *format, ... ); | (3) | (until C99) |
int sprintf( char *restrict buffer, const char *restrict format, ... ); | (3) | (since C99) |
int snprintf( char *restrict buffer, int buf_size, | (4) | (since C99) |
Loads the data from the given locations, converts them to character string equivalents and writes the results to a variety of sinks.
1) Writes the results to stdout.
2) Writes the results to a file stream stream
.
3) Writes the results to a character string buffer
.
4) Writes the results to a character string buffer
. At most buf_size
- 1 characters are written. The resulting character string will be terminated with a null character, unless buf_size
is zero.
Contents
- 1 Parameters
- 2 Return value
- 3 Example
- 4 See also
[edit] Parameters
stream | - | output file stream to write to | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
buffer | - | pointer to a character string to write to | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
buf_size | - | up to buf_size - 1 characters may be written, plus the null terminator | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
format | - | pointer to a null-terminated multibyte string specifying how to interpret the data. The format string consists of whitespace characters, non-whitespace characters (except
The following format specifiers are available:
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
... | - | arguments specifying data to print |
[edit] Return value
1-3) number of characters written if successful or negative value if an error occurred.
4) number of characters written if successful or negative value if an error occurred. If the resulting string gets truncated due to buf_size
limit, function returns the total number of characters (not including the terminating null-byte) which would have been written, if the limit was not imposed.
[edit] Example
#include <stdio.h>int main(){ printf("Strings:\n"); const char* s = "Hello"; printf("\t.%10s.\n\t.%-10s.\n\t.%*s.\n", s, s, 10, s); printf("Characters:\t%c%%\n", 65); printf("Integers\n"); printf("Decimal:\t%i%d%.6i%i%.0i%+i%u\n", 1, 2, 3, 0, 0, 4, -1); printf("Hexadecimal:\t%x%x%X%#x\n", 5, 10, 10, 6); printf("Octal:\t%o%#o%#o\n", 10, 10, 4); printf("Floating point\n"); printf("Rounding:\t%f%.0f%.32f\n", 1.5, 1.5, 1.3); printf("Padding:\t%05.2f%.2f%5.2f\n", 1.5, 1.5, 1.5); printf("Scientific:\t%E%e\n", 1.5, 1.5); printf("Hexadecimal:\t%a%A\n", 1.5, 1.5);}
Output:
Strings: . Hello. .Hello . . Hello.Characters: A%IntegersDecimal: 1 2 000003 0 +4 4294967295Hexadecimal: 5 a A 0x6Octal: 12 012 04Floating pointRounding: 1.500000 2 1.30000000000000004440892098500626Padding: 01.50 1.50 1.50Scientific: 1.500000E+00 1.500000e+00Hexadecimal: 0x1.8p+0 0X1.8P+0
[edit] See also
vprintfvfprintfvsprintfvsnprintf (C99) | prints formatted output to stdout, a file stream or a buffer using variable argument list (function) |
fputs | writes a character string to a file stream (function) |
scanffscanfsscanf | reads formatted input from stdin, a file stream or a buffer (function) |