Post Go back to editing

# KCC's quizzes AQQ257 about a C programming puzzle

And here is a new challenge for the week:

1. Quote of the week: "Well done is better than well said." - Benjamin Franklin.

2. New quiz of the week: AQQ257 about a C programming puzzle (proposal from our colleague Mirza Kolakovic:

Questions:

1. What is the result of : a+++++b ; statement in C ?
2. What is the value of operands after the execution ?

Good luck!

• a+++++b can be written as
(a++)+(++b), value of a is used and then incremented by 1, value of b is incremented by 1 and then used.

at the time of execution, c= a+(1+b)
after execution, a= a+1 and b= b+1

• a++ + ++b will become a+(b+1)

After operation the operand will be a= a+1 and b= b+1

I am taking random integer a and b and assigning some values

Assume a = 2, b = 3 then the expression will go like this a++ + ++b

And a++ is postfix operation and ++b is prefix operation so the value will go like this a= 2 and b = 4 so answer will be 6.

And after this operation a++ will execute and a will become 3 and b will become 4.

• The statement a+++++b is effectively a++ (a post increment) + (add) ++b (b pre increment)

So the result will be a + (b+1)

After the execution of the operation, both operands will have been incremented, i.e. a = a+1 and b = b+1

• 1. The result of     (a++)+(++b)    which is   (a+b)+1

2.  a and b would been increased by one.

i++   icreases i by one first after having used it,

4 + i++  results in:

first  makes  4 + i

next icreases i by 1.

While  4 + ++i  in:

first  increases i  by 1

next makes  4 + i

• Since I'm not fluent in C++, I solicited the help of my son, who is a sophomore CS major, and this was his reply... (feel free to not include me in any solutions since I didn't come up with it.  However, I played a part in making him, so I feel that I contributed to the solution at least in some way. )

My guess: assuming a and b are integers…

a+++++b can be rewritten as (a++) + (++b)

Which means the full expression returns the value a + (b + 1)

Reason is because doing ++b increments b first and returns b plus one, while doing a++ increments a but returns the original value (a)

The value of the operands afterward are both just incremented by one

Is this accurate?

Brian

• Correct  , Congratulations!

I give you hereunder the more detailed answer from our colleague Mirza Kolakovic who had proposed this quiz:

In C, the expression a++ + ++b means increment b, add to a, then increment a. If a and b equal 1 then the expression evaluates to 3 and both variables end up with the value 2. In most other languages it would generate an error message, and quite right too.

• Thank you  ; you are right! I also give you here under the detailed answer from our colleague Mirza Kolakovic who had proposed this quiz:

In C, the expression a++ + ++b means increment b, add to a, then increment a. If a and b equal 1 then the expression evaluates to 3 and both variables end up with the value 2. In most other languages it would generate an error message, and quite right too.

• Bingo Martin! I give you also the detailed answer from our colleague Mirza Kolakovic who had proposed this quiz (I am quite weak in software...):

In C, the expression a++ + ++b means increment b, add to a, then increment a. If a and b equal 1 then the expression evaluates to 3 and both variables end up with the value 2. In most other languages it would generate an error message, and quite right too.

• Correct  ! Cong

In C, the expression a++ + ++b means increment b, add to a, then increment a. If a and b equal 1 then the expression evaluates to 3 and both variables end up with the value 2. In most other languages it would generate an error message, and quite right too.

ratulations. I give you also hereunder the detailed answer from our colleague Mirza Kolakovic who had proposed this quiz:

• Your answer is correct and fully accurate  ! Congratulations to you and to your colleague too! I also give hereunder the detailed answer from Mirza Kolakocic who had proposed this quiz:

In C, the expression a++ + ++b means increment b, add to a, then increment a. If a and b equal 1 then the expression evaluates to 3 and both variables end up with the value 2. In most other languages it would generate an error message, and quite right too.

By the way, I am like you; I am quite light in software...