在我测试之前,我一直认为scanf("%c" , &addr);等于:getchar()
#include<stdio.h>
int main()
{
    int i;
    scanf("%c",&i);
    printf("%d\n", i);
    if(i == EOF)
        printf("EOF int type and char input\n");
    i =getchar();
    printf("%d\n", i);
    if(i == EOF)
        printf("EOF int type and char input\n");
}
当我使用“Ctrl+D”两次时,我得到了输出:
-1217114112
-1
EOF int 类型和 char 输入
由于 EOF-1在inttype 中,我也尝试使用scanf("%d",&i);replace scanf("%c",&i),得到相同的输出。
我很困惑。有人可以为我解释一下吗?
- - - - - - - - - - - - - - - - - 编辑 - - - - - - - - --------------------------------
我想知道scanf("%c",i)Ctrl+D 的行为,我做了测试:
#include<stdio.h>
int main()
{
    int i;
    int j;
    j = scanf("%c",&i);
    printf("%c\n", i);
    printf("%d\n", j);
    if(i == EOF)
        printf("EOF int type and char input");
     i =getchar();
    printf("%d\n", i);
    if(i == EOF)
        printf("EOF int type and char input");
}
输出:
k                  // If the scanf set 1 byte in i , why here print 'k' ?
-1
-1
EOF int type and char input