簡體   English   中英

VS Code C 程序正在運行但未顯示任何內容

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

我正在嘗試在我的 vs 代碼上運行 c 程序。 我在 vs 代碼中安裝了代碼運行器和 c/c++ 擴展。 每當我嘗試運行該程序時,它總是在運行而不顯示我的程序的任何輸出或結果。

它發生在這個特定的代碼中。

#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;
}

在這里您可以看到問題所在。 它運行了 2-3 分鍾,但沒有顯示任何輸出。

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

您正在堆棧上創建一個大小為 10 的數組

int a[10];

但隨后您要求用戶輸入他們想要排序的元素數量。 如果用戶想要對 10 個以上的元素進行排序,那么您的代碼將崩潰,因為您的數組只有 10 個整數的足夠空間。 要解決此問題,您需要使用動態數組並將其分配在堆上。

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

不要忘記包含malloc.h庫並在使用完數組后釋放它

free(a);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM