C 中的表达式求值

发布于 2024-11-18 13:04:13 字数 353 浏览 3 评论 0原文

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 技术交流群。

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

发布评论

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

评论(2

枕头说它不想醒 2024-11-25 13:04:13

在 C 中,a = b 是一个表达式。它将b 分配给a,然后返回a。因此,while循环中的条件意味着:

  1. 调用getchar()获取用户的输入
  2. getchar的结果赋值给x[num]
  3. num 加 1
  4. 检查 x[num] 是否不等于空格 ' '

while (true) {
  x[num] = getchar();
  num ++;
  if (!(x[num] != ' '))
    break;
}

第二个例子有完全不同的行为。这是因为 != 表达式将在不带括号的赋值 = 之前计算。因此,第二个示例意味着

  1. 调用 getchar() 获取用户的输入
  2. 检查 x[num] 是否不等于空格 ' '
  3. 将步骤 2 中的结果(truefalse)分配给 x[num]
  4. num 加 1

警告因为很容易将 while (a = b) 误写为 while (a == b)

In C, a = b is an expression. It assigns b to a, and then returns a. Therefore, the condition in the while loop means:

  1. Call getchar() to get the user's input
  2. Assign the result of getchar to x[num].
  3. Increase num by 1
  4. Check if x[num] is not equal to a space ' '.

i.e.

while (true) {
  x[num] = getchar();
  num ++;
  if (!(x[num] != ' '))
    break;
}

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 means

  1. Call getchar() to get the user's input
  2. Check if x[num] is not equal to a space ' '.
  3. Assign the result in step 2 (true or false) to x[num]
  4. Increase num by 1

The warning is because it's easy to mistakenly write while (a = b) to mean while (a == b).

与他有关 2024-11-25 13:04:13

这两个版本都不是好的编码;两者都忽略了在读取空格之前到达 EOF 的可能性,并且都忽略了在读取空格之前溢出缓冲区的可能性。

第一个在其他方面是正确的。它包含一个赋值:

x[num++] = getchar()

赋值的结果就是赋值的值。因此,如果 getchar() 返回“X”,则赋值结果为“X”。然后将赋值结果与空白进行比较;它们是不同的,并且循环重复。如果getchar()返回一个空白,那么赋值的结果也是一个空白,并且空白等于空白,因此循环终止。

第二个是明显的错误。因为赋值的优先级比 != 低,所以结果就好像代码读取:

while (x[num++] = (getchar() != ' '))
    ;

即通过 getchar() 读取一个字符,并与空白进行比较,生成如果字符不是空格,则值为 1;如果字符为空格,则值为 0。这个 0 或 1 被分配给 x[num++],然后作为逻辑条件进行计算。如果结果为0,则循环终止;如果结果为 1,则继续循环。请注意,该版本中未记录读取的字符 - 这就是编译器发出警告的原因。这几乎总是一个错误。在那些不是错误的奇怪情况下,您可以告诉编译器,通过提供额外的括号,可以让编译器以及您的人类观众(其他将阅读您的代码的人)清楚地表达您的意图。 (记住:如果你在 6 个月甚至 6 周后回来,你就会变成一个不同的人,并且很可能很难记住这些微妙之处。让每个人都明白。在过度括号之间需要保持良好的平衡。和括号内的代码。)

代码可能应为:

int c;
int max = sizeof(x) - 1;
int num = 0;

while ((c = getchar()) != EOF && num < max && (x[num++] = c) != ' ')
    ;

请特别注意,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:

x[num++] = getchar()

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. If getchar() 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:

while (x[num++] = (getchar() != ' '))
    ;

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 to x[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:

int c;
int max = sizeof(x) - 1;
int num = 0;

while ((c = getchar()) != EOF && num < max && (x[num++] = c) != ' ')
    ;

Note, in particular, that c must be an int and not a char.

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