Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
407 views
in Technique[技术] by (71.8m points)

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

The output for this code printed out ‘Success’.

printf("%m
");
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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
", filename);

is equivalent to:

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

The ‘%m’ conversion is a GNU C Library extension.

So:

printf("%m
", d);

is equivalent to

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

which is equivalent to

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...