overwriting character arrays in C
Imported from the Apple Support Forums via the Wayback Machine 🔗
So, I've noticed that if I have code like this:
char userName[32];
scanf("%s", userName);
printf(" %s", userName);
When I run the program, when it comes time to type in my name, I've noticed that I can enter a name well over 32 characters, and the next statement will still print back the full 32+ character string, showing that, even though userName was declared to hold 32 characters or less, it still can hold more than that.
My instinct is to ask why C allows you to do this, but I'm going to try to stick to the relevant question here, which is -- is there a reason that I should refrain from letting C overwrite the character array like this? Does it matter if my user (not that I have a user, but for argument's sake) enters something over 32 characters, since the array will hold it anyways?
6 replies
Sign in or join Beachball Help to reply.
- 0
- 0
- 0
- 0
- 0
- 0