首先,把代码写好,该回车的地方回车,该换行的地方换行,不要把代码揉成一团
自己尝试着分析这个程序,如果那里看不懂再提问#include <stdio.h>
#include <string.h>
void view_byte(void *obj, int size) {
for(int i = 0; i < size; ++i) {
printf("%.2x ", ((unsigned char *)obj)[i]);
}
printf("\n");
}
int main(void) {
union {int a; float b; char c; double d;} demo;
printf("char: %lu\n", sizeof(char));
printf("int: %lu\n", sizeof(int));
printf("float: %lu\n", sizeof(float));
printf("double: %lu\n", sizeof(double));
printf("demo: %lu\n", sizeof(demo));
printf("\na = 1234\n");
memset(&demo, 0, sizeof(demo));
view_byte(&demo, sizeof(demo));
demo.a = 1234;
view_byte(&demo, sizeof(demo));
printf("\nb = 123.456\n");
memset(&demo, 0, sizeof(demo));
view_byte(&demo, sizeof(demo));
demo.b = 123.456;
view_byte(&demo, sizeof(demo));
printf("\nc = 'a'\n");
memset(&demo, 0, sizeof(demo));
view_byte(&demo, sizeof(demo));
demo.c = 'a';
view_byte(&demo, sizeof(demo));
printf("\nd = 123.456\n");
memset(&demo, 0, sizeof(demo));
view_byte(&demo, sizeof(demo));
demo.d = 123.456;
view_byte(&demo, sizeof(demo));
return 0;
}
char: 1
int: 4
float: 4
double: 8
demo: 8
a = 1234
00 00 00 00 00 00 00 00
d2 04 00 00 00 00 00 00
b = 123.456
00 00 00 00 00 00 00 00
79 e9 f6 42 00 00 00 00
c = 'a'
00 00 00 00 00 00 00 00
61 00 00 00 00 00 00 00
d = 123.456
00 00 00 00 00 00 00 00
77 be 9f 1a 2f dd 5e 40
|