We help IT Professionals succeed at work.

%p %u %lf

zizi21
zizi21 asked
on
399 Views
Last Modified: 2020-04-13
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 ...
Comment
Watch Question

This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
AxterSenior Software Engineer
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
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
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
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

Commented:
> 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.




AxterSenior Software Engineer
CERTIFIED EXPERT

Commented:
>>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)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.