Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Why is i + ++i 12 when i is 5?

coalzpp

New Coder
I was tried to add one more of i and i but the result was 12. The language I tried is c. Shouldn't it be 11?

C:
#include <stdio.h>

int main() {

int i=5;

printf("%d", ++i + i);

}

i already tried the same thing in c++ and same result. How can the sum of a number plus one more of that number be equal to two more than twice that number?
 
Last edited by a moderator:
Your assumption is that i remains 5 and that ++i is the same as i+1. But that is not so !
C:
i++
is a unary operator which increases i by one, then evaluates to the new value. So the first term evaluates to 6, and the second term as well, because i is now 6. Your code is equivalent to
C:
i = i+1;
printf("%d", i + i);
I hope that clarifies. If not, you'll need to study C operators in more detail.
This would work the same way in C, C++, C# Java, and Javascript.
 
Your assumption is that i remains 5 and that ++i is the same as i+1. But that is not so !
C:
i++
is a unary operator which increases i by one, then evaluates to the new value. So the first term evaluates to 6, and the second term as well, because i is now 6. Your code is equivalent to
C:
i = i+1;
printf("%d", i + i);
I hope that clarifies. If not, you'll need to study C operators in more detail.
This would work the same way in C, C++, C# Java, and Javascript.
but when I replace ++i + i with i + ++i, it becomes 12. shouldn't it be 11?
 
but when I replace ++i + i with i + ++i, it becomes 12. shouldn't it be 11?
I can see why you think so. But no, the ++ has higher precedence than the +, and is evaluated first. Remember that not everything is executed left to right ! You always need to be conscious of the order or operations. Whenever in doubt, use parentheses (although in this case it would not help). And personally I would never use + and ++ in the same expression. At least not in this manner. It's confusing and kind of defeats the purpose.
 
Last edited by a moderator:
according increment operator, we need to follow following rules (depends on compiler)
according to turbo c++
rule 1) check how many preincrements are available and assign the last incremented value through out the expression
rule 2) go for assignment operator
rule 3) check any post increments , if yes go for it
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom