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

关于scanf()输入超长字符导致后续变量被覆盖的技术咨询

Why do longer inputs overwrite subsequent variables in my C code?

Ah, this is a classic buffer overflow (and input buffer residue) issue in C—let's break down exactly what's happening here.

First, let's look at your variable setup:

char FirstName[20];
char LastName[20];
int BirthYear;

In most C compilers, local variables like these are stored on the stack, which typically grows from higher memory addresses to lower ones. That means your variables are laid out contiguously in memory (the exact order depends on your compiler, but they're right next to each other).

What's causing the overwrite?

You're using scanf("%19s", FirstName) which does limit reads to 19 characters (leaving space for the null terminator \0 at the end of the 20-character array)—so why are subsequent variables getting overwritten? There are two related scenarios here:

  1. Residual characters in the input buffer
    When you type more than 19 characters for FirstName, scanf reads the first 19, but the extra characters (plus the newline you hit to submit) stay stuck in the input buffer. The next scanf("%19s", LastName) doesn't wait for new input—it immediately grabs those leftover characters and shoves them into LastName. If there are still characters left after that, the scanf("%4d", &BirthYear) will try to parse them as an integer, which can lead to garbage values (or even a failed read that leaves BirthYear with an uninitialized, random value). This makes it look like LastName or BirthYear was "overwritten" by your initial long input.

  2. Actual buffer overflow (if you weren't using the width specifier)
    If you didn't have the %19s and just used %s, C doesn't do any bounds checking on the array. Typing more than 19 characters would write past the end of FirstName directly into the memory space allocated for LastName (and then BirthYear). Since stack variables are adjacent, those extra bytes would overwrite whatever was stored there—completely messing up your other variables. Even with %19s, if you somehow managed to bypass the limit (unlikely here), this same overflow would happen.

How to fix this?

Here are two solid approaches to avoid this issue:

  • Clear the input buffer after each read
    After using scanf for strings, you can flush any leftover characters (including the newline) like this:

    // After reading FirstName
    while (getchar() != '\n'); // Discard everything until the newline
    

    This ensures the next scanf starts fresh, waiting for your input instead of grabbing leftovers.

  • Use fgets instead of scanf for string input
    fgets reads an entire line of input (including the newline) and lets you specify the maximum length, which is safer:

    printf("Input your First Name: ");
    fgets(FirstName, sizeof(FirstName), stdin);
    // Remove the trailing newline from fgets
    FirstName[strcspn(FirstName, "\n")] = '\0';
    

    This avoids residual buffer issues entirely, since it reads up to the newline (or the array limit, whichever comes first).

For the integer input, you should also handle any residual characters to prevent similar issues—combining fgets with sscanf is a good approach here too.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 17:33:15