Link to home
Start Free TrialLog in
Avatar of zizi21
zizi21

asked on

%p %u %lf

Hi,
I have read that %p is usually for addresses, but I have some places where %u is also used ? Is thre any difference ..( is this true ?)
So, %lf is for double..but sometimes, when you compile, you get error not ansi 90
it is compiled using
gcc -ansi -Wall -pedantic...

pls advice..thanks ...
ASKER CERTIFIED SOLUTION
Avatar of digitalseraphim
digitalseraphim

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of zizi21
zizi21

ASKER

hi,
%p is the same as %#x, which prints the value as 0x[a-z0-9]...
what does 0x[a-z0-9] print for ? is it addresses ...
thanks
%p does void pointer values (addresses) in lowercase hex notation (0xaabb2233 instead of 0XAABB2233).
%#x does 32 bit integers in lowercase hex notation with 0x
%#X does 32 bit integers in uppercase hex notation with 0X

if you remove the # from the above, it will remove the 0[xX] from the output

-ds
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
While I _have_ used %x for pointers in the past, I was more making the statement that the output _looks_ similar between %p and %#x.  I used %x when I didn't know about %p, and I used 0x%x when I didn't know about %#x.  But, at the same time, I used %x because I _knew_ that the pointer was the same number of bits, in the same order, as an unsigned int.  but the basis of comparing %p to %#x was simply the similarity in their output.  In reality, on a machine with 64 bit pointers, %p is actually like %#lx.  

Also of note, %p is described as formatting like %#x in the printf(3) manpage:

http://www.hmug.org/man/3/printf.php
> In reality, on a machine with 64 bit pointers, %p is actually like %#lx.  

You're still showing quite a bit of flat integer linear address myopia.

There have been and still are a *few* computers where pointers are NOTHING like integers,  addresses do not count linearly upwards, and each byte or word of the pointer has a radically different semantic than its neighbors.  For instance the x86 CPU has a 16:16 bit mode where the first sixteen bits is a 12-bit segment index into another table, then a 4-bit access rights code.  Then the bottom sixteen bits are an index that can range from 0 to some limit specified in the segment table.   No way to make sense of it as a %lu.  Marginally intelligible as a a %8X.




>>While I _have_ used %x for pointers in the past, I was more making the statement that the output _looks_ similar between %p and %#x.

The important part is not what are the similarities, but what are the differences.
Your program could crash if you're using the wrong type, and your implementation has a different size for the integer from a pointer.
Moreover, just because the code works with your current compiler, doesn't mean that it will not fail in a future version of the same compiler.

So even though the output looks the same, you should still use the correct format for the targeted type.
We're all arguing the same points, namely:

 %p is for pointers (arg is void*) and the output looks like 0xaabb1122
 %x/%X/%#x/%#X is for unsigned int in various hex formats (aabb1122, AABB1122, 0xaabb1122, 0XAABB1122)
 and %u is for unsigned int in decimal (base-10) format ; 2864386338



also that %lf should not be used.  %f is for doubles %Lf is for long doubles (where supported)