How to print out the memory contents of a variable in C?

后端 未结 9 684
滥情空心
滥情空心 2020-12-13 14:48

Suppose I do a

double d = 234.5;

I want to see the memory contents of d [the whole 8 bytes]

How do I do that?

相关标签:
9条回答
  • 2020-12-13 15:26

    If you want to print the double values in bits try this. I have tried for float value. If you changed that you can be able to view the double value in 64 bits.

    #include <stdio.h>
    
    int main (void)
    {
            float f = 10.0f;
    
            struct Float {
                    unsigned char bit01:1;
                    unsigned char bit02:1;
                    unsigned char bit03:1;
                    unsigned char bit04:1;
                    unsigned char bit05:1;
                    unsigned char bit06:1;
                    unsigned char bit07:1;
                    unsigned char bit08:1;
                    unsigned char bit09:1;
                    unsigned char bit10:1;
                    unsigned char bit11:1;
                    unsigned char bit12:1;
                    unsigned char bit13:1;
                    unsigned char bit14:1;
                    unsigned char bit15:1;
                    unsigned char bit16:1;
                    unsigned char bit17:1;
                    unsigned char bit18:1;
                    unsigned char bit19:1;
                    unsigned char bit20:1;
                    unsigned char bit21:1;
                    unsigned char bit22:1;
                    unsigned char bit23:1;
                    unsigned char bit24:1;
                    unsigned char bit25:1;
                    unsigned char bit26:1;
                    unsigned char bit27:1;
                    unsigned char bit28:1;
                    unsigned char bit29:1;
                    unsigned char bit30:1;
                    unsigned char bit31:1;
                    unsigned char bit32:1;
            };
    
            struct Float *F;
    
            F = (struct Float *) &f;
    
            printf("\nMSB -->1 bit for sign bit; 8 bit for exponent; 23 bit for mantisa<-- LSB\n");
            printf("%d ", F->bit32);
            printf("%d", F->bit31);
            printf("%d", F->bit30);
            printf("%d", F->bit29);
            printf("%d", F->bit28);
            printf("%d", F->bit27);
            printf("%d", F->bit26);
            printf("%d", F->bit25);
            printf("%d ", F->bit24);
            printf("%d", F->bit23);
            printf("%d", F->bit22);
            printf("%d", F->bit21);
            printf("%d", F->bit20);
            printf("%d", F->bit19);
            printf("%d", F->bit18);
            printf("%d", F->bit17);
            printf("%d", F->bit16);
            printf("%d", F->bit15);
            printf("%d", F->bit14);
            printf("%d", F->bit13);
            printf("%d", F->bit12);
            printf("%d", F->bit11);
            printf("%d", F->bit10);
            printf("%d", F->bit09);
            printf("%d", F->bit08);
            printf("%d", F->bit07);
            printf("%d", F->bit06);
            printf("%d", F->bit05);
            printf("%d", F->bit04);
            printf("%d", F->bit03);
            printf("%d", F->bit02);
            printf("%d\n", F->bit01);
    }
    
    0 讨论(0)
  • 2020-12-13 15:27
    double d = 234.5;
    
    /* 1. use a union */
    union u {
        double d;
        unsigned char c[sizeof(double)];
    };
    union u tmp;
    size_t i;
    tmp.d = d;
    for (i=0; i < sizeof(double); ++i)
        printf("%02x\n", tmp.c[i]);
    
    /* 2. memcpy */
    unsigned char data[sizeof d];
    size_t i;
    memcpy(data, &d, sizeof d);
    for (i=0; i < sizeof d; ++i)
        printf("%02x\n", data[i]);
    
    /* 3. Use a pointer to an unsigned char to examine the bytes */
    unsigned char *p = (unsigned char *)&d;
    size_t i;
    for (i=0; i < sizeof d; ++i)
        printf("%02x\n", p[i]);
    

    All the methods show you the bytes—but the same double value may print the bytes differently on different systems, e.g., due to different encodings (rare), or different endianness.

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

    Did you try taking the address of d and print sizeof( d ) bytes starting from that address?

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