Can anyone explain why do we need +1 byte for sentinel value?As we know that 1 char = 1 byte so if we declare an array such as char a[50] why I can't store 50 chars instead of 49?
Char array - why do we need +1 byte for sentinel value?
c++;array
Typically C++ users prefer to use std::string where possible. Your question stems from lower-level C-programming and native types (which of course are still relevant in C++, but anyways)Bottom-line, when you pass C-strings around you're simply passing the address of the first byte of the string. A lot of commonly-used string manipulation function have something internally like this:char* c;for (c = str; *c != 0; ++c) {    // Do something}When you're staring out programming in C/C++ you may be tempted to say I know this string is 50 bytes long. But do you really?char myStr[50] = My Name Is Assimilater;It's pretty common to allocate more than the exact number of bytes you need. You wouldn't want printf to copy any garbage characers after Asimilater to the console, now would you?C-String buffers also tend to get reused to store many strings over their lifetime; this isn't a problem so long as the buffer is big enough to handle any string you intend to throw at it.This, coupled with the fact that an algorithm may go off into myStr[51] and beyond if not given a sentinel to stop at, makes the precense of a sentinel value important.