element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Code Exchange
  • Technologies
  • More
Code Exchange
Forum multiple pre increment/post increment in expression of C language
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Code Exchange to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Suggested Answer
  • Replies 35 replies
  • Answers 3 answers
  • Subscribers 51 subscribers
  • Views 15460 views
  • Users 0 members are here
  • increment
  • c
  • in
  • Code Exchange
  • post
  • single
  • precedency
  • operator
  • left
  • value
  • pre
  • expression
  • right
  • turbo
Related

multiple pre increment/post increment in expression of C language

dr.akshay_1980@yahoo.com
dr.akshay_1980@yahoo.com over 9 years ago

I would like to know the explanation for the following expression evaluation in C under windows TURBO c..

void main()

{

     int i=4;

    int x;

    x= ++i + ++i + ++i;   // i becomes 5 then 6 added becomes 11 and then incremented to 12 so 11+12=23   

    printf("%d",x);

    getch();

}

when the above was executed value of x comes out to be 21

 

Slightly eliminating the x variable in the above

 

void main()

{

    int i=4;

    printf("%d", ++i + ++i + ++i);   //accordingly at this step  5+6+7=18

    getch();

}

when the above is executed the answer comes out to be 18


Can anyone provide a decent explanation step by step and why two answers are different???


Thanks in advance.

Akshay


  • Sign in to reply
  • Cancel

Top Replies

  • michaelkellett
    michaelkellett over 9 years ago in reply to dr.akshay_1980@yahoo.com +3
    If anyone asked you that (kind of) question at an interview (unless, perhaps, the job was to revise the C standard specification) you should explain that the only reasonable approach to such code was to…
  • gadget.iom
    gadget.iom over 9 years ago in reply to dr.akshay_1980@yahoo.com +2
    Ahhh... Complete code usually helps As for the problem... You have fallen victim to undefined behaviour as a result of each compilers interpretation of ambiguous code.
  • shabaz
    shabaz over 9 years ago in reply to dr.akshay_1980@yahoo.com +1
    Oh. I now get this is purely an academic question (and a very esoteric one not in my area of interest, sorry!) In real life people wouldn't write that code.. perhaps it is tested nowadays and therefore…
Parents
  • gadget.iom
    0 gadget.iom over 9 years ago

    This might be your issue:

    Akshay Sharma wrote:

     

    under windows TURBO c..

    I cannot even get your code to compile without errors.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • dr.akshay_1980@yahoo.com
    0 dr.akshay_1980@yahoo.com over 9 years ago in reply to gadget.iom

    Hi Paul,

                   My code is pretty simple and its bug free. I just want to know the logic behind both the above cases for giving different outputs. I executed them in Turbo C compiler and got two different outputs why??

    Case 1

    void main()

    {

         int i=4;

        int x;

        x= ++i + ++i + ++i;    

        printf("%d",x);

        getch();

    }

    output x=21

     

     

    Case 2

    void main()

    {

        int i=4;

        printf("%d", ++i + ++i + ++i);   //accordingly at this step  5+6+7=18

        getch();

    }

    output x=18

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • dr.akshay_1980@yahoo.com
    0 dr.akshay_1980@yahoo.com over 9 years ago in reply to gadget.iom

    Hi Paul,

                   My code is pretty simple and its bug free. I just want to know the logic behind both the above cases for giving different outputs. I executed them in Turbo C compiler and got two different outputs why??

    Case 1

    void main()

    {

         int i=4;

        int x;

        x= ++i + ++i + ++i;    

        printf("%d",x);

        getch();

    }

    output x=21

     

     

    Case 2

    void main()

    {

        int i=4;

        printf("%d", ++i + ++i + ++i);   //accordingly at this step  5+6+7=18

        getch();

    }

    output x=18

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
  • gadget.iom
    0 gadget.iom over 9 years ago in reply to dr.akshay_1980@yahoo.com

    image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • dr.akshay_1980@yahoo.com
    0 dr.akshay_1980@yahoo.com over 9 years ago in reply to gadget.iom

    imageimage

     

     

    Hi Paul,

    I have just compiled them in codepad.org and ideone.com     just see the following links

    http://ideone.com/wxl59o

    http://codepad.org/X8PGNWlp

    The answer comes out to be 19 but in turbo c++ 3.0 IDE it is 21

    How could you please explain step by step.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • gadget.iom
    0 gadget.iom over 9 years ago in reply to dr.akshay_1980@yahoo.com

    Ahhh... Complete code usually helps image

     

    As for the problem...

    You have fallen victim to undefined behaviour as a result of each compilers interpretation of ambiguous code.

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • shabaz
    0 shabaz over 9 years ago in reply to gadget.iom

    Am I seeing right? That's a 24-year old compiler and IDE?

    Akshay, GCC is free, and can run in a Linux VM (I presume you're running a VM since you performed a screen capture of the Turbo C++ IDE).

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • dr.akshay_1980@yahoo.com
    0 dr.akshay_1980@yahoo.com over 9 years ago in reply to gadget.iom

    That's indeed true, compilers have shown be different results, but what could be the best possible answer as far as theoretical answer is concerned is it 19, 21 or 18??? I am still confused. assume this is asked in interview round.

    I know GCC compilers under linux follow different rules than turbo c..

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • dr.akshay_1980@yahoo.com
    0 dr.akshay_1980@yahoo.com over 9 years ago in reply to shabaz

    yup thats true shabaz, its old compiler, I just want to see the behaviour of two different compilers for same query , other one GCC under linux.. but could not understand the answer... still puzzled.. how its 19.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • shabaz
    0 shabaz over 9 years ago in reply to dr.akshay_1980@yahoo.com

    Oh. I now get this is purely an academic question (and a very esoteric one not in my area of interest, sorry!)

    In real life people wouldn't write that code.. perhaps it is tested nowadays and therefore has some defined behavior, but 24 years ago.. maybe not.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • dr.akshay_1980@yahoo.com
    0 dr.akshay_1980@yahoo.com over 9 years ago in reply to shabaz

    yes its  academic but believe me it was asked me in the interview,but this evaluates to output as 19. how?? still haunting me, being an embedded developer couldn't crack it??

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • michaelkellett
    0 michaelkellett over 9 years ago in reply to dr.akshay_1980@yahoo.com

    If anyone asked you that (kind of) question at an interview (unless, perhaps, the job was to revise the C standard specification) you should explain that the only reasonable approach to such code was to remove it or not write it in the first place. If the interviewer objects to such an answer you should ask for a copy of the relevant C standard and work through it with them out loud (no one should be expected to remember the rules for this kind of silliness).

     

    If these approaches are not acceptable you don't want to work with such a person (it will end in tears).

     

    MK

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • neuromodulator
    0 neuromodulator over 7 years ago in reply to dr.akshay_1980@yahoo.com

    5+6+7 = 18, while 7+7+7 = 21. When in doubt of what's going on, compilers allow you to check assembly behind each line of code. Now about this specific issue I'm not sure why it's evaluating it differently. I suspect it may me an example of undefined behavior, but to be sure you would have to check the standard. Still nobody would code like that in "real code".

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube