scanf不要求输入

时间:2016-03-09 07:31:27

标签: c xcode scanf

编译需要花费大量时间,并且会显示一些随机数,显然scanf()不会要求输入

#include <stdio.h>

int main() {

    int a;
    //a =  1472464;
    scanf ("%d", &a);
        if ((a % 6) && (a %4) == 0)
        {
            printf("Input %d is divisible by 6 and 4\n", a);
        }
        else {
            printf(" Input %d is not divisible by 6 and 4\n", a);
        }
    printf("Hello, World!\n");
    return 0;
}

2 个答案:

答案 0 :(得分:1)

这一行错了:

    if ((a % 6) && (a %4) == 0)

应该是:

    if ((a % 6) == 0 && (a %4) == 0)

I don't see any other obvious problem with the code.

答案 1 :(得分:1)

表达式(a % 6) && (a %4) == 0不会将模运算与零进行比较。相反,(a % 6)会产生05之间的数字,并将其用作布尔值,然后使用(a %4) == 0的结果。

相反,您需要单独进行每项比较:(a % 6) == 0 && (a % 4) == 0

这里要知道的重要一点是,在C中只有零,空指针被认为是“假”。任何非零(或空指针)的都是真的。

这意味着如果a例如4,则a % 6将为“true”,因为a % 64且不为零。相反,当a例如6时,a % 6将是0,这是“假”。

因此,仅使用a % 6实际上会产生与您想要的结果相反的结果,当a 均可被{{1}均匀分割时,它将为“true” }。

相关问题