Reputation: 908
Why dose the folowing code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello world!\n");
int x;
long l;
double d;
printf("\n%d",sizeof(x+l+d));
return 0;
}
prints on the console 8?
I originaly thought that will convert x
and l
to double and display 24.
Why 8?
Upvotes: 0
Views: 51
Reputation: 9873
You are only passing one argument to sizeof, and that single argument's type is double
. If you wrote sizeof(x) + sizeof(l) + sizeof(d), that would be something different (although still not 24, because not each argument is double
).
Upvotes: 1
Reputation: 25199
sizeof
returns the number of bytes used to store its argument. In this case its argument is x+l+d
. x
is an int
, l
a long
, and d
a double
. When you add integer types to double
s, the result is promoted to form a double
. So what you have written is the equivalent of sizeof(double)
. A double
takes 8 bytes to store, so you are seeing 8
as the result.
Upvotes: 3