1 votes 1 votes What is the output of the following C program? main() { printf("%d%d%d", sizeof('3'), sizeof("3"), sizeof(3)); } a) 111. b)222 c)666 d)333 Programming in C programming-in-c + – Sambhrant Maurya asked Jul 26, 2018 • edited Jul 26, 2018 by srestha Sambhrant Maurya 1.5k views answer comment Share Follow See all 6 Comments See all 6 6 Comments reply Show 3 previous comments srestha commented Jul 26, 2018 reply Follow Share and it includes NULL too 0 votes 0 votes Sambhrant Maurya commented Jul 27, 2018 reply Follow Share If a size of 1 B is assumed for char then shouldn't sizeof('3') return 1 ? 0 votes 0 votes Shaik Masthan commented Jul 27, 2018 reply Follow Share If a size of 1 B is assumed for char then shouldn't sizeof('3') return 1 ? read the comments carefully 0 votes 0 votes Please log in or register to add a comment.
1 votes 1 votes In C sizeof character literal is same as sizeof int. So, it will take 4B. In C, the type of a character constant like 'a' is actually an int, with size of 4 (or some other implementation-dependent value). In C++, the type is char, with size of 1. This is one of many small differences between the two languages. https://stackoverflow.com/questions/2172943/size-of-character-a-in-c-c There are some difference between sizeof and strlen. Sizeof searches for NULL character and counts the number of elements before NULL character, while strlen returns number of character includind NULL. So, that is why "a" returns 2 strlen() searches for that NULL character and counts the number of memory address passed, So it actually counts the number of elements present in the string before the NULL character, here which is 8. sizeof() operator returns actual amount of memory allocated for the operand passed to it. Here the operand is an array of characters which contains 9 characters including Null character and size of 1 character is 1 byte. So, here the total size is 9 bytes. https://www.geeksforgeeks.org/difference-strlen-sizeof-string-c-reviewed/ Atlast sizeof(3) is an integer, which returns sizeof integer i.e.4 srestha answered Jul 26, 2018 • edited Jul 27, 2018 by srestha srestha comment Share Follow See all 7 Comments See all 7 7 Comments reply Show 4 previous comments Shaik Masthan commented Jul 26, 2018 reply Follow Share size of character = 1 and size of int =4 or 2 Bytes in C but the in c,type of character literal is int where 'a' is a character literal it gives the size same as int 1 votes 1 votes srestha commented Jul 26, 2018 reply Follow Share sizeof(char)=sizeof('a')=1 what is difference between character literal and sizeof(char)?? I got both same https://stackoverflow.com/questions/433895/why-are-c-character-literals-ints-instead-of-chars 0 votes 0 votes srestha commented Jul 26, 2018 reply Follow Share oh yes sizeof(char)=1 sizeof('a')=4 #include <stdio.h> int main() { printf("%d%d%d", sizeof('3'), sizeof(char), sizeof(3)); return 0; } Moreover here character promotion taking place 0 votes 0 votes Please log in or register to add a comment.