gpt4 book ai didi

c - 如何在 Git bash 中运行 C 程序?

转载 作者:行者123 更新时间:2023-12-03 23:47:06 27 4
gpt4 key购买 nike

我在 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);

只需刷新缓冲区即可让程序在打印后等待您的输入。

工作示例截图:

Git Bash Working Program Example

关于c - 如何在 Git bash 中运行 C 程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62040923/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com