What is the difference between %# and % in printf

  • Thread starter transgalactic
  • Start date
  • Tags
    Difference
In summary, the conversation discusses the use of different radixes in printf and the addition of a "#" sign to the specifiers. The "#" sign causes a 0 or 0x to be prepended to non-zero numbers in octal or hexadecimal basis, respectively. The purpose of this is to recreate the string necessary for creating a literal if the value is pasted back into C. The conversation also mentions that the use of trailing zeroes with the addition of "#" may vary depending on the value.
  • #1
transgalactic
1,395
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
  • #2
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.
 
  • #3
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:
  • #4
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
 
  • #5
thanks i understand know
 

What is the difference between %# and % in printf?

The %# in printf is used to specify the output format for a given value. It adds a prefix or notation to the output, such as a 0x for hexadecimal values or a 0 for octal values. The % symbol, on the other hand, is used to indicate a placeholder for a value that will be passed into the printf function. It does not add any additional formatting to the output.

When should I use %# versus % in printf?

You should use %# when you want to add a specific prefix or notation to the output of a value, such as for hexadecimal or octal values. If you do not need any additional formatting, you can simply use the % symbol as a placeholder for the value.

Can I use both %# and % in the same printf statement?

Yes, you can use both %# and % in the same printf statement. This can be useful when you want to add a prefix or notation to one value, but not to others.

What happens if I use %# or % with a value that does not require it?

If you use %# with a value that does not require any additional formatting, it will simply be ignored and the value will be printed without any prefix or notation. Similarly, if you use % with a value that does not need a placeholder, it will be printed as is.

Is there a performance difference between using %# and % in printf?

There is typically no significant performance difference between using %# and % in printf. However, adding additional formatting with %# may slightly slow down the execution of the function compared to using % alone.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
659
  • Programming and Computer Science
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
871
  • Programming and Computer Science
Replies
32
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
Replies
4
Views
631
Back
Top