小编Phi*_*oll的帖子

为什么使用多个"if"不起作用,但它确实可以在while循环中使用"if"和"else if"?

这是我的代码,不使用else if:

#include <stdio.h>

main()
{
    long s = 0, t = 0, n = 0;
    int c;
    while ((c = getchar()) != EOF)
        if (c == ' ')
            ++s;
        if (c == '\t')
            ++t;
        if (c == '\n')
            ++n;
    printf("spaces: %d tabulations: %d newlines: %d", s, t, n);
}
Run Code Online (Sandbox Code Playgroud)

这是使用else的代码,如果:

#include <stdio.h>

main()
{
    long s = 0, t = 0, n = 0;
    int c;
    while ((c = getchar()) != EOF)
        if …
Run Code Online (Sandbox Code Playgroud)

c if-statement while-loop getchar

1
推荐指数
1
解决办法
85
查看次数

标签 统计

c ×1

getchar ×1

if-statement ×1

while-loop ×1