一道笔试题

来源:互联网 发布:阿里云独立服务器租用 编辑:程序博客网 时间:2024/06/05 23:20

  看一道题目:

#include<stdio.h>struct node{__int64 a;__int64 b;__int64 c;}Node;void test(){struct node node;node.a = 1;node.b = 2;node.c = 3;printf("%d %d %d\n", node.a, node.b, node.c);}int main(){test();return 0;}

输出的结果是什么?

答案: 1 0 2

为什么答案不是1 2 3呢?

继续测试一下...

对test函数作修改,如下:

void test(){struct node node;node.a = 1;node.b = 2;node.c = 3;printf("%d %d %d\n", node.a, node.b, node.c);printf("%d %d %d\n", node.a, node.c, node.b);printf("%d %d %d\n", node.b, node.a, node.c);printf("%d %d %d\n", node.b, node.c, node.a);printf("%d %d %d\n", node.c, node.a, node.b);printf("%d %d %d\n", node.c, node.b, node.a);}
再运行,输出结果为:

1 0 2
1 0 3
2 0 1
2 0 3
3 0 1
3 0 2
Press any key to continue

发现规律了吧! 也就是说输出第一个%d时,能够得到该值(前提是小端法,大端法输出为0),第二个%d时输出的是第一个%d变量的下面的四个字节的数据,为0(这与小端法的存储有关),接下来第三个%d输出的就是第二个变量的值.腾讯2013年的笔试题,很可惜,我写错了.可是这是为什么???who can tell me why?

原创粉丝点击