Question

I was reading a multiplication table program source code and I encounter # sign in all of his printf functions that were printing decimal.

I played with the source code to see if it makes any difference, but everything seemed the same to me. what does # mean and why would he use it in printf function?

printf ("%#2d |", i);

Edit: I wanted to know why would this guy use # in printf() function for his multiplication table when he doesn't even need it.

http://en.kioskea.net/faq/7392-multiplication-table-as-c-table#note

Everyone copy pasted from somewhere. not an answer to my question.

Was it helpful?

Solution 2

Character

#

Description

Alternate form. For 'g' and 'G', trailing zeros are not removed. For 'f', 'F', 'e', 'E', 'g', 'G', the output always contains a decimal point. For 'o', 'x', and 'X', a 0, 0x, and 0X, respectively, is prepended to non-zero numbers.

Source: http://en.wikipedia.org/wiki/Printf_format_string#Format_placeholders

EDIT

To answer one of your other questions:

edit: i wanted to know why would this guy use # in printf() function for his multiplication table when he doesnt even need it.

Good question. gcc gives a warning:

warning: '#' flag used with ‘%d’ gnu_printf format [-Wformat=]

You might have to ask the person who wrote the script, since we are not able to know what they were thinking when they wrote it, and we can't give you a definitive answer just guess.

OTHER TIPS

From http://www.cplusplus.com/reference/cstdio/printf/?kw=printf

Used with o, x or X specifiers the value is preceeded with 0, 0x or 0X respectively for values different than zero.

Used with a, A, e, E, f, F, g or G it forces the written output to contain a decimal point even if no more digits follow. By default, if no digits follow, no decimal point is written.

It doesn't say anything about what what happens with d specifier.

Seems to add conversional prefix or suffix.
Here is an example from wikipedia:

printf("Color %s, number1 %d, number2 %05d, hex %#x, float %5.2f, unsigned value %u.\n", "red", 123456, 89, 255, 3.14159, 250); 

will print the following line (including new-line character, \n):

Color red, number1 123456, number2 00089, hex 0xff, float  3.14, unsigned value 250. 

you can also refer to 'man 3 printf', the following is an snapshot from it
# The value should be converted to an "alternate form". For o conversions, the first character of the output string is made zero (by prefixing a 0 if it was not zero already). For x and X conversions, a nonzero result has the string "0x" (or "0X" for X conversions) prepended to it. For a, A, e, E, f, F, g, and G conversions, the result will always contain a decimal point, even if no digits follow it (normally, a decimal point appears in the results of those conversions only if a digit follows). For g and G conversions, trailing zeros are not removed from the result as they would otherwise be. For other conversions, the result is undefined.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top