C语言 getchar() function



我正在尝试使用getchar()来计数数字。

如果我使用getchar(不计算点或逗号的条件)并输入像345.234这样的数字,它会像3-4-5-2-3-4那样计数6还是像345-23 -4那样计数4?

i=0
while((c=getchar())!=',' && c!=EOF)
i++;

简单的问题这算多少钱345.234和ctrl+z同时输入这算吗?还是算6

每次调用getchar(),只要不返回EOF',', i将递增。

如果你输入345.234,然后按Ctrl-Z键,这将导致在离开while循环时,i的值为7

3个数字+ 1个点+ 3个数字= 7个字符

可能

#include <stdio.h>
int main(){
    int i=0,c;
    while(EOF!=(c=getchar())){
        if(c != ',' && c != '.' && c!= 'n')
            ++i;
    }
    printf("number count is %dn", i);
    return 0;
}

最新更新