关于scanf()输入超长字符导致后续变量被覆盖的技术咨询
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:
Residual characters in the input buffer
When you type more than 19 characters forFirstName,scanfreads the first 19, but the extra characters (plus the newline you hit to submit) stay stuck in the input buffer. The nextscanf("%19s", LastName)doesn't wait for new input—it immediately grabs those leftover characters and shoves them intoLastName. If there are still characters left after that, thescanf("%4d", &BirthYear)will try to parse them as an integer, which can lead to garbage values (or even a failed read that leavesBirthYearwith an uninitialized, random value). This makes it look likeLastNameorBirthYearwas "overwritten" by your initial long input.Actual buffer overflow (if you weren't using the width specifier)
If you didn't have the%19sand just used%s, C doesn't do any bounds checking on the array. Typing more than 19 characters would write past the end ofFirstNamedirectly into the memory space allocated forLastName(and thenBirthYear). 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 usingscanffor strings, you can flush any leftover characters (including the newline) like this:// After reading FirstName while (getchar() != '\n'); // Discard everything until the newlineThis ensures the next
scanfstarts fresh, waiting for your input instead of grabbing leftovers.Use
fgetsinstead ofscanffor string inputfgetsreads 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

