putc

printf

#include <stdio.h>int printf(const char *format, ...);

The printf( ) function writes to stdout the arguments that comprise the argument list as specified by the string pointed to by format.

In C99, format is qualified with restrict.

The string pointed to by format consists of two types of items. The first type is made up of characters that will be printed on the screen. The second type contains format specifiers that define the way the arguments are displayed. A format specifier begins with a percent sign and is followed by the format code. There must be exactly the same number of arguments as there are format specifiers, and the format specifiers and the arguments are matched in order. For example, the following printf( ) call displays “Hi c 10 there!”:

printf("Hi %c %d %s", 'c', 10, "there!"); 

If there are insufficient arguments to match the format specifiers, the output is undefined. If there are more arguments than format specifiers, the remaining arguments are discarded. The format specifiers are shown here.

Code

Format

%a

Hexadecimal output in the form 0xh.hhhhp+d. (C99 only)

%A

Hexadecimal output in the form 0Xh.hhhhP+d. (C99 only)

%c

Character

%d

Signed decimal integers

%i

Signed decimal integers

%e

Scientific notation (lowercase e)

%E

Scientific notation (uppercase E)

%f

Decimal floating point

%F

Decimal floating point (C99 only; produces uppercase INF, INFINITY, or NAN when applied to infinity or a value that is not-a-number. The %f specifier produces lowercase equivalents.)

%g

Uses %e or %f, whichever is shorter

%G

Uses %E or %F, whichever is shorter

%o

Unsigned octal

%s

String of characters

%u

Unsigned decimal integers

%x

Unsigned hexadecimal (lowercase letters)

%X

Unsigned hexadecimal (uppercase letters)

%p

Displays a pointer

%n

The associated argument must be a pointer to an integer. This specifier causes the number of characters written (up to the point at which the %n is encountered) to be stored in that integer.

%%

Prints a % sign

The printf( ) function returns the number of characters actually printed. A negative return value indicates that an error has taken place.

The format codes can accept modifiers that specify the field width, precision, and left-justification. An integer placed between the % sign and the format code acts as a minimum field-width specifier. This pads the output with spaces or 0’s to ensure that it is at least a certain minimum length. If the string or number is greater than that minimum, it will be printed in full, even if it overruns the minimum. The default padding is done with spaces. If you want to pad with 0’s, place a 0 before the field-width specifier. For example, %05d will pad a number of less than 5 digits with 0’s so that its total length is 5.

The exact meaning of the precision modifier depends on the format code being modified. To add a precision modifier, place a decimal point followed by the precision after the field-width specifier. For a, A, e, E, f, and F formats, the precision modifier determines the number of decimal places printed. For example, %10.4f will display a number at least ten characters wide with four decimal places. When the precision modifier is applied to the g or G format code, it determines the maximum number of significant digits displayed. When applied to integers, the precision modifier specifies the minimum number of digits that will be displayed. Leading zeros are added, if necessary.

When the precision modifier is applied to strings, the number following the period specifies the maximum field length. For example, %5.7s will display a string that will be at least five characters long and will not exceed seven. If the string is longer than the maximum field width, the characters will be truncated off the end.

By default, all output is right-justified: if the field width is larger than the data printed, the data will be placed on the right edge of the field. You can force the information to be left-justified by putting a minus sign directly after the %. For example, %–10.2f will left-justify a floating-point number with two decimal places in a ten-character field.

There are two format modifiers that allow printf( ) to display short and long integers. These modifiers can be applied to the d, i, o, u, x, and X type specifiers. The l modifier tells printf( ) that a long data type follows. For example, %ld means that a long int is to be displayed. The h modifier tells printf( ) to display a short integer. Therefore, %hu indicates that the data is of type short unsigned int.

If you are using a modern compiler that supports the wide-character features added in 1995, then you can use the l modifier with the c specifier to indicate a wide character. You can also use the l modifier with the s format command to indicate a wide-character string.

An L modifier can prefix the floating-point commands of a, A, e, E, f, F, g, and G, and indicates that a long double follows.

The n command causes the number of characters that have been written at the time the n is encountered to be placed in an integer variable whose pointer is specified in the argument list. For example, this code fragment displays the number 14 after the line “This is a test”:

int i; printf("This is a test%n", &i); printf("%d", i);

You can apply the l modifier to the n specifier to indicate that the corresponding argument points to a long integer. You can specify the h modifier to indicate that the corresponding argument points to a short integer.

The # has a special meaning when used with some printf( ) format codes. Preceding a, A, g, G, f, e, or E with a # ensures that the decimal point will be present, even if there are no decimal digits. If you precede the x or X format code with a #, the hexadecimal number will be printed with a 0x prefix. If you precede the o format with a #, the octal value will be printed with a 0 prefix. The # cannot be applied to any other format specifiers.

The minimum field-width and precision specifiers may be provided by arguments to printf( ) instead of by constants. To accomplish this, use an * as a placeholder. When the format string is scanned, printf( ) will match each * to an argument in the order in which they occur.

Format Modifiers Added to printf( ) by C99

C99 adds several format modifiers to printf( ): hh, ll, j, z, and t. The hh modifier can be applied to d, i, o, u, x, X, or n. It specifies that the corresponding argument is a signed or unsigned char value, or in the case of n, a pointer to a signed char variable. The ll modifier also can be applied to d, i, o, u, x, X, or n. It specifies that the corresponding argument is a signed or unsigned long long int value, or in the case of n, a pointer to a long long int.

The j format modifier, which applies to d, i, o, u, x, X, or n, specifies that the matching argument is of type intmax_t or uintmax_t. These types are declared in <stdint.h> and specify greatest-width integers.

The z format modifier, which applies to d, i, o, u, x, X, or n, specifies that the matching argument is of type size_t. This type is declared in <stddef.h> and specifies the result of sizeof.

The t format modifier, which applies to d, i, o, u, x, X, or n, specifies that the matching argument is of type ptrdiff_t. This type is declared in <stddef.h> and specifies the difference between two pointers.

C99 also allows the l to be applied to the floating-point specifiers a, A, e, E, f, F, g, and G, but it has no effect.

Related functions are scanf( ) and fprintf( ).




C(s)C++ Programmer's Reference
C Programming on the IBM PC (C Programmers Reference Guide Series)
ISBN: 0673462897
EAN: 2147483647
Year: 2002
Pages: 539

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net