Simple c program-please explain output.

Discussion in 'C' started by hitesh.incept, Sep 13, 2012.

  1. hitesh.incept

    hitesh.incept New Member

    Joined:
    Sep 13, 2012
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    int main()
    {
         int a=7, t=0;
         t=--a+--a+a+++a;
         printf("%d",t);
    }
    output: 20

    I could not understand why it prints '20' ? please explain it !
     
  2. hobbyist

    hobbyist New Member

    Joined:
    Jan 7, 2012
    Messages:
    141
    Likes Received:
    0
    Trophy Points:
    0
    I'd suspect it's undefined behavior because you're trying to modify a more than once in a sequence.

    Code:
    int a[] = { 0, 1, 2, 3 },
        b[] = { 4, 5, 6, 7 },
        i=0;
    
    a[i++] = b[++i];
    
    the compiler can't tell which operation it should do first - increment i in a or i in b or assign then increment, etc. The same thing occurs in your snippit.
     
  3. anzul

    anzul New Member

    Joined:
    Sep 16, 2012
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Well I can do that...

    Inititally a was 7.

    Such expressions are explained in the order :
    1. Pre
    2. Expression
    3. Post

    and while solving the expression, a variable has all the same values...

    expression is: --a + --a + a++ + a

    initially a is 7

    1. All pre operations are done... name --a & --a.
    therefore, a becomes 5.
    2. Expression is solved with all the a's as 5...
    Giving you 5 + 5 + 5 + 5 = 20;
    3. Post operations are a++... giving a = 6.

    Hope this helps.
     
  4. HARIT

    HARIT New Member

    Joined:
    Jul 16, 2011
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    but as per operator precedence in c, ++ should evaluate first than =. please explanation?
     
  5. anzul

    anzul New Member

    Joined:
    Sep 16, 2012
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    you are very right HARIT,

    See this page's NOTE 2
    difranco.net/compsci/C_Operator_Precedence_Table.htm

    it states that :

    Postfix increment/decrement have high precedence, but the actual increment or decrement of the operand is delayed (to be accomplished sometime before the statement completes execution). So in the statement y = x * z++; the current value of z is used to evaluate the expression (i.e., z++ evaluates to z) and z only incremented after all else is done.
     
  6. anzul

    anzul New Member

    Joined:
    Sep 16, 2012
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    you are very right HARIT, but


    Postfix increment/decrement have high precedence, but the actual increment or decrement of the operand is delayed (to be accomplished sometime before the statement completes execution). So in the statement y = x * z++; the current value of z is used to evaluate the expression (i.e., z++ evaluates to z) and z only incremented after all else is done.
     
  7. HARIT

    HARIT New Member

    Joined:
    Jul 16, 2011
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    I change the example a little. Now the o/p is
    j=2
    *p=5

    here the statement act like j= --*p and p++. why?
    if the actual increment or decrement of the operand is delayed, then the statement should be act like j=(--*p)++.

    WHY POST INCREMENT OPERATOR INCREMENT THE ADDRESS HOLED BY P, NOT THE VALUE OF P??
     
  8. sumedh yadav

    sumedh yadav New Member

    Joined:
    Aug 18, 2012
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    int main()
    {
         int a=7, t=0;
         t=(--a)+(--a)+a+(++a);
         printf("%d\n",t);
         printf("%d",a);
         return 0;
    }
    
    This code replies 21 instead of 20 because here a++ statement is replaced by ++a(notice the last parenthisis),here actually ( ++a) specifying the we are performing preincrementation,but in the problem that you specified since postincremented order >preincremented therefore answer comes out to be 20.
     
  9. HARIT

    HARIT New Member

    Joined:
    Jul 16, 2011
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    Sorry friend, in TurboC output of this code is
    24
    6

    Here the simple reason, parenthesis work first and value of a incremented by 1 then decremented twice. so now the value of 'a' is 6 and the statement will look like t=6+6+6+6, that returns 24. :)
     
  10. sumedh yadav

    sumedh yadav New Member

    Joined:
    Aug 18, 2012
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    May be the C compiler architecure different from TurboC compiler
     
  11. KRV

    KRV New Member

    Joined:
    Dec 26, 2011
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    LEARNING
    Location:
    SRI LANKA
    a= 10;
    printf("a a++ a a++ a a++ a ++a a \
    %d %d %d %d %d %d %d %d %d\n" \
    , a, a++ ,a ,a++ ,a, a++ , a, ++a, a);

    PLEASE explain precedence within printf different combination gives different answers always..
     
  12. Arup Kr Goswami

    Arup Kr Goswami New Member

    Joined:
    Oct 3, 2015
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Kolkata
    Sorry Friend!! In gcc compiler the output is 21.
    Code:
    #include<stdio.h>
    void main()
    {
         int a=7, t=0;
         t=--a+--a+a+++a;
         printf("%d\n",t);
    }
    
    ar@ar-V:~$ gcc x.c
    ar@ar-V:~$ ./a.out
    21
     
  13. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Hobbyist is correct - the behaviour is UNDEFINED. All arguments of the form "my compiler does X therefore you are wrong" are nonsense.

    Undefined means the exact implementation is up to the compiler writer. So you could get different results from different compilers (and as we can see from the discussion, that's exactly what's happening).

    All x++ means is that x is incremented "some time" after the value of x is taken. And ++x means x is incremented "some time" before the value is taken. The precise definition of "some time" is left to the compiler writers.

    So the code int x=0; int i=x++ + x++; could take the value 0 and increment x to 1, then take the value 1 and increment x to 2, making i=1. Or it could take the value 0 twice, perform the addition, then increment x twice, making i=0. Both are equally valid.

    The best solution here is NOT to use more than one pre-/post-/inc-/dec-rement operator in an expression, then to modify the variables in separate code. If the intent of "t=--a+--a+a+++a;" was to do something like: "int a=7; a-=2; t=4*a; a++;", then that is what you should do, and this will be both clear and portable, and more importantly, defined (a will be 6 at the end, and t will be 4*5=20).
     
    shabbir likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice