2.55 Compile and Run

Problem:

Compile and run the sample code that uses show_bytes on different machines to which you have access. Determine he byte orderings used by these machines.

code:

#include <stdio.h>
typedef unsigned char* byte_pointer;

void show_bytes(byte_pointer start, size_t len) {
    size_t i;
    for(i = 0; i < len; i++)
        printf(" %.2x", start[i]);
    printf("\n");
}

void show_int(int x) {
    show_bytes((byte_pointer) &x, sizeof(int));
}

void show_float(float x) {
    show_bytes((byte_pointer) &x, sizeof(float));
}

void show_pointer(void* x) {
    show_bytes((byte_pointer) &x, sizeof(void *));
}

void test_show_bytes(int val) {
    int ival = val;
    float fval = (float)val;
    int *pval = &val;
    show_int(ival);
    show_float(fval);
    show_pointer(pval);
}

int main() {
    int val = 12345;
    test_show_bytes(val);
    return 0;
}

compile:

gcc -m64 show_bytes.c -o show_bytes

execute:

./show_bytes

result:

39 30 00 00
00 e4 40 46
bc 68 1f b6 fd 7f 00 00

Last updated