Char array capacity

I've just tried to enter more characters than I set for char array in C ex) code: char a[9]; input: this definitely is more than 9 characters But it still works! Why is it?

15th Oct 2019, 8:16 AM
SangMinCNSA - avatar
1 Answer
+ 2
Because C lets you do it by not checking the bounds of your array. It's a very simple tradeoff, less security for more speed. What actually happens is called undefined behaviour, i.e. the outcome is compiler-specific, but C does not guarantee for anything here; it might work, it might seem to be working but is actually corrupted, it might crash, everything might be on fire, etc. etc., you get what I mean. For more reference, see the following questions: https://stackoverflow.com/questions/16706459/c-why-string-variable-accepts-more-characters-than-its-size https://stackoverflow.com/questions/30756816/how-string-length-is-printing-more-then-size-of-char-array-if-give-input-more-t https://stackoverflow.com/questions/39188469/char-array-can-hold-more-than-expected/39188510#39188510
15th Oct 2019, 9:07 AM
Shadow - avatar