What is the difference between %# and % in printf

  • Thread starter Thread starter transgalactic
  • Start date Start date
  • Tags Tags
    Difference
Click For Summary

Discussion Overview

The discussion revolves around the use of the format specifiers in the printf function in C, specifically focusing on the difference between using the '#' flag and not using it in relation to different numeric bases (decimal, hexadecimal, and octal). The scope includes technical explanations and clarifications regarding formatting behavior.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • Some participants explain that the '#' flag in printf alters the output format by prepending '0', '0x', or '0X' to octal and hexadecimal numbers, respectively.
  • One participant questions why the '#' flag adds '0x' for hexadecimal but only '0' for octal, seeking clarification on the behavior of these format specifiers.
  • Another participant mentions that prepending '0' indicates an octal literal, while '0x' indicates a hexadecimal literal, but expresses uncertainty about the behavior of 'x' without a leading zero.
  • There is a mention of the treatment of trailing zeros in floating-point formats, with a participant asking for clarification on how they are handled with the 'g' specifier.
  • One participant expresses a personal view that the '#' flag is not commonly used and suggests that they typically prepend '0x' manually when needed.

Areas of Agreement / Disagreement

Participants express varying levels of understanding and uncertainty regarding the behavior of the '#' flag in printf, with no consensus reached on the specific reasons behind the formatting differences for octal and hexadecimal outputs.

Contextual Notes

Some participants express confusion about specific behaviors and terminology related to format specifiers, indicating that further exploration or experimentation may be necessary to fully understand the implications of using the '#' flag.

transgalactic
Messages
1,386
Reaction score
0
printf ("Some different radixes: %d %x %o %#x %#o \n", 100, 100, 100, 100, 100);

%x shows 100 in hexadecimal basis

64

but %#x makes some memory address out of it

0x64what does the addition of # ?
 
Technology news on Phys.org
http://en.wikipedia.org/wiki/printf

"#": 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.
 
regarding this
"For 'o', 'x', and 'X', a 0, 0x, and 0X, respectively, is prepended to non-zero numbers."

X turns to hex basis and O to octal basis
i was told that when we add # it add 0X to the transformed basis number in hex basis
but in octal basis it adds 0
why??

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

for what values we have different added signs?

g is a small float number

the trail of zeros you mean
0.765000000 >>0.765

??
 
Last edited:
Prepending 0 -- like 0123 -- indeed causes a literal value to use octal basis.

Prepending 0x -- like 0x0123 -- causes a literal value to use hex basis.

As far as I know prepending x (x0123?) doesn't do anything. I could be wrong.

I assume the reason they prepend 0 in octal and 0x in hex is because in both cases they're trying to reproduce the string necessary in order to create a literal if you paste the value back into C directly.

I wouldn't worry too much about the %# badge, I don't think many people use it. I've personally actually never used it before in my life, when I want to prepend 0x I just, you know, prepend 0x...

I don't know exactly what they mean by "trailing zeroes". I'd suggest just trying it and seeing what happens if you're really curious.

for what values we have different added signs?
Don't understand the question
 
thanks i understand know
 

Similar threads

Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
3
Views
3K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 32 ·
2
Replies
32
Views
2K