This section describes miscellaneous conversions for
The `%c' conversion prints a single character. The
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.
printf ("%c%c%c%c%c", 'h', 'e', 'l', 'l', 'o');
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.
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
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.)
printf ("%p", "testing");
prints `0x' followed by a hexadecimal number--the address of the
"testing". It does not print the word
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
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.
int nchar; printf ("%d %s%n\n", 3, "bears", &nchar);
7, because `3 bears' is seven
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.