Practice mode C Language

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.