pre and post increment

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • matangi
    New Member
    • Aug 2010
    • 1

    #1

    pre and post increment

    int main()
    {
    int a=5,s;
    s=++a + ++a;
    printf("%d",a);
    printf("%d",s);
    }

    output is 7 and 14

    BUT

    int main()
    {
    int a, s;
    printf("Enter value of a");
    scanf ("%d",&a);
    s=++a + ++a;
    printf("%d",a);
    printf("%d",s);
    }

    input user gives is 5
    output is 7 and 13

    WHY?
  • zahraZ
    New Member
    • Jul 2010
    • 17

    #2
    hello,
    i test your codes but they have same result!(7 & 14)

    Comment

    • newb16
      Contributor
      • Jul 2008
      • 687

      #3
      Because the result of ++a + ++a is undefined per the language standard. (see two similar topics below). Value of 'a' is guaranteed to be 7 after evaluating of this expression.

      Comment

      Working...