由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 大家看看这道C语言题是怎么回事?
相关主题
fork(): why both if and else are executed?Help needed on coding for Fibonacci series
专业c++程序员都用什么ide (转载)请教一个C语言的面试题
问一个volatile和memcpy一起用的问题呼叫THRUST等C语言牛牛,菜鸟级C语言指针问题
批判Rust语言,以及C/C++为什么永远不会死(ZZ)如何实现N层循环嵌套
Go什么时候可能支持Generic?Linux GNU C, readlink问题
C/C++函数调用和栈内存再问个fork的题 (转载)
JHQ的一道指针题。Help: a Perl script question, Thanks (转载)
请教一道C语言的题目c++ 问题 (转载)
相关话题的讨论汇总
话题: val话题: ans话题: 45话题: c语言话题: expression
进入Programming版参与讨论
1 (共1页)
c*********t
发帖数: 2921
1
http://www.includehelp.com/c/operators-aptitude-questions-and-answers.aspx
3) What will be the output of following program ?
#include
void main()
{
int x=10;
x+=(x++)+(++x)+x;
printf("%d",x);
}
1. 44
2. 45
3. 46
4. 47
Correct Answer - 2
45
1) expand the expression : x=x+(x++)+(++x)+x;
2) due to pre increment ++x , x will be 11 for this expression.
3) after executing expression x will be 44.
4) finally x will be 45 due to post increment (x++).
弄不懂呀,到底这里 ++x, x++哪个先算?觉得x++应该先算,因为它先出现。可是解
释的正好相反。
我在电脑上编译run了一下这个代码,输出的就是45. 可是解释不通呀,
谁给看看。多谢
过去一直觉得对+这样的运算符都是从左到右。这题彻底颠覆了俺学了/用了几十年的
C语言的基本常识。很悲催呀。
a*****g
发帖数: 19398
2
这种考题就和脑筋急转弯一样,别当真
好的代码一个因素就是便于维护。

【在 c*********t 的大作中提到】
: http://www.includehelp.com/c/operators-aptitude-questions-and-answers.aspx
: 3) What will be the output of following program ?
: #include
: void main()
: {
: int x=10;
: x+=(x++)+(++x)+x;
: printf("%d",x);
: }
: 1. 44

c*********t
发帖数: 2921
3
还有就是同个link里的这个题
11) What will be the output of following program ?
#include
int main(){
char val=250;
int ans;
ans= val+ !val + ~val + ++val;
printf("%d",ans);
return 0;
}
Correct Answer - 2
-6
250 is beyond the range of char, the value of val will be -6.
Consider the expression:
ans= val+ !val + ~val + ++val;
Operator! , ~ and ++ have equal precedence. And it associative is right to
left.
So, First ++ operator will perform the operation. So value val will -5
Now,
ans = -5 + !-5 + ~-5 + -5
= -5 + !-5 + 4 - 5
= -5 + 0 + 4 -5
= -6
感觉怎么是从右往左算的呀?太坑爹了。

【在 a*****g 的大作中提到】
: 这种考题就和脑筋急转弯一样,别当真
: 好的代码一个因素就是便于维护。

g****t
发帖数: 31659
4
做这种题是一步一步的build up坏习惯。戒了吧。
实在想做,写个python程序自动给答案?
指针都没有,就是加号什么的,应该不难。


: 还有就是同个link里的这个题

: 11) What will be the output of following program ?

: #include

: int main(){

: char val=250;

: int ans;

: ans= val !val ~val val;

: printf("%d",ans);

: return 0;

: }



【在 c*********t 的大作中提到】
: 还有就是同个link里的这个题
: 11) What will be the output of following program ?
: #include
: int main(){
: char val=250;
: int ans;
: ans= val+ !val + ~val + ++val;
: printf("%d",ans);
: return 0;
: }

l**m
发帖数: 3
5
如果用gcc编译,结果是46. 第二行是45.
x += (x++)+(++x)+x; 46
x = x+(x++)+(++x)+x; 45
x = (x++)+(++x)+x+x; 46
用clang,结果是46. 第二行是44,
x += (x++)+(++x)+x; 46
x = x+(x++)+(++x)+x; 44
x = (x++)+(++x)+x+x; 46
用VC++,全是45。
pre inc当然先算了。只是不同的compiler处理sequence point时有小小差别。
a****a
发帖数: 5763
6

