What is printed by this C code: char s = “hello”; printf(“%zu”, sizeof(s));?
Correct answer A. 6
Explanation
When a char array is initialised from a string literal without a size, its length includes the terminating null character, so s has 6 elements. sizeof gives the size of the whole array in bytes, 6. strlen(s) would be 5 because it stops before the null. Answer 8 would be sizeof a char pointer on a 64-bit system, which applies only if s were declared as char *s.
Answer checked How we write and check questions
Discussion