/* show_bytes.c * For use in CSE351 Lec 3 on Memory, Data, and Addressing */ // needed for library function printf() #include /* Normally you would put function prototypes here: void show_bytes(char *start, int len); void show_int(int x); Except that in this case the function declarations come before their first calls in the file, so they are not necessary. */ /* Takes start address of data and prints out len bytes in hex. */ void show_bytes(char *start, int len) { int i; // for loop doesn't need curly braces {} because single-line body for (i = 0; i < len; i++) // printf symbols: // %p - print as pointer // \t - tab character // %x - print in hex (.2 means pad to 2 digits) // \n - newline character printf("%p\t0x%.2hhX\n", start+i, *(start+i)); printf("\n"); } /* Uses show_bytes() to print hex representation of integer. Use of sizeof() means this code will work even if int isn't 4B. */ void show_int(int x) { show_bytes((char *) &x, sizeof(int)); } /* Example usage of show_int(). */ int main() { int x = 123456; // 123456 = 0x1E240 printf("int x = %d;\n", x); show_int(x); return 0; }