![]() |
C - Entwicklung Stream-IO: printf und scanf Homepage von PS-Trainer - C-Entwicklung - IO-Stream - an PS-Trainer |
|
| StreamIO | Data Stream Input & Output |
| printf, wprintf | Print formatted output to the standard output stream. |
| scanf, wscanf
|
Read formatted data from the standard input stream. |
| Format specification | Format control string for printf and scanf functions |
| printf, wprintf Print formatted output to the standard output stream. int printf( const char *format [, argument]... ); int wprintf( const wchar_t *format [, argument]... );
Remarks The printf function formats and prints a series of characters and values to the standard output stream, stdout. If arguments follow the format string, the format string must contain specifications that determine the output format for the arguments. printf and fprintf behave identically except that printf writes output to stdout rather than to a destination of type FILE. wprintf is a wide-character version of printf; format is a wide-character string. wprintf and printf behave identically otherwise. Generic-Text Routine Mappings
Subject: Floating-Point Support Routines, Stream I/O Routines, Locale Routines Keywords: See also fopen, fprintf, scanf, sprintf, vprintf Functions |
|||||||||||||||||||||
| Return Value Each of these functions returns the number of characters printed, or a negative value if an error occurs. Parameters
The format argument consists of ordinary characters, escape sequences, and (if arguments follow format) format specifications. The ordinary characters and escape sequences are copied to stdout in order of their appearance. For example, the line printf("Line one\n\t\tLine two\n"); produces the output Line one Line two Format specifications always begin with a percent sign (%) and are read left to right. When printf encounters the first format specification (if any), it converts the value of the first argument after format and outputs it accordingly. The second format specification causes the second argument to be converted and output, and so on. If there are more arguments than there are format specifications, the extra arguments are ignored. The results are undefined if there are not enough arguments for all the format specifications. Details: Format control strings |
|||||||||||||||||||||
| Example /* PRINTF.C: This program uses the printf and wprintf functions * to produce formatted output. */ #include <stdio.h> void main( void ) /* Display integers. */ printf( "Decimal %d as:\n\tHex:
%Xh C hex: 0x%x Octal: %o\n", /* Display in different radixes.
*/ /* Display characters. */ printf("Characters in
field (1):\n%10c%5hc%5C%5lc\n", ch, ch, wch, wch); /* Display strings. */ printf("Strings in field
(1):\n%25s\n%25.4hs\n\t%S%25.3ls\n", /* Display real numbers. */ /* Display pointer. */ /* Count characters printed.
*/ Address as: 0012FFAC Display to
here: |
| scanf, wscanf Read formatted data from the standard input stream. int scanf( const char *format [,argument]... ); int wscanf( const wchar_t *format [,argument]... );
Remarks The scanf function reads data from the standard input stream stdin and writes the data into the location given by argument. Each argument must be a pointer to a variable of a type that corresponds to a type specifier in format. If copying takes place between strings that overlap, the behavior is undefined. wscanf is a wide-character version of scanf; the format argument to wscanf is a wide-character string. wscanf and scanf behave identically otherwise. Generic-Text Routine Mappings
For more information, see Format Specification Fields scanf functions and wscanf Functions. Subject: Floating-Point Support Routines, Stream I/O Routines, Locale Routines Kexwords: See also fscanf, printf, sprintf, sscanf |
|||||||||||||||||||||
| Return Value Both scanf and wscanf return the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned. The return value is EOF for an error or if the end-of-file character or the end-of-string character is encountered in the first attempt to read a character. Parameters
|
|||||||||||||||||||||
| Example /* SCANF.C: This program uses the scanf and wscanf functions * to read formatted input. */ #include <stdio.h> void main( void ) printf( "\n\nEnter an int, a float, two chars and two strings\n"); result = scanf( "%d %f
%c %C %s %S", &i, &fp, &c, &wc, s, ws ); wprintf( L"\n\nEnter an int, a float, two chars and two strings\n"); result = wscanf( L"%d
%f %hc %lc %S %ls", &i, &fp, &c, &wc, s, ws ); The number
of fields input is 6 Enter an int,
a float, two chars and two strings The number
of fields input is 6 |
|
Format Specification Fields: |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
printf
Format: Type Field Characters
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
printf
Format: Flag Characters
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
printf
Format: How Precision Values Affect
Type
If the argument corresponding to a floating-point specifier is infinite, indefinite, or NaN, printf gives the following output.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
printf Format: Size Prefixes
for printf and wprintf Format-Type Specifiers
Thus to print single-byte or wide-characters with printf functions and wprintf functions, use format specifiers as follows.
To print strings with printf functions and wprintf functions, use the prefixes h and l analogously with format type-specifiers s and S. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| printf Format: Width Specification The second optional field of the format specification is the width specification. The width argument is a nonnegative decimal integer controlling the minimum number of characters printed. If the number of characters in the output value is less than the specified width, blanks are added to the left or the right of the values depending on whether the flag (for left alignment) is specified until the minimum width is reached. If width is prefixed with 0, zeros are added until the minimum width is reached (not useful for left-aligned numbers). The width specification never causes a value to be truncated. If the number of characters in the output value is greater than the specified width, or if width is not given, all characters of the value are printed (subject to the precision specification). If the width specification is an asterisk (*), an int argument from the argument list supplies the value. The width argument must precede the value being formatted in the argument list. A nonexistent or small field width does not cause the truncation of a field; if the result of a conversion is wider than the field width, the field expands to contain the conversion result. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Format Specification Fields:
scanf and wscanf Functions A format specification has the following form: %[*] [width] [{h | l | I64 | L}]type The format argument specifies the interpretation of the input and can contain one or more of the following: A white-space character causes scanf to read, but not store, all consecutive white-space characters in the input up to the next nonwhite-space character. One white-space character in the format matches any number (including 0) and combination of white-space characters in the input. A nonwhite-space character causes scanf to read, but not store, a matching nonwhite-space character. If the next character in stdin does not match, scanf terminates. A format specification causes scanf to read and convert characters in the input into values of a specified type. The value is assigned to an argument in the argument list. The format is read from left to right. Characters outside format specifications are expected to match the sequence of characters in stdin; the matching characters in stdin are scanned but not stored. If a character in stdin conflicts with the format specification, scanf terminates, and the character is left in stdin as if it had not been read. When the first format specification is encountered, the value of the first input field is converted according to this specification and stored in the location that is specified by the first argument. The second format specification causes the second input field to be converted and stored in the second argument, and so on through the end of the format string. An input field is defined as all characters up to the first white-space character (space, tab, or newline), or up to the first character that cannot be converted according to the format specification, or until the field width (if specified) is reached. If there are too many arguments for the given specifications, the extra arguments are evaluated but ignored. The results are unpredictable if there are not enough arguments for the format specification. Each field of the format specification is a single character or a number signifying a particular format option. The type character, which appears after the last optional format field, determines whether the input field is interpreted as a character, a string, or a number. The simplest format specification contains only the percent sign and a type character (for example, %s). If a percent sign (%) is followed by a character that has no meaning as a format-control character, that character and the following characters (up to the next percent sign) are treated as an ordinary sequence of characters, that is, a sequence of characters that must match the input. For example, to specify that a percent-sign character is to be input, use %%. An asterisk (*) following the percent sign suppresses assignment of the next input field, which is interpreted as a field of the specified type. The field is scanned but not stored. Keywords: See also printf Format specification. |
| Aktuelle Daten dieser Seite | Letzte Änderung: |
| |