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
  • phoenixcomm
    0 phoenixcomm over 6 years ago

    This is a great example of what NEVER to do and expect the correct answer.

     

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

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

    What is going on in C is that ++I, (pre-inc),  i++ (post-inc) and as well as +=, -=, *=, /=, and %= are called
    basic mathamatical operators, as well as =, +, -, *, /, %.

    With that being said lets get down to a C statement in BNF: (Backus-Naur Form)

    This grammar was adapted from Section A13 of The C programming language, 2nd edition, by Brian W. Kernighan and Dennis M. Ritchie,Prentice Hall, 1988.

    <unary-expression> ::= <postfix-expression> | ++ <unary-expression> | -- <unary-expression> |

    <unary-operator> <cast-expression> | sizeof <unary-expression> | sizeof <type-name>

     

    <postfix-expression> ::= <primary-expression> | <postfix-expression> [ <expression> ] |

    <postfix-expression> ( {<assignment-expression>}* ) | <postfix-expression> . <identifier> | <postfix-expression> -> <identifier> | <postfix-expression> ++ | <postfix-expression>

    <unary-operator> ::= & | * | + | - | ~ | !

    <expression> ::= <assignment-expression>  | <expression> , <assignment-expression>

    <assignment-expression> ::= <conditional-expression>  | <unary-expression> <assignment-operator> <assignment-expression>

    <assignment-operator> ::= = | * |  /=  | %=  | +=  | -=  | <<=  | >>=  | &=  | ^= | |=

     

    In Summary: Well you just can't do it. It is not valid code. In effect, you were making a statement that was like this

    Variable X = prefix-expression operator prefix-expression operator prefix-expression eol.

     

    POST For all of the folks who thought this was a trick question its not. And yes If you use any language and you put it on your CV or Resume and get it wrong, you should be ashamed of your self. The correct answer to the test question is: Statment is not in legal C syntax. Sorry for being overly critical sometimes but this is not it. I have been working with C since sometime in the 80's, and have written and debugged some compilers. One early on was the DeSmet C compiler and I bit me in the ass when it wouldn't do this terminal[80][] = {{5,5}, ... That took a long weekend a Pauls place.

    FINS

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • phoenixcomm
    0 phoenixcomm over 6 years ago

    This is a great example of what NEVER to do and expect the correct answer.

     

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

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

    What is going on in C is that ++I, (pre-inc),  i++ (post-inc) and as well as +=, -=, *=, /=, and %= are called
    basic mathamatical operators, as well as =, +, -, *, /, %.

    With that being said lets get down to a C statement in BNF: (Backus-Naur Form)

    This grammar was adapted from Section A13 of The C programming language, 2nd edition, by Brian W. Kernighan and Dennis M. Ritchie,Prentice Hall, 1988.

    <unary-expression> ::= <postfix-expression> | ++ <unary-expression> | -- <unary-expression> |

    <unary-operator> <cast-expression> | sizeof <unary-expression> | sizeof <type-name>

     

    <postfix-expression> ::= <primary-expression> | <postfix-expression> [ <expression> ] |

    <postfix-expression> ( {<assignment-expression>}* ) | <postfix-expression> . <identifier> | <postfix-expression> -> <identifier> | <postfix-expression> ++ | <postfix-expression>

    <unary-operator> ::= & | * | + | - | ~ | !

    <expression> ::= <assignment-expression>  | <expression> , <assignment-expression>

    <assignment-expression> ::= <conditional-expression>  | <unary-expression> <assignment-operator> <assignment-expression>

    <assignment-operator> ::= = | * |  /=  | %=  | +=  | -=  | <<=  | >>=  | &=  | ^= | |=

     

    In Summary: Well you just can't do it. It is not valid code. In effect, you were making a statement that was like this

    Variable X = prefix-expression operator prefix-expression operator prefix-expression eol.

     

    POST For all of the folks who thought this was a trick question its not. And yes If you use any language and you put it on your CV or Resume and get it wrong, you should be ashamed of your self. The correct answer to the test question is: Statment is not in legal C syntax. Sorry for being overly critical sometimes but this is not it. I have been working with C since sometime in the 80's, and have written and debugged some compilers. One early on was the DeSmet C compiler and I bit me in the ass when it wouldn't do this terminal[80][] = {{5,5}, ... That took a long weekend a Pauls place.

    FINS

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
  • michaelkellett
    0 michaelkellett over 6 years ago in reply to phoenixcomm

    I wouldn't expect any one other than a C compiler expert to know if this was valid (by strict interpretation of the relevant standard) code or not.

     

    I would expect any, even slightly, competent C programmer to recognise it as the kind of code that should never be written.

     

    The question is not entirely daft, because the messing about with i could be concealed in more complex expression - my compiler (Keil) warns about such things if you do them inadvertently.

     

    MK

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • phoenixcomm
    0 phoenixcomm over 6 years ago in reply to michaelkellett

    No Michael, Any competent C programmer should know this one. Maybe its that you and current programmers put too much reliance on the IDE to tell you, your mistakes.  I am far from an expert.   Instead of making excuses please learn something from this bitter pill.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • michaelkellett
    0 michaelkellett over 6 years ago in reply to phoenixcomm

    Please, moderate your tone a bit - E14 is a nice friendly group, let's keep it that way.

     

    It's quite possible to have a discussion without recourse to accusing others of "making excuses" because they happen to disagree with you.

     

    To make it quite clear, I never said that I relied on the IDE to tell me about this (or any other issue), I pointed out that it is flagged as a warning, confirming that in Keil's opinion it is not a good idea but perhaps not, strictly speaking, an error.

     

    There are many lengthy discussions on this and closely related topics on the web:

     

    Here is just one:

     

    https://www.quora.com/What-does-an-expression-involving-multiple-post-pre-decrement-increment-operators-evaluate-to-in-C…

     

    MK

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • phoenixcomm
    0 phoenixcomm over 6 years ago in reply to michaelkellett

    Micheal, sorry about that I have been accused of going too far, and well I did this time.  But as a 'programmer' yes I went to school, but not for C (they liked PASCAL at the time), the object as an engineer is to solve problems, not to create them. This subject should never have come up in the first place, as 'operators' are normally taught in the first week or two. And even if you did not go to school pick up a book and read. yes I know that is going out of favor.

    I general have solved problems all my life and yes caused more than a few as I ruffed the feathers, as you see I can be quite blunt and sarcastic.

    But I did write a post about "operators". I don't know why this question keeps popping up. Your Compiler should just throw up errors as a malformed statement. I have seen it on a test once. And I was sucked into this black hole once in an interview where when I told him that the code was invalid they insisted it should work (I stopped the interview with a few choice words and left, a week or so they apologized to me and agreed yes it was malformed and offered me a much better job.) 

    About Quora and Stack Exchange. Sometimes when looking for an answer I will look on SE but the questions are "Voted Up/Down" and Mass does not mean right!  So please take it with a grain of salt. Oh and Q just ignore it.

    Now about operator in C and "overloading"  ie x =  i++ + i ++;  Please Please put it back in the black hole it which it lives. No matter what you or anybody else thinks according to K&R (the bible) it can't be done!

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • neuromodulator
    0 neuromodulator over 6 years ago in reply to phoenixcomm

    I gotta disagree with you. Knowing every detail of a language does not make you a better programmer. When solving problems, the focus is on algorithms, not obscure language details.

     

    Now the expression itself is alright in terms of grammar. Considering that you generate parsers out of the grammar it would make not sense that the compiler does not complain about it.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • phoenixcomm
    0 phoenixcomm over 6 years ago in reply to neuromodulator

    Please put this fable away. It is Not valid C K&R or ANSI grammar. If you think you can do it you are dead wrong. PLEASE LOOK AT THE GRAMMAR in the K&R book or what portions of which are relevant.

    1. Please Stop whipping this dead horse.

    2. If you really think you are right (snowball's chance in hell) Run the dam gramma with the grammar above. (still, can't be done!)

    3. When you want to say UNCLE and give up, In disgust just apologize for being very very silly. 

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • neuromodulator
    0 neuromodulator over 6 years ago in reply to phoenixcomm

    Here is my snowball chance in hell:

     

    From: http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1570.pdf

     

    x = ++i + ++i;

     

    expression-statement

    expression;

    assignment-expression;

    unary-expression assignment-operator assignment-expression;

    postfix-expression = conditional-expression;

    primary-expression = logical-OR-expression;

    identifier = logical-AND-expression;

    identifier = inclusive-OR-expression;

    identifier = exclusive-OR-expression;

    identifier = AND-expression;

    identifier = equality-expression;

    identifier = relational-expression;

    identifier = shift-expression;

    identifier = additive-expression;

    identifier = additive-expression + multiplicative-expression

    identifier = multiplicative-expression + multiplicative-expression;

    identifier = cast-expression + cast-expression;

    identifier = unary-expression + unary-expression;

    identifier = ++unary-expression + ++unary-expression;

    identifier = ++postfix-expression + ++postfix-expression;

    identifier = ++primary-expression + ++primary-expression;

    identifier = ++identifier + ++identifier;

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • michaelkellett
    0 michaelkellett over 6 years ago in reply to neuromodulator

    The version of the C standard you quote, page 76:

     

    84) This paragraph renders undefined statement expressions such as

    i = ++i + 1;

    a[i++] = i;

     

    You have to hop around the standard a bit to get the full story on this but the standard (to me ) suggests that you would expect a warning, not an error.

     

    But, as the behavior is undefined, it's not something you should ever do.

     

    You'll need a C compiler expert (not me) to explain the philosophy behind what gets to be an error and what is just undefined.

     

    But I don't see much of a problem here, the answer to the interview question is simple enough, it's undefined behavior (with a nice clear example in the standard) so you shouldn't do it.

     

     

    MK

    • Cancel
    • Vote Up +1 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