/* PRINTF.C illustrates output formatting with printf. */ #include void main( void ) { char ch = 'h', *string = "computer"; int count = -9234; double fp = 251.7366; /* Display integers. */ printf( "Integer formats:\n" "\tDecimal: %d Justified: %.6d Unsigned: %u\n", count, count, count, count ); printf( "Decimal %d as:\n\tHex: %Xh C hex: 0x%x Octal: %o\n", count, count, count, count ); /* Display in different radixes. */ printf( "Digits 10 equal:\n\tHex: %i Octal: %i Decimal: %i\n", 0x10, 010, 10 ); /* Display characters. */ printf( "Characters in field:\n%10c %5c\n", ch, ch ); /* Display strings. */ printf( "Strings in field:\n%25s\n%25.4s\n", string, string ); /* Display real numbers. */ printf( "Real numbers:\n\t%f %.2f %e %E\n", fp, fp, fp, fp ); /* Display pointers. */ printf( "Address as:\n\tDefault: %p Near: %Np Far: %Fp\n", &count, (int __near *)&count, (int __far *)&count ); /* Count characters printed. */ printf( "Display to here:\n" ); printf( "1234567890123456%n78901234567890\n", &count ); printf( "\tNumber displayed: %d\n\n", count ); } /* Format Specification Fields A format specification, which consists of optional and required fields, has the following form: %[flags] [width] [.precision] [{F | N | h | l | L}]type Each field of the format specification is a single character or a number signifying a particular format option. The simplest format specification contains only the percent sign and a type character (for example, %s). The optional fields, which appear before the type character, control other aspects of the formatting. The fields in a printf format specification are described in the following list: Field Description type Required character that determines whether the associated argument is interpreted as a character, a string, or a number. (See type Field Characters.) flags Optional character or characters that control justification of output and printing of signs, blanks, decimal points, and octal and hexadecimal prefixes. (See flag Directives.) More than one flag can appear in a format specification. width Optional number that specifies minimum number of characters output. (See width Specification.) precision Optional number that specifies maximum number of characters printed for all or part of the output field, or minimum number of digits printed for integer values. (See precision Specification.) F, N Optional prefixes that refer to the "distance" to the object being printed (near or far). F and N are not part of the ANSI definition for printf. They are Microsoft extensions that should not be used if ANSI portability is desired. h, l, L Optional prefixes that determine the size of the argument expected, as shown below: Prefix Use h Used with the integer types d, i, o, x, and X to specify that the argument is short int, or with u to specify short unsigned int. If used with %p, it indicates a 16-bit pointer. l Used with d, i, o, x, and X types to specify that the argument is long int, or with u to specify long unsigned int; also used with e, E, f, g, and G types to specify double rather than float. If used with %p, it indicates a 32-bit pointer. L Used with e, E, f, g, and G types to specify long double. If a percent sign is followed by a character that has no meaning as a format field, the character is copied to stdout. For example, to print a percent-sign character, use %%. */