Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Variables 变量扫描_Variables_If Statement - Fatal编程技术网

Variables 变量扫描

Variables 变量扫描,variables,if-statement,Variables,If Statement,我使用LINUX操作系统和GCC编译器。当我运行这个代码时 #include<stdio.h> int main() { int age; char col; printf("Enter a age "); scanf("%d",&age); printf("Enter a college:"); scanf("%c",&co

我使用LINUX操作系统和GCC编译器。当我运行这个代码时

        #include<stdio.h>
        int main()
        {
        int age;
        char col;
        printf("Enter a age ");
        scanf("%d",&age);
        printf("Enter a college:");
        scanf("%c",&col);
        if(age>=25 && (col=='H' || col=='Y'))
        printf("Harvard");
        else
        printf("Yale");
        return 0;
        }
即使我尝试给年龄赋予不同的值,结果仍然是一样的。 为什么循环会中止并打印代码中的下一条语句

当我更改变量声明时,也就是说,我首先读取字符,然后读取年龄。现在代码正常工作。
为什么会发生这种情况?

在接受整数输入
age
后,按enter键,应将其计为
char
,并在
列中赋值。您需要注意使用
getchar()
按下的回车键。请看下面的代码:

int main(){
    int age;
    char col;
    printf("Enter a age ");
    scanf("%d",&age);
    getchar();  // the enter which you hit after taking age as an integer input
    printf("Enter a college:"); 
    scanf("%c",&col);
    if(age>=25 && (col=='H' || col=='Y'))
    printf("Harvard");
    else
    printf("Yale");

    return 0;
}
int main(){
    int age;
    char col;
    printf("Enter a age ");
    scanf("%d",&age);
    getchar();  // the enter which you hit after taking age as an integer input
    printf("Enter a college:"); 
    scanf("%c",&col);
    if(age>=25 && (col=='H' || col=='Y'))
    printf("Harvard");
    else
    printf("Yale");

    return 0;
}