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
|
|