I was trying different ways to declare a string in C for exam preparation. We know that string in C is character array with '\0' at end. Then I found that even if I declare an array of 5 characters and put 5 characters in it like "abcde" it is accepted. Then where is the null char stored?
I declared strings in following ways
char str[] = {'a','b','c','d','e'};
char str2[] = "abcde";
char str3[5] = "abcde";
Now, in the 3rd case, I am allocating 5 byte of space and I have exactly 5 characters in the array, then if string should have a null character at end where is it being stored? Or is it the case that null is not appended?
What about the 1st and 2nd cases, are null appended there?
strlen() returns 5 in all 3 cases.
char str4[] = "abc\0\0";.sizeof(str4)--> 6 andstrlen(str4)--> 3.