%p Format specifier in c

后端 未结 6 553
长情又很酷
长情又很酷 2020-12-15 08:12

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

void main() 
{
    int i=85;

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

    get         


        
相关标签:
6条回答
  • 2020-12-15 08:21

    %p is for printing an address but you need to use the ampersand i.e. & operator before i to get the address of i

    So to get the address of variable i the correct format will be:

    void main() 
    {
        int i=85;
    
        printf("%p %Fp", &i, &i);
    
        getch();   
    }
    

    if you don't use & then u will just get the value contained in i which in this case is 85 or 55 in hex

    0 讨论(0)
  • 2020-12-15 08:25

    %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

    0 讨论(0)
  • 2020-12-15 08:25

    It's purpose is to print a pointer value in an implementation defined format. The corresponding argument must be a void * value.

    And %p is used to printing the address of a pointer the addresses are depending by our system bit.

    0 讨论(0)
  • 2020-12-15 08:26

    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.

    0 讨论(0)
  • 2020-12-15 08:28

    Here is the compilation output from my machine:

    format.c:7:5: warning: format ‘%p’ expects argument of type ‘void *’, but argument 2 has type ‘int’ [-Wformat]

    format.c:7:5: warning: format ‘%F’ expects argument of type ‘double’, but argument 3 has type ‘int’ [-Wformat]

    so there are warnings but it does compile and the output is: 0x55 0.000000p

    I am surprised you aren't getting a p at the end. Are you sure code and output matches? I guess it isn't impossible for the address of i to also be 0x0...055..but something looks wrong here.

    btw: the typical usage of %p would be to print an address i.e. &i as opposed an int

    0 讨论(0)
  • 2020-12-15 08:30

    Addition to what @Myforwik said

    %p is for printing a pointer address.

    %Fp is probably used to format a FAR pointer which is of the form --> (0x1234:0x5678)

    and 85 in decimal is 55 in hexadecimal.

    I hope its okay now.

    References : http://www.lix.polytechnique.fr/~liberti/public/computing/prog/c/C/FUNCTIONS/format.html http://www.winehq.org/pipermail/wine-devel/2005-March/034390.html

    0 讨论(0)
提交回复
热议问题