Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

++post incrementation function in C

Status
Not open for further replies.

E-design

Advanced Member level 5
Advanced Member level 5
Joined
Jun 1, 2002
Messages
2,014
Helped
507
Reputation
1,016
Reaction score
408
Trophy points
1,363
Activity points
14,193
does it really matter if i use x++ or x=x+1?

I am not a C person and need some help understanding this.

Say we have two int declared: x=20 and y=35

x = x++ + y++ results in x = 56

Can anyone explain which one (my guess is y) got incremented and why the answer is not 57?

Does it matter if it was written as x = y++ + x++ or not
 

function in c

the flow goes this way:--

you are having post increment operator, so first the operation will be evaluated and then the incremented operator will perform the action

x = x++ + y++ will go in this way

x = x + y and after this perform x++ and y++
so the result will be
x = 20 + 35 and after this x++ and y++

so finally you will get x = 55 with x++ and y++

FINAL RESULT 55++ and 35++
i.e. x = 56 and y = 36

Hope this helps
 

    E-design

    Points: 2
    Helpful Answer Positive Rating
function of c

So what you are saying is that you first look at the experession (x + y) and ingnoring the ++

Then apply the ++ after the result of (x + y)

So x = x+y = 55
then increment x, which changes x = (x+1) = 56
then y is incremented but not added in the same expression?

How will you apply it in the following statement:

x = ++x + ++y?

Thanks for your help so far!
 

Re: ++ function in C

Hi E-design

x = ++x + ++y?

The same will be the result.

Please check the C tutorial on operators. First the addition and substraction from left to right is executed and then the multiplication and division will be executed and the rest next

Nandhu
 

Re: ++ function in C

Nandhu says

nandhu015 said:
The same will be the result.

But this is wrong
you check the tutorial for the pre and post increment operator

Here E-design is using pre increment operator
So the result will go in the following way

first both the variables will be incremented
++x and ++y
x=(20+1) and y=(35+1)

x=21 and y=36
then the addition will takes place
x=21+36
x=57

FINAL RESULT x=57 and y=36
 

Re: ++ function in C

Putting the ++ BEFORE the variable means increment it then use it.

Putting the ++ AFTER the variable means use it then increment it.

In your first post, I think the L to R is evaluating as:

X = X++
meaning X becomes X and then it is incremented so X is now 21

+ Y++
meaning the value of Y is added to X (added before the increment) 21 + 35 = 56.

Then Y is incremented as well but the addition has already taken place.

Brian.
 

++ function in C

Everyone is wrong!

x = x++

is undefined in C and change depending on compiler to compiler.
 

Re: ++ function in C

I know from reading a few books and papers that you should avoid using the same in/decremental operation more than once in the same statement, otherwise you get side effects which varies from compiler to compiler.

http://msdn.microsoft.com/en-us/library/8a425116(VS.80).aspx
 

Re: ++ function in C

Is this a print error? Should it be "a" instead of "b"?
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top