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

来源:百度知道 编辑:UC知道 时间:2024/05/05 11:27:55
大家不要笑啊,刚学c语言,很不明白一件事:为什么编写的c程序编译链接后run,出现一个命令窗口,一般后面跟着:“Press any key to continue”然后按任意键窗口消失,有时甚至run后还未看清楚内容窗口就消失了。附一个谁都知道的源代码:
#include<stdio.h>
void main()
{
printf("Hello World!"\n);
}
请不要笑话我啊,我感觉这个问题也很弱智,但就是想不明白。

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

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

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

改过以后:
#include<stdio.h>
#include<conio.h>
void main()
{
printf("Hello World!"\n);
getch();
}

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

改过以后:
#include<stdio.h>
#include<stdlib.h>
void main()
{
printf("Hello World!"\n);
system("pause");
}