推荐一本关于 C 示例中的棘手问题的书,异常 if 条件

发布于 2024-11-15 20:20:52 字数 564 浏览 4 评论 0原文

可能的重复:
“,”运算符在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

囍孤女 2024-11-22 20:20:52

逗号表达式 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.

倚栏听风 2024-11-22 20:20:52

查看逗号运算符

正如您所看到的,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 standard

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

反话 2024-11-22 20:20:52

维基百科关于逗号运算符的条目非常好,它解释了它是如何实现的工作简洁。

表达式 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 is c which in this case is equal to '0', which evaluates to true.

厌倦 2024-11-22 20:20:52

逗号表达式。它的结果是最右边的争论,在你的例子中 - 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

爱的十字路口 2024-11-22 20:20:52

逗号运算符是一个 C++ 运算符,其作用是计算所有表达式,并丢弃除最后一个表达式之外的所有表达式的结果。

对于您的情况,以下两个语句是等效的。

if (a,b,c) {
 printf("wow \n");
}

if (c) {
 printf("wow \n");
}

在这两种情况下,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.

if (a,b,c) {
 printf("wow \n");
}

if (c) {
 printf("wow \n");
}

In both the cases, the code inside the IF-statement will be executed basing on the value of c.

禾厶谷欠 2024-11-22 20:20:52

逗号表达式的结果是最后一个表达式的值。其中“0”不为零。

The result of the comma expression is the value of the last expression. Which is '0' which is not zero.

梦回梦里 2024-11-22 20:20:52

现在它已经过时了,作者大量参考了 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文