gpt4 book ai didi

c - 此代码是否 ungetch() '\n' ?

转载 作者:行者123 更新时间:2023-11-30 17:27:38 25 4
gpt4 key购买 nike

我到处寻找,但找不到答案!我正在使用 K&R C 书籍中的一个名为 getop 的函数进行练习。例如,当我输入 123 时,代码会检查输入的每个元素,并在它不是数字时停止。在此示例中为“\n”;它会取消 eclipse 刻( '\n' )吗?

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;
}

取消 eclipse 刻功能:

void ungetch(int c) {
if(bufp < MAXBUF) {
printf("ungetch has been called\n");
buf[bufp++] = c;
}
else
printf("the buffer is full\n");
}

最佳答案

I thought that eof is for window ctrl+z , and '\n' is something else...?

ISO/IEC 9899编程语言 - C:

EOF ... expands to an integer constant expression, with type int and a negative value, that is returned by several functions to indicate end-of-file, that is, no more input from a stream

Windows 上的 Ctrl-Z 是一个输入键组合,不会返回更多字符,甚至不会返回与 Ctrl-Z (0x1A) 等效的字符。

\n (new line) Moves the active position to the initial position of the next line.

关于原来的问题:由于负值 EOF 与任何 (unsigned char) 值不同,是的,这段代码确实执行了 ungetch() '\n'

关于c - 此代码是否 ungetch() '\n' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26325756/

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