我正在学习c语言,我正在使用Linux终端。我已经编写了下面的简单代码,但是当我键入输入时,文件不退出,因此不计算字符数。有人能帮我吗?我也试过其他输入码。所有与输入相关的代码都是一样的。我做错什么了?请帮忙。

main()
{
    /* count characters in input */
    printf("Type some characters and the program will count the number of characters: ");

    int c = getchar();

    while(c!=EOF && c!= '\n')
        ++c;

    printf("Number of characters typed: %1d\n", c);
}

最佳答案

所以要注意那些有用的评论;

#include <stdio.h>

int main(){
        int c;
        int count = 0;

        while((c=getchar()) != '\n' && c != EOF)
                count++;
        printf("%d\n", count);
};

此代码按预期工作。

关于c - 输入未在c中退出,因此输出不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40187178/

10-11 22:36
查看更多