why sizeof is not changing the value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maheshgupta024
    New Member
    • Jun 2008
    • 14

    #1

    why sizeof is not changing the value

    main()
    {
    int temp = 3;
    int x = sizeof(temp++);
    printf("%d\n", temp);
    }

    In the above piece of code. I find that variable temp is not changed after sizeof operation. Can anyone tell me what is the reason for this.
  • Meetee
    Recognized Expert Contributor
    • Dec 2006
    • 928

    #2
    sizeof returns the size of the type of the variable which is 4 in this case (integer type). sizeof is performed at compile-time with the type of the variable. It doesn't do any evaluation on value of variable.

    Please use code quotes around your piece of code.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Annex J.1 of the C99 specification provides a list of situations that produce unspecified behavior. The following item from that list is germane to your question:
      Whether or not a size expression is evaluated when it is part of the operand of a sizeof operator and changing the value of the size expression would not affect the result of the operator (6.7.5.2).

      Comment

      Working...