You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VSCode中C程序输出垃圾值,在线编译器运行正常的问题求助

问题:C代码在VSCode运行输出垃圾值,在线编译器正常

这段用于录入并展示银行客户信息的C代码,在VSCode运行时输出垃圾值,但在线编译器运行正常,排查代码无果,寻求解决方法。

原代码

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>

typedef struct Customer {
    char name[20];
    int date;
    int month;
    int year;
    int amount;
    int type_of_account;
} cus;

void display(int n);
void data_entry_of_customers(int n);

int main() {
    int i, n, choices;
    printf("\n Enter the number of customer you want to enter their data : ");
    scanf("%d", &n);
    do {
        printf("\n Enter your choice :");
        scanf("%d", &choices);
        switch (choices) {
        case 1:
            data_entry_of_customers(n);
            break;

        case 2:
            display(n);
            break;

        case 3:
            exit(0);
            break;

        default:
            {
                printf("\n Invalid Choice");
            }
        }
        i++;
    } while (choices != 3);
    getch();
    return 0;
}

void data_entry_of_customers(int n) {
    cus c[n];
    int i;
    for (i = 0; i < n; i++) {
        printf("\n Enter the name of the customer%d : ", i + 1);
        scanf("%s", c[i].name);
        printf("\n Enter the date when the account was opened by customer : ");
        scanf("%d", &c[i].date);
        printf("\n Enter the month when the account was opened by customer : ");
        scanf("%d", &c[i].month);
        printf("\n Enter the year when the account was opened by customer : ");
        scanf("%d", &c[i].year);
        printf("\n Enter the amount disposited by the customer : ");
        scanf("%d", &c[i].amount);
    }
}

void display(int n) {
    cus c[n];
    int i;
    for (i = 0; i < n; i++) {
        printf("\n The name of the customer%d is %s", i + 1, c[i].name);
        printf("\n The date-month-year when the account was opened by customer%d is %d-%d-%d", i + 1, c[i].date, c[i].month, c[i].year);
        printf("\n The amount when the account was opened by customer%d is %d", i + 1, c[i].amount);
        printf("\n");
    }
}

运行现象

录入客户信息后选择展示,输出内容为乱码和随机数字(垃圾值),示例截图如下:
运行截图1
运行截图2

问题根源

核心问题是局部变量的作用域与生命周期:

  • 在data_entry_of_customers和display函数中,各自定义了局部数组cus c[n],这类局部变量存储在栈内存中,函数执行完毕后会立即被销毁,内存空间被释放。
  • 录入的数据仅存在于data_entry_of_customers函数运行期间,调用display时,它自己的c[n]是未初始化的栈内存,里面的内容是随机的垃圾值。
  • 在线编译器偶尔正常是因为栈内存未被其他操作覆盖,属于未定义行为,不具备可靠性。

解决方法

方法1:在main中定义数组并传递指针

在main函数中创建数组,将指针传递给录入和展示函数,确保数据存储在生命周期覆盖整个程序的内存区域:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>

typedef struct Customer {
    char name[20];
    int date;
    int month;
    int year;
    int amount;
    int type_of_account;
} cus;

void display(cus *c, int n);
void data_entry_of_customers(cus *c, int n);

int main() {
    int i = 0, n, choices;
    printf("\n Enter the number of customer you want to enter their data : ");
    scanf("%d", &n);
    cus c[n]; // main中的数组,生命周期覆盖整个程序运行期间
    
    do {
        printf("\n Enter your choice :");
        scanf("%d", &choices);
        switch (choices) {
        case 1:
            data_entry_of_customers(c, n);
            break;

        case 2:
            display(c, n);
            break;

        case 3:
            exit(0);
            break;

        default:
            printf("\n Invalid Choice");
        }
        i++;
    } while (choices != 3);
    getch();
    return 0;
}

void data_entry_of_customers(cus *c, int n) {
    int i;
    for (i = 0; i < n; i++) {
        printf("\n Enter the name of the customer%d : ", i + 1);
        scanf("%s", c[i].name);
        printf("\n Enter the date when the account was opened by customer : ");
        scanf("%d", &c[i].date);
        printf("\n Enter the month when the account was opened by customer : ");
        scanf("%d", &c[i].month);
        printf("\n Enter the year when the account was opened by customer : ");
        scanf("%d", &c[i].year);
        printf("\n Enter the amount disposited by the customer : ");
        scanf("%d", &c[i].amount);
    }
}

void display(cus *c, int n) {
    int i;
    for (i = 0; i < n; i++) {
        printf("\n The name of the customer%d is %s", i + 1, c[i].name);
        printf("\n The date-month-year when the account was opened by customer%d is %d-%d-%d", i + 1, c[i].date, c[i].month, c[i].year);
        printf("\n The amount when the account was opened by customer%d is %d", i + 1, c[i].amount);
        printf("\n");
    }
}

方法2:使用动态内存分配+全局指针

通过malloc动态分配内存,结合全局指针让所有函数共享数据,适合客户数量较大的场景:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>

typedef struct Customer {
    char name[20];
    int date;
    int month;
    int year;
    int amount;
    int type_of_account;
} cus;

cus *c; // 全局指针
int customer_count;

void display();
void data_entry_of_customers();

int main() {
    int i = 0, choices;
    printf("\n Enter the number of customer you want to enter their data : ");
    scanf("%d", &customer_count);
    // 动态分配内存,避免栈溢出
    c = (cus*)malloc(customer_count * sizeof(cus));
    if (c == NULL) {
        printf("Memory allocation failed");
        exit(1);
    }
    
    do {
        printf("\n Enter your choice :");
        scanf("%d", &choices);
        switch (choices) {
        case 1:
            data_entry_of_customers();
            break;

        case 2:
            display();
            break;

        case 3:
            free(c); // 释放内存,避免泄漏
            exit(0);
            break;

        default:
            printf("\n Invalid Choice");
        }
        i++;
    } while (choices != 3);
    getch();
    return 0;
}

void data_entry_of_customers() {
    int i;
    for (i = 0; i < customer_count; i++) {
        printf("\n Enter the name of the customer%d : ", i + 1);
        scanf("%s", c[i].name);
        printf("\n Enter the date when the account was opened by customer : ");
        scanf("%d", &c[i].date);
        printf("\n Enter the month when the account was opened by customer : ");
        scanf("%d", &c[i].month);
        printf("\n Enter the year when the account was opened by customer : ");
        scanf("%d", &c[i].year);
        printf("\n Enter the amount disposited by the customer : ");
        scanf("%d", &c[i].amount);
    }
}

void display() {
    int i;
    for (i = 0; i < customer_count; i++) {
        printf("\n The name of the customer%d is %s", i + 1, c[i].name);
        printf("\n The date-month-year when the account was opened by customer%d is %d-%d-%d", i + 1, c[i].date, c[i].month, c[i].year);
        printf("\n The amount when the account was opened by customer%d is %d", i + 1, c[i].amount);
        printf("\n");
    }
}

额外注意点

  • 原代码中main函数的i变量未初始化,属于不良编码习惯,已在修改后的代码中补全初始化int i = 0;。
  • conio.h是非标准库,部分编译器可能不支持,如果遇到编译错误,可以移除该头文件和getch()函数。

内容的提问来源于stack exchange,提问作者Anirban Saha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 23:25:19