What is the length of string when we don't explicitly initialize a character array with null character?
The length of the string is the number of characters it contains. (Let us ignore for simplicity the fact that there is a difference between the number of graphemes, code points and code units, all of which are the length of the string depending on perspective. In the context of this answer, character == code unit).
The length of a null terminated string is the number of characters before the null terminator. If a string doesn't contain a null terminator, then it isn't a null terminated string.
strlen(b)
b
isn't a null terminated string. strlen
requires that the argument points to a null terminated string. If the requirement isn't satisfied, the behaviour of the program is undefined.
But its working except that for giving o/p 13, its giving output 26
The behaviour is undefined. Possible behaviours include, none of which are guaranteed:
- working
- not working
- random output
- non-random output
- the expected output
- unexpected output
- no output
- any output
- crashing at random
- crashing always
- not crashing
- corruption of data
- different behaviour, when executed on another system
- , when compiled with another compiler
- , on tuesday
- , only when you're not looking
- same behaviour in all of the above cases
- anything else within the power of the computer (hopefully limited by the OS)
Undefined behaviour is undesirable in any program.