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

C语言技术问题:如何将标准输入存入数组并正确打印?

问题描述

我正在练习编写C语言程序,重点是读取标准输入(stdin)语句。以下是我编写的用于读取标准输入的代码,但无法将输入内容正确存入数组并打印出正确值。

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

int main(int argc, char* argv[])
{
    int count = 0;
    char* number = "-n";
    int result;
    char numArray[50];

    result = strcmp(argv[1], number);
    if (result == 0) {
        printf("Numbers Only\n");
        while (!feof(stdin)) {
            if (feof(stdin))
                break;

            for (int i=0; i < sizeof(numArray); i++){
                scanf("%s", &numArray[i]);
            }
        }
    }
    for (int i=0; i< sizeof(numArray); i++){
        printf("%d\n", numArray[i]);
    }
}

程序无报错但输出异常,输入1、2、3后按Ctrl+D结束输入,输出含大量额外数值:

1                  (1, 2, 3是我在终端输入的内容)
2
3
49
50
51
0
0
0
0
0
32
-56
109
-63
6
127
0
0
-128
80
110
-63
6
127
0
0
20
0
0
0
0
0
0
0
64
0
0
0
0
0
0
0
0
0
-16
-67
6
127
0
0
4
0

需要解决:

  1. 为何输出额外数值?如何避免?
  2. 如何让数组大小匹配输入数字数量?
问题原因
  1. scanf格式符与目标地址不匹配:用%s读取字符串时,传入&numArray[i](单个char的地址)会导致输入的字符串覆盖数组后续元素,输入的字符1/2/3的ASCII值49/50/51被写入数组,这是输出前几个非输入值的原因。
  2. 遍历整个未初始化数组:numArray大小为50,但仅输入3个值,未被赋值的数组区域保留内存中的随机垃圾值,这就是额外数值的来源。
  3. 循环逻辑错误:内层for循环尝试读取50次输入,而外层feof循环会导致多读一次,进一步破坏数组结构。
解决方法

一、修复输入与输出,避免额外数值

1. 正确读取输入

根据需求选择读取字符或整数:

  • 若存储单个数字字符:
int main(int argc, char* argv[])
{
    char* number = "-n";
    char numArray[50];
    int count = 0;

    if (argc >=2 && strcmp(argv[1], number) == 0) {
        printf("Numbers Only\n");
        char c;
        // 跳过空白字符,只读取数字字符,直到输入结束或数组满
        while (scanf(" %c", &c) == 1 && isdigit(c) && count < sizeof(numArray)-1) {
            numArray[count++] = c;
        }
        numArray[count] = '\0'; // 字符串终止符
    }

    // 仅输出有效输入的元素
    for (int i=0; i < count; i++){
        printf("%c\n", numArray[i]);
    }
    return 0;
}
  • 若存储整数,将数组改为int类型:
int main(int argc, char* argv[])
{
    char* number = "-n";
    int numArray[50];
    int count = 0;

    if (argc >=2 && strcmp(argv[1], number) == 0) {
        printf("Numbers Only\n");
        int num;
        // 读取整数直到输入结束或数组满
        while (scanf("%d", &num) == 1 && count < sizeof(numArray)/sizeof(int)) {
            numArray[count++] = num;
        }
    }

    // 仅输出有效输入的元素
    for (int i=0; i < count; i++){
        printf("%d\n", numArray[i]);
    }
    return 0;
}

2. 关键注意点

  • 用scanf的返回值判断读取是否成功(返回1表示成功读取一个值,返回EOF表示输入结束),不要用feof做循环条件。
  • 始终记录实际输入的元素个数(count),遍历输出时只处理前count个元素,跳过未初始化的数组区域。

二、动态匹配输入数量的数组

如果需要数组大小完全匹配输入数量,使用动态内存分配+扩容:

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

int main(int argc, char* argv[])
{
    char* number = "-n";
    if (argc <2 || strcmp(argv[1], number) != 0) {
        return 1;
    }

    printf("Numbers Only\n");
    int *numArray = NULL;
    int capacity = 2; // 初始容量
    int count = 0;
    int num;

    // 初始化动态数组
    numArray = malloc(capacity * sizeof(int));
    if (!numArray) {
        perror("malloc failed");
        return 1;
    }

    // 读取输入并动态扩容
    while (scanf("%d", &num) == 1) {
        if (count >= capacity) {
            // 容量翻倍
            capacity *= 2;
            int *temp = realloc(numArray, capacity * sizeof(int));
            if (!temp) {
                perror("realloc failed");
                free(numArray);
                return 1;
            }
            numArray = temp;
        }
        numArray[count++] = num;
    }

    // 输出所有输入的元素
    for (int i=0; i < count; i++){
        printf("%d\n", numArray[i]);
    }

    // 释放动态内存
    free(numArray);
    return 0;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 06:50:27