c语言编写的程序在命令窗口运行很快消失

2025-04-30 14:52:23
推荐回答(4个)
回答1:

后面跟着:“Press any key to continue”是很正常的,这是系统自动加上去的,不影响程序的运行,窗口很快消失你只需在主函数最后加上语句getch();就可以了,使用这个函数要包含头文件#include这个函数的功能是等待你输入一个字符,然后再关闭窗口,只要你不去按键盘这个窗口就永远不会消失了!例如:
#include
#include
void main()
{
printf("Hello World!\n");
getch();
}
自己试试吧!

你这个最简单的代码存在点小问题,我想是你马虎打错了。
将printf("Hello World!"\n); 改为printf("Hello World!\n");

回答2:

两个方法:
方法1.
包含头文件:#include
使用函数:getch();

改过以后:
#include
#include
void main()
{
printf("Hello World!"\n);
getch();
}

方法1.
包含头文件:#include
使用函数:system("pause");

改过以后:
#include
#include
void main()
{
printf("Hello World!"\n);
system("pause");
}

试试吧:)

回答3:

那是因为程序已经运行完了,所以窗口自动关闭,
只要在程序末加上个getch()就行了,当按下任意键后,窗口会自动关闭。

回答4:

呵呵我是定义了一个变量,接着获得输入的值。
#include
void main()
{
printf("Hello World!"\n);
int i;
scanf("%d",&i);
}