Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C++
#include<stdio.h>
void main()
{
    int b,k=10;
    b=(k++-k++-k++,k++);
    printf("%d",b);
}


What I have tried:

I have tried the operator precedence rules and associativity rules
Posted
Updated 8-Feb-16 12:01pm
v5
Comments
E.F. Nijboer 8-Feb-16 10:45am    
by asking the right question... The title of your question says nothing about your actual question. Also, just run the code to get the answer. Looks like a school assignment so you should just do the work needed and learn something.
CHill60 8-Feb-16 10:46am    
What is the problem?
Sergey Alexandrovich Kryukov 8-Feb-16 11:02am    
Why having a section "what I have tried" without telling us what have you tried? Or, in this case, you told it above, not in this section.

We got a number "questions" with the same structure and attitude, and I wonder: is it the same person under different accounts? The common characteristics are: no effort, having not clue on what to do, something creating a lot of doubt if you should do this work at all...

You did not explain what's the problem and what you are trying to achieve, so what's the point of saying what you have tried?

—SA

Basically, don't do that.
The problem (especially with older languages such as C) is that the exact order in which "things happen" is not defined by the language.
As a result, the compiler write is at liberty to implement pre- and post- increment and decrement operations as they find convenient - which doesn't necessarily mean "immediately" - it is quite possible that they are done before, during or after the execution of the line.
When you add in that the compiler is at liberty to evaluate it's expression in either left-to-right or right-to-left order (unless forced to by precedence rules) and what value you get is not going to be defined, or even consistent between two compilers, or even two versions of the same compiler, or worse: different depending on optimization!

And it's a PITA to read, which means it's difficult to understand and maintain: separate it into different lines, and let the optimizer sort it out!
 
Share this answer
 
Comments
Patrice T 8-Feb-16 11:55am    
+5
Albert Holguin 9-Feb-16 13:50pm    
+5, That ladies and gentlemen, is what we'd call horrible code in the business... don't be that guy. Just because you can do something doesn't mean you should.
Quote:
How do i get the answer ?
Simply Run the program.

As spotted in Solution 1, you are in a gray zone where the language behavior is undefined. said otherwise, if you compile the program on 2 different compilers, you may get 2 different result and both will be exact from the language point of view.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900