Previous | Contents | Index |
Remarks
scanf("%d", &n) |
scanf("%d", n) |
field = %x |
field = 5218 field=5218 field= 5218 field =5218 |
fiel d=5218 |
The format specification string for the output of information can contain:
A conversion specification consists of the following, in the order listed:
For examples of conversion specifications, see the sample programs in Section 2.6.
Table 2-4 shows the characters you can use between the percent sign (%) (or the sequence %n$) and the conversion specifier. These characters are optional, but if specified, they must occur in the order shown in Table 2-4.
Character | Meaning | ||||||||
---|---|---|---|---|---|---|---|---|---|
flags |
You can use the following flag characters, alone or in any combined
order, to modify the conversion specification:
|
||||||||
|
|||||||||
field width |
The minimum field width can be designated by a decimal integer
constant, or by an output source. To specify an output source, use an
asterisk (*) or the sequence *
n$, where
n refers to the
nth output source listed after the format specification.
The minimum field width is considered after the conversion is done according to all the other components of the format directive. This component affects the padding of the conversion result as follows: If the result of the conversion is wider than the minimum field, write it out. If the result of the conversion is narrower than the minimum width, pad it to make up the field width. Pad with spaces by default. Pad with zeros if the 0 flag is specified; this does not mean that the width is an octal number. Padding is on the left by default, and on the right if a minus sign is specified. For the wide-character output functions, the field width is measured in wide characters; for the byte output functions, it is measured in bytes. |
||||||||
. (period) | Separates the field width from the precision. | ||||||||
precision |
The precision defines any of the following:
If a precision appears with any other conversion specifier, the behavior is undefined. Precision can be designated by a decimal integer constant, or by an output source. To specify an output source, use an asterisk (*) or the sequence * n$, where n refers to the nth output source listed after the format specification. If only the period is specified, the precision is taken as 0. |
||||||||
h, l, or L (or ll) |
An h specifies that a following d, i, o, u, x, or X conversion
specifier applies to a
short int
or
unsigned short int
argument; an h can also specify that a following n conversion specifier
applies to a pointer to a
short int
argument.
An l (lowercase ell) specifies that a following d, i, o, u, x, or X conversion specifier applies to a long int or unsigned long int argument; an l can also specify that a following n conversion specifier applies to a pointer to a long int argument. On OpenVMS Alpha and I64 systems, an L or ll (two lowercase ells) specifies that a following d, i, o, u, x, or X conversion specifier applies to an __int64 or unsigned __int64 argument. (ALPHA, I64) An L specifies that a following e, E, f, g, or G conversion specifier applies to a long double argument. An l specifies that a following c or s conversion specifier applies to a wchar_t argument. If an h, l, or L appears with any other conversion specifier, the behavior is undefined. On OpenVMS VAX and OpenVMS Alpha systems, HP C int values are equivalent to long values. |
Table 2-5 describes the conversion specifiers for formatted output.
Specifier | Output Type1 | Description |
---|---|---|
d, i | Converts an int argument to signed decimal format. | |
o | Converts an unsigned int argument to unsigned octal format. | |
u | Converts an unsigned int argument to unsigned decimal format (giving a number in the range 0 to 4,294,967,295). | |
x, X | Converts an unsigned int argument to unsigned hexadecimal format (with or without a leading 0x). The letters abcdef are used for x conversion, and the letters ABCDEF are used for X conversion. | |
f |
Converts a
float
or
double
argument to the format [--]mmm.nnnnnn. The number of n's is equal to
the precision specification as follows:
The value is rounded to the appropriate number of digits. |
|
e, E | Converts a float or double argument to the format [--]m.nnnnnnE<pm symbol>xx. The number of n's is specified by the precision. If no precision is specified, the default is 6. If the precision is explicitly 0 and the # flag is specified, the decimal point appears but no n's appear. If the precision is explicitly 0 and the # flag is not specified, the decimal point also does not appear. An 'e' is printed for e conversion; an 'E' is printed for E conversion. The exponent always contains at least two digits. If the value is 0, the exponent is 0. | |
g, G | Converts a float or double argument to format f or e (or E if the G conversion specifier is used), with the precision specifying the number of significant digits. If the precision is 0, it is taken as 1. The format used depends on the value of the argument: format e (or E) is used only if the exponent resulting from such a conversion is less than --4, or is greater than or equal to the precision; otherwise, format f is used. Trailing zeros are suppressed in the fractional portion of the result. A decimal point appears only if it is followed by a digit. | |
c | Byte |
Converts an
int
argument to an
unsigned char
, and writes the resulting byte.
If the optional character l (lowercase ell) precedes this conversion specifier, then the specifier converts a wchar_t argument to an array of bytes representing the character, and writes the resulting character. If the field width is specified and the resulting character occupies fewer bytes than the field width, then it will be padded to the given width with space characters. If the precision is specified, then the behavior is undefined. |
Wide-character |
If an l (lowercase ell) does not precede the c specifier, then the
int
argument is converted to a wide character as if by calling
btowc
, and the resulting character is written.
If an l (lowercase ell) precedes the c specifier, then the specifier converts a wchar_t argument to an array of bytes representing the character, and writes the resulting character. If the field width is specified and the resulting character occupies fewer characters than the field width, it will be padded to the given width with space characters. If the precision is specified, the behavior is undefined. |
|
C | Byte | Converts a wchar_t argument to an array of bytes representing the character, and writes the resulting character. If the field width is specified and the resulting character occupies fewer bytes than the field width, then it will be padded to the given width with space characters. If the precision is specified, then the behavior is undefined. |
Wide-character | Converts a wchar_t argument to an array of bytes representing the character, and writes the resulting character. If the field width is specified and the resulting character occupies fewer wide characters than the field width, then it will be padded to the given width with space characters. If the precision is specified, then the behavior is undefined. | |
s | Byte |
Requires an argument that is a pointer to an array of characters of type
char
. The argument is used to write characters until a null character is
encountered or until the number of characters indicated by the
precision specification is exhausted. If the precision specification is
0 or omitted, then all characters up to a null are output.
If the optional character l (lowercase ell) precedes this conversion specifier, then the specifier converts an array of wide-character codes to multibyte characters, and writes the multibyte characters. Requires an argument that is a pointer to an array of wide characters of type wchar_t . Characters are written until a null wide character is encountered or until the number of bytes indicated by the precision specification is exhausted. If the precision specification is omitted or is greater than the size of the array of converted bytes, then the array of wide characters must be terminated by a null wide character. |
Wide-character |
If an l (lowercase ell) does not precede the s specifier, then the
specifier converts an array of multibyte characters, as if by calling
mbrtowc
for each multibyte character, and writes the resulting characters until
a null wide character is encountered or the number of wide characters
indicated by the precision specification is exhausted. If the precision
specification is omitted or is greater than the size of the array of
converted characters, then the converted array must be terminated by a
null wide character.
If an l precedes this conversion specifier, then the argument is a pointer to an array of wchar_t . Characters from this array are written until a null wide character is encountered or the number of wide characters indicated by the precision specification is exhausted. If the precision specification is omitted or is greater than the size of the array, then the array must be terminated by a null wide character. |
|
S | Byte | Converts an array of wide-character codes to multibyte characters, and writes the multibyte characters. Requires an argument that is a pointer to an array of wide characters of type wchar_t . Characters are written until a null wide character is encountered or until the number of bytes indicated by the precision specification is exhausted. If the precision specification is omitted or is greater than the size of the array of converted bytes, then the array of wide characters must be terminated by a null wide character. |
Wide-character | The argument is a pointer to an array of wchar_t . Characters from this array are written until a null wide character is encountered or the number of wide characters indicated by the precision specification is exhausted. If the precision specification is omitted or is greater than the size of the array, then the array must be terminated by a null wide character. | |
p | Requires an argument that is a pointer to void . The value of the pointer is output as a hexadecimal number. | |
n | Requires an argument that is a pointer to an integer. The integer is assigned the number of characters written to the output stream so far by this call to the formatted output function. No argument is converted. | |
% | Writes out the percent symbol. No conversion is performed. The complete conversion specification would be %%. |
HP C defines three file pointers that allow you to perform I/O to and from the logical devices usually associated with your terminal (for interactive jobs) or a batch stream (for batch jobs). In the OpenVMS environment, the three permanent process files SYS$INPUT, SYS$OUTPUT, and SYS$ERROR perform the same functions for both interactive and batch jobs. Terminal I/O refers to both terminal and batch stream I/O. The file pointers stdin , stdout , and stderr are defined when you include the <stdio.h> header file using the #include preprocessor directive.
The stdin file pointer is associated with the terminal to perform input. This file is equivalent to SYS$INPUT. The stdout file pointer is associated with the terminal to perform output. This file is equivalent to SYS$OUTPUT. The stderr file pointer is associated with the terminal to report run-time errors. This file is equivalent to SYS$ERROR.
There are three file descriptors that refer to the terminal. The file descriptor 0 is equivalent to SYS$INPUT, 1 is equivalent to SYS$OUTPUT, and 2 is equivalent to SYS$ERROR.
When performing I/O at the terminal, you can use Standard I/O functions and macros (specifying the pointers stdin , stdout , or stderr as arguments), you can use UNIX I/O functions (giving the corresponding file descriptor as an argument), or you can use the Terminal I/O functions and macros. There is no functional advantage to using one type of I/O over another; the Terminal I/O functions might save keystrokes since there are no arguments.
Previous | Next | Contents | Index |