'ANSI C printf function'에 해당되는 글 1건

  1. 2021.09.10 Watcom C Library : printf Function (Format Control strings)

 

 

Watcom C Library Reference : printf Function (Format Control strings)

 

 

 

 

printf Function (Format Control strings)

 

Synopsis : #include <stdio.h> 

              int printf( const char *format, ...);

 


Description : The printf function writes output to the file designated by stdout under control of the
argument format. The format string is described below.

 

 

Returns : The printf function returns the number of characters written, or a negative value if an output error occurred. When an error has occurred, errno contains a value indicating the type of error that has been detected.

 


See Also : _bprintf, cprintf, fprintf, sprintf, _vbprintf, vcprintf, vfprintf, vprintf, vsprintf

 


Example :

#include <stdio.h>


void main( )

{
    char *weekday, *month;


    weekday = "Saturday";
    month = "April";

    printf( "%s, %s %d, %d\n", weekday, month, 18, 1999 ); 

    printf( "f1 = %8.4f  f2 = %10.2E  x = %#08x  i = %d\n", 23.45, 3141.5926, 0x1db, -1 );

}

 


produces the following :
Saturday, April 18, 1999

f1 = 23 4500  f2 = 3.14E+003  x = 0x0001db  i = -1

 

 

Format Control String: The format control string consists of ordinary characters, that are written exactly as they occur in the format string, and conversion specifiers, that cause argument values to be written as they are encountered during the processing of the format string.

 

An ordinary character in the format string is any character, other than a percent character(%), that is not part of a conversion specifier. A conversion specifier is a sequence of characters in the format string that begins with a percent character(%) and is followed, in sequence, by the following:

 • zero or more format control flags that can modify the final effect of the format directive
 • an optional decimal integer, or an asterisk character ('*'), that specifies a minimum field width to be reserved for the formatted item
 • an optional precision specification in the form of a period character (.), followed by an optional decimal integer or an asterisk character (*)
 • an optional type length specification : one of "h", "I", "L", "w", "N" or "F"
 • a character that specifies the type of conversion to be performed: one of the characters "cdeEfFgGinopsuxX".

 

 

The valid format control flags are:

"." - the formatted item is left-justified within the field; normally, items are right-justified


"+" - a signed, positive object will always start with a plus character (+); normally, only negative items begin with a sign


" " - a signed, positive object will always start with a space character; if both "+" and " " are specified, "+" overrides " "


"#" an alternate conversion form is used:

 • for "o" (unsigned octal) conversions, the precision is incremented, if necessary, so that the first digit is "0".
 • for "x" or "X" (unsigned hexadecimal) conversions, a non-zero value is prepended with "0x" or "0X" respectively
 • for "e", "E", "f", "g" or "G" (any floating-point) conversions, the result always contains a decimal-point character, even if no digits follow it; normally, a decimal-point character appears in the result only if there is a digit to follow it
 • in addition to the preceding, for "g" or "G" conversions, trailing zeros are not removed from the result

 

 

If no field width is specified, or if the value that is given is less than the number of characters in the converted value (subject to any precision value), a field of sufficient width to contain the converted value is used. If the converted value has fewer characters than are specified by the field width, the value is padded on the left (or right, subject to the left-justification flag) with spaces or zero characters ("0").

 

If the field width begins with a zero, the value is padded with zeros, otherwise the value is padded with spaces. If the field width is "*", a value of type int from the argument list is used (before a precision argument or a conversion argument) as the minimum field width. A negative field width value is interpreted as a left-justification flag, followed by a positive field width.


As with the field width specifier, a precision specifier of "*" causes a value of type int from the argument list to be used as the precision specifier. If no precision value is given, a precision of 0 is used. The precision value affects the following conversions:

 • for "d", "i", "o", "u", "x" and "X" (integer) conversions, the precision specifies the minimum number of digits to appear
 • for "e", "E" and "f" (fixed-precision, floating-point) conversions, the precision specifies the number of digits to appear after the decimal-point character
 • for "g" and "G" (variable-precision, floating-point) conversions, the precision specifies the maximum number of significant digits to appear
 • for "s" (string) conversions, the precision specifies the maximum number of characters to appear

 


A type length specifier affects the conversion as follows:

 • "h" causes a "d", "i", "o", "u", "x" or "X" (integer) format conversion to treat the argument as a short int or unsigned short int argument. Note that, although the argument may have been promoted to an int as part of the function call, the value is converted to the smaller type before it is formatted.
 • "h" causes an "f" format conversion to interpret a long argument as a fixed-point number. The value is formatted with the same rules as for floating-point values. This is a WATCOM extension.
 • "h" causes an "n" (converted length assignment) operation to assign the converted length to an object of type unsigned short int
 • "h" causes an "s" operation to treat the input string as an ASCII character string composed of 8-bit characters.
 • "l" causes a "d", "i", "o", "u", "x" or "X" (integer) conversion to process a long int or unsigned long int argument;
 • "l" causes an "n" (converted length assignment) operation to assign the converted length to an object of type unsigned long int
 • "l" or "w" cause an "s" operation to treat the input string as a wide character string (a string composed of characters of type wchar_t) or a 16-bit Unicode character string.
 • "F" causes the pointer associated with "n", "p", "s" conversions to be treated as a far pointer;
 • "L" causes an "e", "E", "f", "g", "G" (double) conversion to process a long double argument;
 • "N" causes the pointer associated with "n", "p", "s" conversions to be treated as a near pointer;

 


