i have a problem in c language program..

Discussion in 'Programming' started by bhuppi890109, May 7, 2012.

  1. #1
    hi friends...

    i am running a program in turbo c and i have a confusion in it..just check it and tell me how it works.

    #include<stdio.h>
    #include<conio.h>
    main()
    {
    int a=4,b;
    clrscr();
    b=a++ + ++a + a++;

    printf("%d",b);
    getch();
    }

    when i am running this program it is showing the result: 15

    #include<stdio.h>
    #include<conio.h>
    main()
    {
    int a=4;
    clrscr();


    printf("%d",a++ + ++a + a++);
    getch();
    }


    and when i am running this program it is showing: 16



    i am confused about these outputs. both programs has same meaning. but they are showing different output.. can anybody explain with complete solution and exact answer..
    Thanks in advance
     
    bhuppi890109, May 7, 2012 IP
  2. DennisRitchieCollective

    DennisRitchieCollective Peon

    Messages:
    44
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I'm not really sure. Try surrounding the sum of the pre and post increemments of variable a in the "printf" statement with parentheses.
     
  3. DennisRitchieCollective

    DennisRitchieCollective Peon

    Messages:
    44
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It could also be some really weird compiler issue, but I highly doubt that.
     
  4. lockerzking

    lockerzking Greenhorn

    Messages:
    48
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #4
    Its a compiler issue for sure
     
    lockerzking, May 9, 2012 IP
  5. e-abi

    e-abi Member

    Messages:
    122
    Likes Received:
    1
    Best Answers:
    3
    Trophy Points:
    38
    #5
    This one should give your answer:

    http://en.wikipedia.org/wiki/Sequence_point

    You may also try surrounding the upper code with parentheses and try to see what answer comes out.
    Also running the same code with different compiler may yield different results.

    In short it is not recommended to use code like that, since the behaviour is undefined.
     
    e-abi, May 10, 2012 IP