Go to the first, previous, next, last section, table of contents.


Other Output Conversions

This section describes miscellaneous conversions for printf.

The `%c' conversion prints a single character. The int argument is first converted to an unsigned char. The `-' flag can be used to specify left-justification in the field, but no other flags are defined, and no precision or type modifier can be given. For example:

printf ("%c%c%c%c%c", 'h', 'e', 'l', 'l', 'o');

prints `hello'.

The `%s' conversion prints a string. The corresponding argument must be of type char * (or const char *). A precision can be specified to indicate the maximum number of characters to write; otherwise characters in the string up to but not including the terminating null character are written to the output stream. The `-' flag can be used to specify left-justification in the field, but no other flags or type modifiers are defined for this conversion. For example:

printf ("%3s%-6s", "no", "where");

prints ` nowhere '.

If you accidentally pass a null pointer as the argument for a `%s' conversion, the GNU library prints it as `(null)'. We think this is more useful than crashing. But it's not good practice to pass a null argument intentionally.

The `%m' conversion prints the string corresponding to the error code in errno. See section Error Messages. Thus:

fprintf (stderr, "can't open `%s': %m\n", filename);

is equivalent to:

fprintf (stderr, "can't open `%s': %s\n", filename, strerror (errno));

The `%m' conversion is a GNU C library extension.

The `%p' conversion prints a pointer value. The corresponding argument must be of type void *. In practice, you can use any type of pointer.

In the GNU system, non-null pointers are printed as unsigned integers, as if a `%#x' conversion were used. Null pointers print as `(nil)'. (Pointers might print differently in other systems.)

For example:

printf ("%p", "testing");

prints `0x' followed by a hexadecimal number--the address of the string constant "testing". It does not print the word `testing'.

You can supply the `-' flag with the `%p' conversion to specify left-justification, but no other flags, precision, or type modifiers are defined.

The `%n' conversion is unlike any of the other output conversions. It uses an argument which must be a pointer to an int, but instead of printing anything it stores the number of characters printed so far by this call at that location. The `h' and `l' type modifiers are permitted to specify that the argument is of type short int * or long int * instead of int *, but no flags, field width, or precision are permitted.

For example,

int nchar;
printf ("%d %s%n\n", 3, "bears", &nchar);

prints:

3 bears

and sets nchar to 7, because `3 bears' is seven characters.

The `%%' conversion prints a literal `%' character. This conversion doesn't use an argument, and no flags, field width, precision, or type modifiers are permitted.


Go to the first, previous, next, last section, table of contents.