Char array capacity | Sololearn: Learn to code for FREE!

0

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?

10/15/2019 8:16:41 AM

SangMinCNSA

1 Answer

New 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