s j
last year
Scountry asked

In C, if I declare char name[20], why can I only enter 19 characters even though the index goes from 0 to 19?

Abhay Jajodia
Expert
last year
Abhay Jajodia answered

Hi s j,

In C, a string needs one extra character at the end called the null terminator '\0' to mark where the text ends.

So char name[20] has 20 slots total:

  • up to 19 slots for the characters you type

  • 1 slot for '\0'

That is why you can only store 19 normal characters, even though the array size is 20.

If you have more questions, I am here to help 😊

C
This question was asked as part of the Learn C Programming course.