Result = 5
Why?
Isn't the compiler supposed to add an extra "null" character at the end automatically.
Also, can someone please give an example of why the NULL character is necessary?
Arrays are usually addressed to the specific array number anyway. In what situation would I have "random data" come in?
Thanks
initializes the elements explicitly, thus it would be stupid to "automatically" add a terminating null. Arrays are used for all kinds of purposes, it should be left up to the programmer what they want to do with them.
However when you specify a string as a string constant, for example
char test1[] = "chars";
it's obvious that a character string is desired, so test1 will have a null appended and be 6 bytes long.
A null character is required, because some method is needed to signal the end of a string. How else would you like to do it?
When was the last time you typed into a computer? That would be "random data coming in"...
initializes the elements explicitly, thus it would be stupid to "automatically" add a terminating null. Arrays are used for all kinds of purposes, it should be left up to the programmer what they want to do with them.
However when you specify a string as a string constant, for example
char test1[] = "chars";
it's obvious that a character string is desired, so test1 will have a null appended and be 6 bytes long.
A null character is required, because some method is needed to signal the end of a string. How else would you like to do it?
When was the last time you typed into a computer? That would be "random data coming in"...
Got it. Thanks. Do you have an example of where NOT including a NULL char will cause erroneous results?