推荐一本关于 C 示例中的棘手问题的书,异常 if 条件
可能的重复:
“,”运算符在 C 中做什么?
好吧,我有今天的一次采访,他们问我以下代码在我的机器上运行后应该输出什么
#include<stdio.h>
int main ()
{
int a=1,b=1;
char c='0';
if(a,b,c)
printf("wow \n");
}
,我能够得到答案,但我无法回答。我想知道是否允许这样的 if 语句?哪里有提到呢?
我的问题是上面提到的 if 条件,我无法理解该 if 语句如何工作。
**更新**
我在 K&R 中没有发现任何这样的东西,任何人都可以推荐一本好书。我已经编程了一些东西,对 C 来说并不陌生,但在这个问题失败后,我想再次看看是否还有一些深入的 C 概念(特别是这样的)如上所述)在哪里可以阅读。
Possible Duplicate:
What does the ',' operator do in C?
Ok I had an interview today and they asked me what should be the output of the following code
#include<stdio.h>
int main ()
{
int a=1,b=1;
char c='0';
if(a,b,c)
printf("wow \n");
}
after running it on my machine I am able to get the answer but I was not able to answer there.I want to know if such a if statement is allowed? Where is it mentioned?
My problem is the if condition mentioned above I am not able to understand how does that if statement work.
**UPDATE **
I did not found any such thing in K&R can any one recommend a good book.I have programmed things and not new to C but still after failing this question I want to once more look if some more C concepts in depth (specially such as above) are mentioned where can I read.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
逗号表达式
a,b,c
仅采用最后一个值c
的值,该值具有字符值'0'
,其中数值为 48。因此表达式的计算结果为 true。The comma expression
a,b,c
just takes the value of the last value,c
which has the character value'0'
, which has a numeric value of 48. So the expression evaluates to true.查看逗号运算符。
正如您所看到的,
e1, e2, e3
的计算结果是 e3,如 ANSI C 标准中所指定,因此,您的 if 条件的计算结果为
'0'
,即0 个字符,其值不同于 0,因此,条件为 true,并且打印“wow”Have a look at the comma operator.
As you can see, the evaluation of
e1, e2, e3
is e3, as specified in the ANSI C standardso, your if condition evaluates in
'0'
wich is the 0 charcater, wich have a VALUE different from 0, so, the condition is true, and 'wow' is printed维基百科关于逗号运算符的条目非常好,它解释了它是如何实现的工作简洁。
表达式
a, b, c
的结果是c
,在本例中等于'0'
,其计算结果为正确。
Wikipedia's entry on the comma operator is quite good, it explains how it works concisely.
The result of the expression
a, b, c
isc
which in this case is equal to'0'
, which evaluates totrue
.逗号表达式。它的结果是最右边的争论,在你的例子中 -
c
。还值得注意的是,逗号表达式保证了一个序列点,即参数从左到右计算,这与许多其他运算符不同
Comma expression. The result of it is the rightmost arguement, in your case -
c
.It is also notable that the comma expression guarantees a sequence point, that is the arguments are evaluated from left to right, unlike many other operators
逗号运算符是一个 C++ 运算符,其作用是计算所有表达式,并丢弃除最后一个表达式之外的所有表达式的结果。
对于您的情况,以下两个语句是等效的。
在这两种情况下,IF 语句内的代码将根据
c
的值执行。The comma operator is a C++ operator that has the effect of evaluating all the expressions, and discarding the result of all the expressions but the last.
In your case, both the following statements are equivalent.
In both the cases, the code inside the IF-statement will be executed basing on the value of
c
.逗号表达式的结果是最后一个表达式的值。其中“0”不为零。
The result of the comma expression is the value of the last expression. Which is '0' which is not zero.
现在它已经过时了,作者大量参考了 Solaris 环境和编译器,但您可以看看 Peter Van Der Linden 写的“专家 C 编程:深层 C 秘密”。这本书至少90%的内容还是非常有用的。甚至还有面试问题的附录。我不记得逗号运算符是否被覆盖,但很多其他东西都被覆盖了。
另外两个(免费)资源:
http://c-faq.com/
http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf
It's getting old now and the author makes a lot of reference to the Solaris environment and compiler, but you could look at 'Expert C Programming: Deep C Secrets' by Peter Van Der Linden. At least 90% of the book is still very useful. There is even an appendix on interview questions. I can't remember if the comma operator is covered but many other things are.
Two other (free) resources:
http://c-faq.com/
http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf