in Programming in C
2,760 views
1 vote
1 vote

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;
}
in Programming in C
2.8k views

2 Comments

here the sizeof() is an operator. but fun() does not get invoked. but I don't why it prints 4. is it because return type of fun() is int ?
1
1
Yes it just because of the return type of that function
0
0

2 Answers

4 votes
4 votes
Best answer

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
by
0 votes
0 votes
answer will be 4 bcz here return type of function is intger
by

Related questions