What's the meaning of the %m formatting specifier?

Manuel picture Manuel · Dec 14, 2013 · Viewed 19.7k times · Source

The output for this code printed out ‘Success’.

printf("%m\n");

Answer

ouah picture ouah · Dec 14, 2013

m conversion specifier is not C but is a GNU extension to printf:

From GNU documentation:

http://www.gnu.org/software/libc/manual/html_node/Other-Output-Conversions.html

The ‘%m’ conversion prints the string corresponding to the error code in errno. See 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.

So:

printf("%m\n", d);

is equivalent to

printf("%s\n", strerror (errno), d);

which is equivalent to

printf("%s\n", strerror (errno));

Note that %mdoes not require an argument. Here printf("%m\n", d) and printf("%s\n", strerror (errno), d) have more arguments than required: with printf if there are extra trailing arguments, they are just evaluated and ignored.