Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.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
getchar()立即返回_C_Getchar - Fatal编程技术网

getchar()立即返回

getchar()立即返回,c,getchar,C,Getchar,我正在Visual Studio 2013 Express中为桌面编程C,当我使用getchar()时,它立即终止 代码如下: #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> int main() { int num1, num2; printf("Enter first number: "); scanf("%d", &num1); printf("Enter second number

我正在Visual Studio 2013 Express中为桌面编程C,当我使用getchar()时,它立即终止

代码如下:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main()
{
    int num1, num2;
    printf("Enter first number: ");
    scanf("%d", &num1);
    printf("Enter second number: ");
    scanf("%d", &num2);
    printf("Result: %d\n", num1 + num2);
    printf("Press any key to exit...");
    getchar();
}
\define\u CRT\u SECURE\u NO\u警告
#包括
int main()
{
int num1,num2;
printf(“输入第一个数字:”);
scanf(“%d”&num1);
printf(“输入第二个数字:”);
scanf(“%d”&num2);
printf(“结果:%d\n”,num1+num2);
printf(“按任意键退出…”);
getchar();
}
打印完最后一个字符串后,程序将立即退出,无需等待任何按键,即使我使用了getchar()

getchar()是否应该等待按键(字符输入),然后继续移动?
为什么它在不等待按键的情况下自动启动并退出程序?

解决方案是调用
getchar()最后两次,或者改为使用


问题是,当你在控制台上输入一个字符时,实际上每次都会输入一个额外的换行符。

“因为输入缓冲区中还有一个字符。这个问题大约每天在StackOverflow上出现一次。”。我会看看是否能找到一些副本…我认为这是一个重复[使用GETCHE()来保存命令提示符打开Visual C++ 2010 ]()非常感谢!我现在明白了。