programming help

Discussion in 'C' started by altafahmed2k4, Aug 26, 2006.

  1. altafahmed2k4

    altafahmed2k4 New Member

    Joined:
    Aug 26, 2006
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    main()
    {
        int a=2;
        printf("%d %d %d",a,a++,++a);
    }
    for the above program igot the result as 4,3,3
    but
    Code:
    main()
    {
        int a=2;
        printf("%d",a);
        printf("%d",a++);
        printf("%d",++a);
    }
    fetched me this result 2,2,4

    may i know why?
    altafahmed2k4@yahoo.co.in
     
    Last edited by a moderator: Aug 26, 2006
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Welcome to G4EF.

    Regarding the code snippets that you post in the threads please do use bb code to format the code snippets.

    Because it calculates right to left.

    See the out put of the following code
    Code:
    main()
    {
        int a=2;
        printf("%d",++a);
        printf("%d",a++);
        printf("%d",a);
    }
    
     
  3. Aztec

    Aztec New Member

    Joined:
    May 9, 2006
    Messages:
    90
    Likes Received:
    0
    Trophy Points:
    0
    C standard doesn't say anything regarding order in which arguments are evaluated. It could be right-to-left , left-to-right or maybe something else. Try to write a code which is independent of order in which arguments are evaluated.
    The OP's code has undefined behaviour.
     
    Last edited: Sep 1, 2006
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Yes thats true and also its an unwritten fact that C compilers calculate right to left where as some programming language specially if I am correct its Fortran calculates it from left to right.
     
  5. Aztec

    Aztec New Member

    Joined:
    May 9, 2006
    Messages:
    90
    Likes Received:
    0
    Trophy Points:
    0
    You can not say that. It might be happening this way in your compiler but there's no such rule.
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    I have tested the same on 5 main compilers when I was in my college

    TurboC2, TurboC3, BorlandC, Microsoft Visual studio and gcc compiler but there is no rule for behaving such.
     
  7. Aztec

    Aztec New Member

    Joined:
    May 9, 2006
    Messages:
    90
    Likes Received:
    0
    Trophy Points:
    0
    TurboC2, TurboC3--->Total Crap
    BorlandC, Microsoft Visual studio and gcc compiler--->Yaa right What should I assume about version numbers.
    And why are you defending your wrong statement?
    You can not assume any particular order. It varies from compiler to compiler.

    In short, code has undefined behaviour.That's it.
     
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    I am not defending the statement. I am just saying what I mentioned earlier its <<unwritten fact that C compilers calculate right to left>>. I knew what you are mentioning about the undefined behaviour and so I wrote it the unwritten fact. I never said it behaves in such a manner. isnt it.
    From 5.x,
    6,7,8 and
    I dont remember which one it was.
     

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