为什么C程序代码不询问第二个值以及char如何以int形式存储,我是从Web上的示例中获取的

#include <stdio.h>
// source tutorial points
int main(){
    int c;
    char d;
    printf("Enter First value \n");

    c = getchar();

    printf("Enter Second value \n");
    d = getchar();

    printf("You have entered first \n");
    putchar(c);

    printf("You have entered second \n");
    putchar(d);

    return(0);
}

当我输入第一个值时,它并没有要求其他值,请帮助我不要在char中输入一个不可能的字符。

评论
对你的冷漠
对你的冷漠

Because each time you input a character and hit ENTER. So, second getchar in your code reads the enter character.

您的代码应更改为:

    c = getchar();
    getchar(); // for consuming the enter character

    printf("Enter Second value \n");
    d = getchar();
    getchar(); // for consuming the enter character

输出:

Enter First value                                                                                                         
a                                                                                                                         
Enter Second value                                                                                                        
b                                                                                                                         
You have entered first                                                                                                    
a                                                                                                                         
You have entered second                                                                                                   
b
点赞
评论