56

The output for this code printed out ‘Success’.

printf("%m\n");
Rachid K.
  • 4,490
  • 3
  • 11
  • 30
Manuel
  • 976
  • 3
  • 9
  • 21

2 Answers2

69

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.

Community
  • 1
  • 1
ouah
  • 142,963
  • 15
  • 272
  • 331
  • 3
    I think the most interesting part is the fact that printf does *not* require an extra parameter for each `%m`. – luiscubal Dec 13 '13 at 23:15
6

Actually, the manual of printf() concerning %m is quite laconic:

m      (Glibc extension; supported by uClibc and musl.)  Print output
              of strerror(errno).  No argument is required.

But strerror() has a problem in multi-threaded programs: it is not reentrant. The thread-safe version is strerror_r().

A little study of the GLIBC implementation shows that %m is actually equivalent to strerror_r() and not to strerror(). Hence %m is thread-safe! The online manual is consequently wrong (or at least not accurate enough)!

Rachid K.
  • 4,490
  • 3
  • 11
  • 30