这种东西实际上是标准里未定义的, 严重依赖于编译器实现
哪个货要是在项目里写了这种code,直接开掉就好了,精神肯定是有问题的

【在 c*********t 的大作中提到】
: http://www.includehelp.com/c/operators-aptitude-questions-and-answers.aspx
: 3) What will be the output of following program ?
: #include
: void main()
: {
: int x=10;
: x+=(x++)+(++x)+x;
: printf("%d",x);
: }
: 1. 44

w***g
发帖数: 5958
7
我隐约记得很多年前有置顶贴告诉大家不要做/问这类题目。

【在 c*********t 的大作中提到】
: http://www.includehelp.com/c/operators-aptitude-questions-and-answers.aspx
: 3) What will be the output of following program ?
: #include
: void main()
: {
: int x=10;
: x+=(x++)+(++x)+x;
: printf("%d",x);
: }
: 1. 44

p***o
发帖数: 1252
8
这种按标准里的定义基本都是undefined behavior, 和compiler-dependent差远了。

【在 a****a 的大作中提到】
:
: 这种东西实际上是标准里未定义的, 严重依赖于编译器实现
: 哪个货要是在项目里写了这种code,直接开掉就好了,精神肯定是有问题的

o**o
发帖数: 3964
9
碰到这种拿不确定性为考题的傻逼,直接把管撸到他脸上。

【在 c*********t 的大作中提到】
: http://www.includehelp.com/c/operators-aptitude-questions-and-answers.aspx
: 3) What will be the output of following program ?
: #include
: void main()
: {
: int x=10;
: x+=(x++)+(++x)+x;
: printf("%d",x);
: }
: 1. 44

l*****n
发帖数: 1648
10
怎么可能,等你撸到射的时候,人家早跑了

【在 o**o 的大作中提到】
: 碰到这种拿不确定性为考题的傻逼,直接把管撸到他脸上。
相关主题
C/C++函数调用和栈内存Help needed on coding for Fibonacci series
JHQ的一道指针题。请教一个C语言的面试题
请教一道C语言的题目呼叫THRUST等C语言牛牛,菜鸟级C语言指针问题
进入Programming版参与讨论
c*********e
发帖数: 16335
11
对。这种题没意思。

【在 w***g 的大作中提到】
: 我隐约记得很多年前有置顶贴告诉大家不要做/问这类题目。
k*****u
发帖数: 136
12
1. 我想知道 为什么还在用C语言
2. 这种形式的题 在实际工作中是永远 用不到的
s***i
发帖数: 600
13
写这种代码的人直接解雇。这是在给雇主埋炸弹。换compiler的是会搞死人的。

【在 c*********t 的大作中提到】
: http://www.includehelp.com/c/operators-aptitude-questions-and-answers.aspx
: 3) What will be the output of following program ?
: #include
: void main()
: {
: int x=10;
: x+=(x++)+(++x)+x;
: printf("%d",x);
: }
: 1. 44

l**********0
发帖数: 150
14
面试碰到这种题直接说我们需要避免这种代码

【在 s***i 的大作中提到】
: 写这种代码的人直接解雇。这是在给雇主埋炸弹。换compiler的是会搞死人的。
h**6
发帖数: 4160
15
上次我们没有换compiler,只是把编译参数从0x改为1y,结果就得出许多不一致。只好
换回0x
1 (共1页)
进入Programming版参与讨论
相关主题
c++ 问题 (转载)Go什么时候可能支持Generic?
一个C++语法问题C/C++函数调用和栈内存
一个multithreading 问题JHQ的一道指针题。
post increment请教一道C语言的题目
fork(): why both if and else are executed?Help needed on coding for Fibonacci series
专业c++程序员都用什么ide (转载)请教一个C语言的面试题
问一个volatile和memcpy一起用的问题呼叫THRUST等C语言牛牛,菜鸟级C语言指针问题
批判Rust语言,以及C/C++为什么永远不会死(ZZ)如何实现N层循环嵌套
相关话题的讨论汇总
话题: val话题: ans话题: 45话题: c语言话题: expression