1

Possible Duplicate:
Why does sizeof(x++) not increment x?

#include<stdio.h>
int main(void)
{
  double num=5.2;
  int var=5;
  printf("%d\t",sizeof(!num));
  printf("%d\t",sizeof(var=15/2));
  printf("%d",var);
  return 0;
}

The program gave an output 4 4 5. I didn't quite get why this happened.

  • Why was the first output 4?
  • Why didn't the value of var get updated to 7?

How does the sizeof operator work?

Community
  • 1
  • 1
sasidhar
  • 7,523
  • 15
  • 49
  • 75
  • while i accept my question happened to be a duplicate of another question on SO, a more appropriate heading to the other question and I might have never put up the question. Didn't turn up in search. Anyways thanks guys. – sasidhar Jul 24 '12 at 11:58

2 Answers2

3

The expressions

!num
var=15/2

both evaluate to an int. sizeof(int) is always 4 on your platform.

As for why var is not updated:

sizeof is a compile-time operator, so at the time of compilation sizeof and its operand get replaced by the result value. The operand is not evaluated (except when it is a variable length array) at all; only the type of the result matters.

https://stackoverflow.com/a/8225813/141172

Community
  • 1
  • 1
Eric J.
  • 147,927
  • 63
  • 340
  • 553
1

The sizeof operator shows you the size of the darts type of its argument. It does not return the result of the argument. On your platform an integer is 4 bytes long, that's what you learn here. If you want the result, just cut out the sizeof.

steffen
  • 8,572
  • 11
  • 52
  • 90