VS Code编写C语言,代码调试之后,能输入值,但按回车之后闪退

在使用VS Code编写一段C程序代码时,代码没有任何问题,按F5进行调试可以启动运行,当输入值后,按下enter键就出现闪退的情况,下面就来带您一起解决这一问题,希望对您有所帮助:

如下小栗子~
更改前的代码:
//打印金字塔
#include<stdio.h>
int main(){ 
    int i,space,rows=0,k=0;
    printf("Enter the number of rows: "); 
    scanf("%d",&rows);
    for(i=1;i<rows;++i){ 
        for(space=1;space<=rows-i;++space){ 
            printf(" ");
        }
        while(k!=2*i-1){ 
            printf("*");
            ++k;
        }
        k=0;
        printf("\n");
    }
    return 0;
}
效果:

《VS Code编写C语言,代码调试之后,能输入值,但按回车之后闪退》

更改后的代码:

其实就只用在控制台应用程序exit(0);前一行加上 system(“pause”); 即可,使其在退出程序前调用系统的暂停命令暂停命令行,如果不加的话程序运行就会立即退出。此时记得加上头文件 #include<stdlib.h>,不加的话会报错喔,如下方可运行。

//打印金字塔
#include<stdio.h>
#include<stdlib.h>  //更改后需要加的头文件
int main(){ 
    int i,space,rows=0,k=0;
    printf("Enter the number of rows: "); 
    scanf("%d",&rows);
    for(i=1;i<rows;++i){ 
        for(space=1;space<=rows-i;++space){ 
            printf(" ");
        }
        while(k!=2*i-1){ 
            printf("*");
            ++k;
        }
        k=0;
        printf("\n");
    }
    system("pause");   //以便在退出程序前调用系统的暂停命令暂停命令行
    return 0;
}
效果:

《VS Code编写C语言,代码调试之后,能输入值,但按回车之后闪退》

    原文作者:^Being^
    原文地址: https://blog.csdn.net/weixin_43802738/article/details/84929739
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