edited by
1,548 views
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

edited by

1 Answer

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

edited by

Related questions

0 votes
0 votes
1 answer
1
1 votes
1 votes
0 answers
2
srestha asked Mar 6, 2019
791 views
void find(int x){ static int i=10,y=0; y=y+i; for(i;i>0;i=i-10){ if(x!=0) find(x-1); else{ printf("%d",y); } } }What will be output printed for find(4)?
2 votes
2 votes
0 answers
4
srestha asked Mar 5, 2019
1,214 views
What will be output of the program?int d=0; int f(int a,int b){ int c; d++; if(b==3) return a*a*a; else{ c=f(a,b/3); return(c*c*c); } } int main(){ printf("%d",f(4,81)); ...