我在 Windows 上,我正在使用 git bash 来运行我的 c 程序。
我使用 gcc 来编译代码。当我只是做一个简单的printf("hello, world");
它有效,但是当我尝试创建一个简单的程序来添加两个数字时,它什么也不做。
它与 gcc -o sum sum.c
一起编译但是当我使用 ./sum
运行它时它什么都不做,但是当我在命令提示符下运行它时,它运行正常。
#include <stdio.h>
int main(void) {
int n1, n2;
printf("Enter a number: ");
scanf("%d", &n1);
printf("Enter another number: ");
scanf("%d", &n2);
int sum = n1 + n2;
printf("Sum: %d\n", sum);
}
我尝试在 git bash 中输入值并与 cmd 相比得到了这个输出
最佳答案
我通过附加 fflush(stdout)
解决了这个问题在那些之后 printf()
声明:
printf("Enter first value: ");
fflush(stdout); // this
scanf("%d", &a);
printf("Enter second value: ");
fflush(stdout); // this
scanf("%d", &b);
只需刷新缓冲区即可让程序在打印后等待您的输入。
工作示例截图:
关于c - 如何在 Git bash 中运行 C 程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62040923/