My question is about the sizeof operator in C.
sizeof('a'); equals 4, as it will take 'a' as an integer: 97.
sizeof("a"); equals 2: why? Also (int)("a") will give some garbage value. Why?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
'a'is a character constant – of typeintin standard C – and represents a single character."a"is a different sort of thing: it’s a string literal, and is actually made up of two characters:aand a terminating null character.A string literal is an array of
char, with enough space to hold each character in the string and the terminating null character. Becausesizeof(char)is1, and because a string literal is an array,sizeof("stringliteral")will return the number of character elements in the string literal including the terminating null character.That
'a'is anintinstead of acharis a quirk of standard C, and explains whysizeof('a') == 4: it’s becausesizeof('a') == sizeof(int). This is not the case in C++, wheresizeof('a') == sizeof(char).