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

标签 c

我到处寻找,但找不到答案!我正在使用 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/

相关文章:

c - 在 linux 之外为 Cortex-A7 启用 SPI Controller

c - 将 SQLite FTS 扩展构建为 DLL 的正确方法是什么?

c - lextestpass.l :384: error: expected expression before ‘int’

c - strtok 表现出不良行为

c - 可移植地确定最严格的对齐数据类型

c - JNA 写入 stdout 时内存访问无效

将结构从 src 复制到 dst

c - 如何在C中通过id选择GtkTreeview行

c - "different width due to prototype"警告是什么意思?

C 计算句子中每个单词的元音