简体   繁体   English

VS Code C 程序正在运行但未显示任何内容

[英]VS Code C program is running but not showing anything

I am trying to run the c program on my vs code.我正在尝试在我的 vs 代码上运行 c 程序。 I have code runner and c/c++ extension installed in vs code.我在 vs 代码中安装了代码运行器和 c/c++ 扩展。 Whenever I try to run the program it's always running not showing any output or result of my program.每当我尝试运行该程序时,它总是在运行而不显示我的程序的任何输出或结果。

It's happening with this specific code.它发生在这个特定的代码中。

#include<stdio.h>
int main()
{
    int a[10],i,j,temp,n;
    printf("\n Enter the max no.of Elements to Sort: \n");
    scanf("%d",&n);
    printf("\n Enter the Elements : \n");
    for(i=0; i<n; i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=0; i<n; i++)
        for(j=i+1; j<n; j++)
        {
            if(a[i]>a[j])
            {
                temp=a[i];
                a[i]=a[j];
                a[j]=temp;
            }
        }
    for(i=0; i<n; i++)
    {
        printf("%d\t",a[i]);
    }
    return 0;
}

Here you can see what's the problem.在这里您可以看到问题所在。 It's running for 2-3 min but not showing any output.它运行了 2-3 分钟,但没有显示任何输出。

[Running] cd "c:\\Users\\rakib\\Downloads\\code" && gcc labreport.c -o labreport && "c:\\Users\\rakib\\Downloads\\code"labreport [运行] cd "c:\\Users\\rakib\\Downloads\\code" && gcc labreport.c -o labreport && "c:\\Users\\rakib\\Downloads\\code"labreport

You are creating an array of size 10 on the stack您正在堆栈上创建一个大小为 10 的数组

int a[10];

But then you ask the user to enter how many elements they want to sort.但随后您要求用户输入他们想要排序的元素数量。 If the user wants to sort more than 10 elements then your code would crash since your array only has enough space for 10 integers.如果用户想要对 10 个以上的元素进行排序,那么您的代码将崩溃,因为您的数组只有 10 个整数的足够空间。 To solve the problem you would need to use a dynamic array and allocate it on the heap.要解决此问题,您需要使用动态数组并将其分配在堆上。

int *a = (int*)malloc(n*sizeof(int));

Don't forget to include malloc.h library and to free the array when you are finished using it不要忘记包含malloc.h库并在使用完数组后释放它

free(a);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM