C 中的表达式求值
while ((x[num++] = getchar()) != ' ');
它一次读取一个字符,如果遇到空格则停止。 但它如何评估带空格的括号呢?括号里的结果是什么?那么基本上,这个 (x[num++] = getchar()
) 等于什么,以便将其与空格进行比较?
我在我的一本课堂书籍中找到了这个例子,它是不同的,我收到了警告,所以我想这不是一个好的做法,是吗?这里的评价如何?它是否首先将读取的值分配给x[num]
,然后将其与空间进行比较?
while(x[num++] = getchar() != ' ');
while ((x[num++] = getchar()) != ' ');
This reads a char at a time and stops if it encounters a space.
But how does it evaluate the parenthesis with the space? What is the result of the parenthesis? So basically, what does this (x[num++] = getchar()
) equal to, in order to compare it to a space?
I found this example in one of my class books, it was different and I got a warning so I guess it's a not good practice is it? How does the evaluation go here? Does it first assign the read value to x[num]
and then compares it to space?
while(x[num++] = getchar() != ' ');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 C 中,
a = b
是一个表达式。它将b
分配给a
,然后返回a
。因此,while循环中的条件意味着:getchar()
获取用户的输入getchar
的结果赋值给x[num]
。num
加 1x[num]
是否不等于空格' '
。即
第二个例子有完全不同的行为。这是因为
!=
表达式将在不带括号的赋值=
之前计算。因此,第二个示例意味着getchar()
获取用户的输入x[num]
是否不等于空格' '
。true
或false
)分配给x[num]
num
加 1警告因为很容易将
while (a = b)
误写为while (a == b)
。In C,
a = b
is an expression. It assignsb
toa
, and then returnsa
. Therefore, the condition in the while loop means:getchar()
to get the user's inputgetchar
tox[num]
.num
by 1x[num]
is not equal to a space' '
.i.e.
The 2nd example has a totally different behavior. This is because the
!=
expression will be evaluated before the assignment=
without the parenthesis. Therefore, the 2nd example meansgetchar()
to get the user's inputx[num]
is not equal to a space' '
.true
orfalse
) tox[num]
num
by 1The warning is because it's easy to mistakenly write
while (a = b)
to meanwhile (a == b)
.这两个版本都不是好的编码;两者都忽略了在读取空格之前到达 EOF 的可能性,并且都忽略了在读取空格之前溢出缓冲区的可能性。
第一个在其他方面是正确的。它包含一个赋值:
赋值的结果就是赋值的值。因此,如果
getchar()
返回“X”,则赋值结果为“X”。然后将赋值结果与空白进行比较;它们是不同的,并且循环重复。如果getchar()
返回一个空白,那么赋值的结果也是一个空白,并且空白等于空白,因此循环终止。第二个是明显的错误。因为赋值的优先级比
!=
低,所以结果就好像代码读取:即通过
getchar()
读取一个字符,并与空白进行比较,生成如果字符不是空格,则值为 1;如果字符为空格,则值为 0。这个 0 或 1 被分配给 x[num++],然后作为逻辑条件进行计算。如果结果为0,则循环终止;如果结果为 1,则继续循环。请注意,该版本中未记录读取的字符 - 这就是编译器发出警告的原因。这几乎总是一个错误。在那些不是错误的奇怪情况下,您可以告诉编译器,通过提供额外的括号,可以让编译器以及您的人类观众(其他将阅读您的代码的人)清楚地表达您的意图。 (记住:如果你在 6 个月甚至 6 周后回来,你就会变成一个不同的人,并且很可能很难记住这些微妙之处。让每个人都明白。在过度括号之间需要保持良好的平衡。和括号内的代码。)代码可能应为:
请特别注意,
c
必须是int
而不是char
。Neither version is good coding; both ignore the possibility of reaching EOF before reading a space, and both ignore the possibility of overflowing the buffer before reading a space.
The first is otherwise correct. It contains an assignment:
The result of an assignment is the value assigned. So, if
getchar()
returns an 'X', the result of the assignment is 'X'. Then the result of the assignment is compared with blank; they are different, and the loop repeats. Ifgetchar()
returns a blank, then the result of the assignment is also a blank, and blank equals blank, so the loop terminates.The second is plain faulty. Because assignment has a lower precedence than
!=
, the result is as if the code read:That is, a character is read by
getchar()
and compared with blank, generating a value 1 if the character is not a blank and 0 if it is a blank. This 0 or 1 is assigned tox[num++]
, and then evaluated as a logical condition. If the result was 0, the loop terminates; if the result was 1, the loop continues. Note that the characters read are not recorded in this version - which is why the compiler gives the warning. It is almost invariably a mistake; on those odd occasions when it is not a mistake, you can tell the compiler that by providing the extra parentheses to make your intention crystal clear to the compiler - and to your human audience, the other people who will read your code. (Remember: if you come back in 6 months, or even 6 weeks, you'll be a different person and may well have difficulty remembering such subtleties. Make it plain to everyone. There is a fine balance to be drawn between over-parenthesizing and under-parenthesizing code.)The code should probably read:
Note, in particular, that
c
must be anint
and not achar
.