sizeof()

why is the value of val 4?

1
2
3
4
5
6
7
8
9
10
11
#include<stdio.h>
int f(int);
int main(){
    int i=3,val;
    val=sizeof (f(i)+ +f(i=1)+ +f(i-1));
    printf("%d %d",val,i);
    return 0;
}
int f(int num){
        return num*5;
}
Because sizeof() is use to calculate the data type
Function f() returns an integer. Hence the result of evaluating the expression
f(i)+ +f(i=1)+ +f(i-1) is an integer too.

In this case sizeof() is giving the size of an integer which is typically 4 bytes, though it can vary across systems.
Note that sizeof() does not evaluate its argument, it just determines its size. So in the example, f() never actually gets called and i never changes from it's initial value of 3.
Last edited on
Well the value given by sizeof is known at compile time, so the expression isn't actually evaluated. But its type is deduced. For example:
 
val = sizeof (f(i)+ +f(i=1)+ +f(i-1) + 1.0);
gives 8 rather than 4 on my system, as the type of the expression is different.
Topic archived. No new replies allowed.