由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 请教一道C语言的题目
相关主题
谁帮我解释一下这个代码C/C++函数调用和栈内存
关于 big/little endian,为什么对char 有影响?little endian vs big endian
请教一个C语言的面试题问一个C语言中类型cast的问题
看下这个小程序C 中的typedef 一问
谁来解释一下这个是compiler问题吗?a pointer to a const addr
G++用-g和-O3编译运行结果竟然不一样一个C语言的面试题,有点乱,麻烦看一下
c++ pointer conversion questionC一个问题搞不懂
C语言跟Java运行速度比较 (转载){C语言}请教如何通过变量访问结构体内的各个属性
相关话题的讨论汇总
话题: endian话题: ds话题: machine话题: d%话题: 0x0503
进入Programming版参与讨论
1 (共1页)
c*********n
发帖数: 1057
1
typedef union ds_{
short s;
char c;
} ds;
ds object;
object.s = 0x0503;
printf("%d\n", object.c);
答案是:
on little endian machine: 3
on big endian machine: 5
谁来解释下好么?
h*****0
发帖数: 4889
2
ds __
s __
c _X
now s = 0x0503, it is two bytes 5 and 3
int little endian machine:
s 35
int big endian machine:
s 53
c is using the first byte of s, therefore...

【在 c*********n 的大作中提到】
: typedef union ds_{
: short s;
: char c;
: } ds;
: ds object;
: object.s = 0x0503;
: printf("%d\n", object.c);
: 答案是:
: on little endian machine: 3
: on big endian machine: 5

c*********n
发帖数: 1057
3
怎么觉得有点不对啊
little endian的话:
如果s的地址是addr
addr+0=3, addr+1=5对吧?
然后c is using the first byte of union ds, 所以是3
这样理解对么?

【在 h*****0 的大作中提到】
: ds __
: s __
: c _X
: now s = 0x0503, it is two bytes 5 and 3
: int little endian machine:
: s 35
: int big endian machine:
: s 53
: c is using the first byte of s, therefore...

c*********n
发帖数: 1057
4
main()
{
int i=5;
printf("%d%d%d%d%d",i++,i--,++i,--i,i);
}
在linux上用gcc打出来是45555
但是题目的标准答案说:The arguments in a function call are pushed into the
stack from left to right. The evaluation is by popping out from the stack.
and the evaluation is from right to left, hence the result.
按照这个答案的话,应该打出来是45545
到底怎么跑的呢?

【在 c*********n 的大作中提到】
: 怎么觉得有点不对啊
: little endian的话:
: 如果s的地址是addr
: addr+0=3, addr+1=5对吧?
: 然后c is using the first byte of union ds, 所以是3
: 这样理解对么?

t****t
发帖数: 6806
5
哪来的标准答案? 把那本书扔掉

【在 c*********n 的大作中提到】
: main()
: {
: int i=5;
: printf("%d%d%d%d%d",i++,i--,++i,--i,i);
: }
: 在linux上用gcc打出来是45555
: 但是题目的标准答案说:The arguments in a function call are pushed into the
: stack from left to right. The evaluation is by popping out from the stack.
: and the evaluation is from right to left, hence the result.
: 按照这个答案的话,应该打出来是45545

m********a
发帖数: 1312
6
google "c++ parameter evaluation order".似乎应该是undefined order.

【在 c*********n 的大作中提到】
: main()
: {
: int i=5;
: printf("%d%d%d%d%d",i++,i--,++i,--i,i);
: }
: 在linux上用gcc打出来是45555
: 但是题目的标准答案说:The arguments in a function call are pushed into the
: stack from left to right. The evaluation is by popping out from the stack.
: and the evaluation is from right to left, hence the result.
: 按照这个答案的话,应该打出来是45545

c*********n
发帖数: 1057
7
在不同的机器上用不同的编译器答案是不同的
对么?

【在 m********a 的大作中提到】
: google "c++ parameter evaluation order".似乎应该是undefined order.
a**u
发帖数: 813
8
this problem makes no sense. consider C++, it is claimed to be undefined(
different compiler can give different result)

【在 c*********n 的大作中提到】
: main()
: {
: int i=5;
: printf("%d%d%d%d%d",i++,i--,++i,--i,i);
: }
: 在linux上用gcc打出来是45555
: 但是题目的标准答案说:The arguments in a function call are pushed into the
: stack from left to right. The evaluation is by popping out from the stack.
: and the evaluation is from right to left, hence the result.
: 按照这个答案的话,应该打出来是45545

1 (共1页)
进入Programming版参与讨论
相关主题
{C语言}请教如何通过变量访问结构体内的各个属性谁来解释一下这个是compiler问题吗?
这个printf结果是什么?G++用-g和-O3编译运行结果竟然不一样
在帮忙看看这个吧 C: int->char*c++ pointer conversion question
Help needed on coding for Fibonacci seriesC语言跟Java运行速度比较 (转载)
谁帮我解释一下这个代码C/C++函数调用和栈内存
关于 big/little endian,为什么对char 有影响?little endian vs big endian
请教一个C语言的面试题问一个C语言中类型cast的问题
看下这个小程序C 中的typedef 一问
相关话题的讨论汇总
话题: endian话题: ds话题: machine话题: d%话题: 0x0503