简体   繁体   中英

How to use %x in printf, in pointers?

I want to print last 4 printf like 62fe14 . Why did it print like 1 0 19 ?

int main() {
    int number=12, *theAdressOfTheNumber;
    float fractionalNumber=5.7, *theAdressOfFractionalNumber;
    double theBigFractionalNumber=7.8, *theAdressOfBigFractionalNumber;
    char character='K',*theAdressOfCharacter;
    
    printf("%d\n",number);
    printf("%f\n",fractionalNumber);
    printf("%lf\n",theBigFractionalNumber);
    printf("%c\n",character);
    
    printf("%x\n",theAdressOfTheNumber);                
    printf("%x\n",theAdressOfFractionalNumber);
    printf("%x\n",theAdressOfBigFractionalNumber);      
    printf("%x\n",theAdressOfCharacter);            
            
    return 0;
}
  

Assign values to the pointers:

    theAddressOfTheNumber = &number;
    theAddressOfFractionalNumber = &fractionalNumber;
    theAddressOfBigFractionalNumber = &theBigFractionalNumber;
    theAddressOfCharacter = &character;

Print them by converting them to void * and using %p :

    printf("theAddressOfTheNumber = %p.\n", (void *) theAddressOfTheNumber);
    printf("theAddressOfFractionalNumber = %p.\n", (void *) theAddressOfFractionalNumber);
    printf("theAddressOfBigFractionalNumber = %p.\n", (void *) theAddressOfBigFractionalNumber);
    printf("theAddressOfCharacter = %p.\n", (void *) theAddressOfCharacter);

Note that I corrected the spelling of “Address” (it has two of the letter d); you should adjust the rest of your code to match.

The formatting used by %p is defined by each C implementation; the C standard does not mandate a particular format. To control the format more specifically, include <stdint.h> and <inttypes.h> , convert to uintptr_t , and print with "%" PRIxPTR :

    printf("theAddressOfTheNumber = %" PRIxPTR ".\n", (uintptr_t) theAddressOfTheNumber);
    printf("theAddressOfFractionalNumber = %" PRIxPTR ".\n", (uintptr_t) theAddressOfFractionalNumber);
    printf("theAddressOfBigFractionalNumber = %" PRIxPTR ".\n", (uintptr_t) theAddressOfBigFractionalNumber);
    printf("theAddressOfCharacter = %" PRIxPTR ".\n", (uintptr_t) theAddressOfCharacter);

uintptr_t is an unsigned integer type that can hold all the data required to represent an address. When formatting it, you can use the ordinary modifiers for formatting integer types, such as using "%016" PRxPTR to request it be formatted with leading zeros and at least 16 digits. However, how a pointer is converted to uintptr_t is also defined by each C implementation. It will usually be an ordinary conversion that makes sense for the architecture, so it may look just like the address printed with %p , but this is not required by the C standard.

Note that the address of different variables might or might not have any apparent relationship to each other. Compilers are free to allocate memory for them as they please. They can be in order by declaration, in order by alignment considerations, in order by alphabetical order of name, in order by effectively random hash values, or combinations of these.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM