解决VS CODE运行C程序闪退的问题

在使用VS Code编写一段C程序代码时,代码没有语法问题,按F5进行调试可以启动运行,当输入值后,按下enter键就出现闪退的情况。

下面解决这个问题:

system("pause");   //以便在退出程序前调用系统的暂停命令暂停命令行

在return前一行加上 system(“pause”); 即可,使其在退出程序前调用系统的暂停命令暂停命令行,如果不加的话程序运行就会立即退出。
案例:

#include<stdio.h>
struct Student	/*学生结构*/

{

	char cName[20];	/*姓名*/

	char cSex;	/*性别*/

	int iGrade;	/*年级*/

} student1={"HanXue",'W',3};/*定义变量并设置初始值*/

int main()
{
	struct Student student2={"WangJiasheng",'M',3};	/*定义变量并设置初始值*/
	/*将第一个结构体中的数据输出*/
	printf("the student1's information:\n");
	printf("Name: %s\n",student1.cName);
	printf("Sex: %c\n",student1.cSex);
	printf("Grade: %d\n",student1.iGrade);/*将第二个结构体中的数据输出*/
	printf("the student2's information:\n");
	printf("Name: %s\n",student2.cName);
	printf("Sex: %c\n",student2.cSex);
	printf("Grade: %d\n",student2.iGrade);
    system("pause");   //以便在退出程序前调用系统的暂停命令暂停命令行
	return 0;
}                    

执行结果:

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_41194129/article/details/107902019
今日推荐