gpt4 book ai didi

c - 需要帮助理解 C 中表达式的求值顺序

转载 作者:太空宇宙 更新时间:2023-11-04 06:27:38 25 4
gpt4 key购买 nike

我无法理解下面代码中表达式的计算方式。我不明白这里的代码是如何工作的

while (isdigit(s[++i] = c = getch()))
;

为什么我们需要

s[1] = '\0';

完整代码

#include <ctype.h>
int getch(void);
void ungetch(int);
/* getop: get next character or numeric operand */

int getop(char s[])
{
int i, c;

while ((s[0] = c = getch()) == ' ' || c == '\t');
s[1] = '\0';

if (!isdigit(c) && c != '.')
return c; /* not a number */

i = 0;

if (isdigit(c)) /* collect integer part */
while (isdigit(s[++i] = c = getch()));

if (c == '.') /* collect fraction part */
while (isdigit(s[++i] = c = getch()));

s[i] = '\0';
if (c != EOF)
ungetch(c);

return NUMBER;
}

感谢您的帮助!

最佳答案

= 是右结合的,因此 isdigit(s[++i] = c = getch()) 将被分组为
是数字( s[++i] = (c = getch()) )getch 将读取一个char 并将其分配给c,然后将c 分配给s[++我].

关于c - 需要帮助理解 C 中表达式的求值顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24686937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com