Q. What will be the output of the following C code?
Code:
#include <stdio.h>
int main()
{
char str1[] = { 'H', 'e', 'l', 'l', 'o' };
char str2[] = "Hello";
printf("%ld,%ld", sizeof(str1), sizeof(str2));
return 0;
}
β
Correct Answer: (C)
5,6
Explanation: str1 is initialized with the characters and there are only 5 characters. Thus, the length of the str is 5. While, str2 is initialized with the string "Hello", when we initialized the string in this way - a null ('\0') character is inserted after the string. Thus, the length of str2 is 6.