The valid conversion type specifiers are:

 

 Type  Meaning
 c  An argument of type int is converted to a value of type char and the corresponding ASCII character code is written to the output stream.
 d, i  An argument of type int is converted to a signed decimal notation and written to the output stream. The default precision is 1, but if more digits are required, leading zeros are added.
 e, E  An argument of type double is converted to a decimal notation in the form [-]d.ddde [ + | - ] ddd similar to FORTRAN exponential (E) notation. The leading sign appears (subject to the format control flags) only if the argument is negative.

 If the argument is non-zero, the digit before the decimal-point character is non-zero. The precision is used as the number of digits following the decimal-point character. If the precision is not specified, a default precision of six is used. If the precision is 0, the decimal-point character is suppressed.

 The value is rounded to the appropriate number of digits. For "E" conversions, the exponent begins with the character "E" rather than "e". The exponent sign and a three-digit number (that indicates the power of ten by which the decimal fraction is multiplied) are always produced.
 f  An argument of type double is converted to a decimal notation in the form [-]ddd.ddd similar to FORTRAN fixed-point (F) notation. The leading sign appears (subject to the format control flags) only if the argument is negative. 

 The precision is used as the number of digits following the decimal-point character. If the precision is not specified, a default precision of six is used. If the precision is 0, the decimal-point character is suppressed, otherwise, at least one digit is produced before the decimal-point character. The value is rounded to the appropriate number of digits. 
 g, G  An argument of type double is converted using either the "f" or "e" (or "E", for a "G" conversion) style of conversion depending on the value of the argument. In either case, the precision specifies the number of significant digits that are contained in the result. 

 "e" style conversion is used only if the exponent from such a conversion would be less than -4 or greater than the precision. Trailing zeros are removed from the result and a decimal-point character only appears if it is followed by a digit.
 n  The number of characters that have been written to the output stream is assigned to the integer pointed to by the argument. No output is produced.
 o  An argument of type int is converted to an unsigned octal notation and written to the output stream. The default precision is 1, but if more digits are required, leading zeros are added.
 p, P  An argument of type void * is converted to a value of type int and the value is formatted as for a hexadecimal ("x") conversion.
 s  Characters from the string specified by an argument of type char *, up to, but not including the terminating null character ('\0'), are written to the output stream. If a precision is specified, no more than that many characters are written.
 hs  Characters from the string specified by an argument of type char *, up to, but not including the terminating null character ('\0'), are written to the output stream. If a precision is specified, no more than that many characters are written (e.g., %.7ls).
 ls, ws  Characters from the string specified by an argument of type wchar_t *, up to, but not including the terminating null character (L'\0'), are written to the output stream. If a precision is specified, no more than that many characters are written (e.g., %.71s).
 u  An argument of type int is converted to an unsigned decimal notation and written to the output stream. The default precision is 1, but if more digits are required, leading zeros are added.
 x, X  An argument of type int is converted to an unsigned hexadecimal notation and written to the output stream. The default precision is 1, but if more digits are required, leading zeros are added.

 Hexadecimal notation uses the digits "0" through "9" and the characters "a" through "f" or "A" through "F" for "x" or "X" conversions respectively, as the hexadecimal digits. Subject to the alternate-form control flag, "0x" or "0X" is prepended to the output.

 

 

Any other conversion type specifier character, including another percent character (%), is written to the output stream with no special interpretation.


The arguments must correspond with the conversion type specifiers, left to right in the string; otherwise, indeterminate results will occur.


If the value corresponding to a floating-point specifier is infinity, or not a number(NAN), then the output will be "inf" or "-inf" for infinity, and "nan" or "-nan" for NAN's.

 

For example, a specifier of the form "%8.*f" will define a field to be at least 8 characters wide, and will get the next argument for the precision to be used in the conversion.

 

 

 

Classification : ANSI, (except for F and N modifiers)
Systems : All

 

 

 

 

 

 

 

 

This manual describes the WATCOM C library for DOS, Windows, and OS/2, It includes the Standard C Library (as defined in the ANSI C Standard).

 

WATCOM C Language Reference manual describes the ANSI C Programming language and extensions to it which are supported by WATCOM C/C++ (32bit)

 

 

 

728x90
반응형
Posted by 전화카드
,