Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

The output for this code printed out ‘Success’.

printf("%m\n"); 
like image 507
Manuel Avatar asked Dec 13 '13 23:12

Manuel


People also ask

What does format specifier mean?

The format specifier is used during input and output. It is a way to tell the compiler what type of data is in a variable during taking input using scanf() or printing using printf().

What is the meaning of %U in printf () statement?

An unsigned Integer means the variable can hold only a positive value. This format specifier is used within the printf() function for printing the unsigned integer variables. Syntax: printf(“%u”, variable_name);


1 Answers

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.

like image 129
ouah Avatar answered Sep 20 '22 02:09

ouah