Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

%p Format specifier in c

How are the specifiers %p and %Fp working in the following code?

void main() 
{
    int i=85;

    printf("%p %Fp",i,i);

    getch();   
}

I am getting the o/p as 0000000000000055 0000000000000055

like image 506
poorvank Avatar asked Sep 28 '12 03:09

poorvank


People also ask

What is %P and %U in C?

Difference between %p and %x in C/C++The %p is used to print the pointer value, and %x is used to print hexadecimal values. Though pointers can also be displayed using %u, or %x. If we want to print some value using %p and %x then we will not feel any major differences.

How does %P work in printf?

By using '%p' you print the address of the variable in question, "The void * pointer argument is printed in hexadecimal (as if by %#x or %#lx)."

What is %p format string?

%p expects the argument to be of type (void *) and prints out the address. Whereas %x converts an unsigned int to unsigned hexadecimal and prints out the result.

What is the difference between %D and %P in C?

As you already explained %d is used for signed integers, while %u is for unsigned integers. %p is used to print pointer addresses. Originally Answered: What are the actual differences between %d, %u and %p format specifiers used in C to print the memory address of a variable?


2 Answers

If this is what you are asking, %p and %Fp print out a pointer, specifically the address to which the pointer refers, and since it is printing out a part of your computer's architecture, it does so in Hexadecimal.

In C, you can cast between a pointer and an int, since a pointer is just a 32-bit or 64-bit number (depending on machine architecture) referring to the aforementioned chunk of memory.

And of course, 55 in hex is 85 in decimal.

like image 166
David Christo Avatar answered Oct 19 '22 02:10

David Christo


%p is for printing a pointer address.

85 in decimal is 55 in hexadecimal.

On your system pointers are 64bit, so the full hexidecimal representation is: 0000000000000055

like image 41
Myforwik Avatar answered Oct 19 '22 00:10

Myforwik