COHERENT manpages
This page displays the COHERENT manpage for printf() [Print formatted text].
List of available manpages
Index
printf() -- STDIO Function (libc) Print formatted text #include <stdio.h> int printf(format [,arg1, .... argN]) char *format; [data type] arg1, ... argN; printf() prints formatted text. It uses the format string to specify an output format for each arg, which it then writes on the standard output. printf() reads characters from format one at a time; any character other than a percent sign `%' or a string that is introduced with a percent sign is copied directly to the output. A `%' tells printf() that what follows specifies how the corresponding arg is to be formatted; the characters that follow `%' can set the output width and the type of conversion desired. The following modifiers, in this order, may precede the conversion type: 1. A minus sign `-' left-justifies the output field, instead of the default right justify. 2. A string of digits gives the width of the output field. Normally, printf() pads the field with spaces to the field width; it is padded on the left unless left justification is specified with a `-'. If the field width begins with `0', the field is padded with `0' characters instead of spaces; the `0' does not cause the field width to be taken as an octal number. Note that this applies only to numeric string descriptors. If the field descriptor describes a character or string (i.e., %c or %s), printf() ignores a leading `0' and always pads the field with spaces. If the width specification is an asterisk `*', the routine uses the next arg as an integer that gives the width of the field. 3. A period `.' followed by one or more digits gives the precision. For floating point (e, f, and g) conversions, the precision sets the number of digits printed after the decimal point. For string (s) conversions, the precision sets the maximum number of characters that can be used from the string. If the precision specification is given as an asterisk `*', the routine uses the next arg as an integer that gives the precision. 4. The letter `l' before any integer conversion (d, o, x, or u) indicates that the argument is a long rather than an int. Capitalizing the conversion type has the same effect; note, however, that capitalized conversion types are not compatible with all C compiler libraries, or with the ANSI standard. This feature will not be supported in future editions of COHERENT. The following format conversions are recognized: % Print a `%' character. No arguments are processed. c Print the int argument as a character. d Print the int argument as signed decimal numerals. e Print the float or double argument in exponential form. The format is d.ddddddesdd, where there is always one digit before the decimal point and as many as the precision digits after it (default, six). The exponent sign s may be either `+' or `-'. f Print the float or double argument as a string with an optional leading minus sign `-', at least one decimal digit, a decimal point (`.'), and optional decimal digits after the decimal point. The number of digits after the decimal point is the precision (default, six). g Print the float or double argument as whichever of the formats d, e, or f loses no significant precision and takes the least space. ld Print the long argument as signed decimal numerals. lo Print the long argument in unsigned octal numerals. lu Print the long argument in unsigned decimal numerals. lx Print the long argument in unsigned hexadecimal numerals. o Print the int argument in unsigned octal numerals. p The ANSI standard states that the behavior of the %p descriptor is implementation-specific. Under COHERENT, %p prints in format %#.8X the literal value of a pointer. Its corresponding variable must be of type char *. r The next argument points to an array of new arguments that may be used recursively. The first argument of the list is a char * that contains a new format string. When the list is exhausted, the routine continues from where it left off in the original format string. This descriptor is not part of the ANSI Standard. Its use is deprecated. Code that uses it may not be portable to other systems. s Print the string to which the char * argument points. Reaching either the end of the string, indicated by a null character, or the specified precision, will terminate output. If no precision is given, only the end of the string will terminate. u Print the int argument in unsigned decimal numerals. x Print the int argument in unsigned hexadecimal numerals. The digits are prefaced by the string 0x. X Like %x, except that the digits are prefaced by the string 0X. Note COHERENT release 4.2 has changed the means of %X to conform to the ANSI C standard. In versions prior to release 4.2, this format conversion printed a long argument in unsigned hexadecimal numerals. Programs that depend upon the obsolete use of %X will no long work the same under the current release of COHERENT. If it wrote the formatted string correctly, printf() returns the number of characters written. Otherwise, it returns a negative number. Example This example implements a mini-interpreter for printf() statements. It is a convenient tool for seeing exactly how some of the printf() options work. To use it, type a printf() conversion specification at the prompt. The formatted string will then appear. To reuse a format identifier, simply type <return>: #include <math.h> #include <stddef.h> #include <stdio.h> #include <stdlib.h> #include <string.h> /* the replies go here */ static char reply[80]; /* ask for a string and echo it in reply. */ char *askstr(msg) char *msg; { printf("Enter %s ", msg); fflush(stdout); if (gets(reply) == NULL) exit(EXIT_SUCCESS); return (reply); } main() { char fid[80], c; /* initialize to an invalid format identifier */ strcpy(fid, "%Z"); for (;;) { askstr("format identifier"); /* null reply uses previous FID */ if (reply[0]) /* leave the '%' */ strcpy(fid + 1, reply); switch(c = fid[strlen(fid) - 1]) { case 'd': case 'i': askstr("signed number"); if(strchr(fid, 'l') != NULL) printf(fid, atol(reply)); else printf(fid, atoi(reply)); break; case 'o': case 'u': case 'x': case 'X': askstr("unsigned number"); if(strchr(fid, 'l') != NULL) printf(fid, atol(reply)); else printf(fid, (unsigned)atol(reply)); break; case 'f': case 'e': case 'E': case 'g': case 'G': printf(fid, atof(askstr("real number"))); break; case 's': printf(fid, askstr("string")); break; case 'c': printf(fid, *askstr("single character")); break; case '%': printf(fid); break; case 'p': /* print pointer to format id */ printf(fid, fid); break; case 'n': printf("n not implemented"); break; default: printf("%c not valid", c); } printf("\n"); } } See Also ecvt(), fcvt(), fprintf(), gcvt(), libc, putc(), puts(), scanf(), sprintf(), vprintf() ANSI Standard, §7.9.6.3 POSIX Standard, §8.1 Notes Because C does not perform type checking, it is essential that each argument match its counterpart in the format string. Versions of COHERENT prior to release 4.2 recognized the conversion formats %D, %O, and %U. The ANSI standard does not recognize these conversion characters, and beginning with release 4.2 the COHERENT implementation of printf() no longer recognizes them. You should instead use, respectively, the conversion characters %ld, %lo, and %lu.