2,739 views
1 votes
1 votes

Assume that the size of an integer is 4 bytes. Predict the output?

#include <stdio.h>
int fun()
{
    puts(" Hello ");
    return 10;
}
 
int main()
{
    printf("%d", sizeof(fun()));
    return 0;
}

2 Answers

Best answer
4 votes
4 votes

It returns the size of the return type from that function (4 on my implementation since that's what an int takes up for me), which you would discover had you run it as is, then changed the return type to char (at which point it would give you 1).

The relevant part of the C99 standard is 6.5.3.4.The sizeof operator:

The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.

Keep in mind that bold bit, it means that the function itself is not called (hence the printf within it is not executed). In other words, the output is simply the size of your int type. i.e. 4.

selected by
0 votes
0 votes
answer will be 4 bcz here return type of function is intger

Related questions

0 votes
0 votes
1 answer
1
SSR17 asked Feb 29
204 views
#include <stdio.h int main() { int i = -1; int x = (unsigned char)i; printf("%d", x); return 0; }output is 255 , but please explain how
2 votes
2 votes
1 answer
2
rupamsardar asked Aug 30, 2023
466 views
#include <stdio.h int f(int x) { if(x%2==0) { return f(f(x-1)); } else return (x++); } int main() { printf("%d",f(12)); ret...
5 votes
5 votes
2 answers
3
saurabh0709 asked Aug 1, 2023
1,136 views
What will be the output of the following code? _______ #include <stdio.h int main(){ char val=250; int ans; ans= val+ !val + ~val + ++val; printf("%d", ans); return 0; }
2 votes
2 votes
1 answer
4