Click here to Skip to main content
15,879,613 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
First printf output is 11, 11. Second one is 10, 20 why is it 20 shouldn't it be 21?
C++
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a=10,b=++a;
    printf("%d,%d\n",a,b);
    b=a+--a;
    printf("%d,%d",a,b);
}


What I have tried:

I really don't understand how it works.
Posted
Updated 23-Apr-19 20:36pm
v2

This has to do with sequence points. I believe that both 20 and 21 are valid answers, since the compiler is free to choose when to evaluate --a, so it can either decrement a before or after the addition. The only thing you know for sure is that following the assignment to b, a will have the value 20.
You should avoid expressions that access and use the increment/decrement operators on the same variable at the same time in different locations. This includes passing the variable to functions. For example
C
int a=10;
printf("%d, %d\n", a, --a)
might print "10, 9", or it might print "9, 9"
 
Share this answer
 
v2
Comments
Member 14325983 23-Apr-19 15:58pm    
This is a school assignment that was giving me a headache, thank you!
It'll do the "prefix" operator (--) before anything else.

when a is 11, then

b=a+(--a)
= 10 + 10

a is updated "inline"; there is no intermediate storage.
 
Share this answer
 
Comments
k5054 23-Apr-19 16:15pm    
This is not correct. The C standard does not specify the order in which operands are evaluated. Given the following program:
#include <stdio.h>
int main()
{
    int a = 10;
    int b = a + --a;
    printf("%d, %d\n", a, b);
    return 0;
}

Compiling with gcc I get the result 9, 18;, but when I compile with clang I get 9, 19. Additionally, clang tells me: foo.c:6:17: warning: unsequenced modification and access to 'a' [-Wunsequenced], and using -Wall gcc tells me foo.c:6:17: warning: operation on ‘a’ may be undefined [-Wsequence-point]
[no name] 23-Apr-19 16:37pm    
I'm addressing his situation; which in this case, appears to be deterministic.

All you've pointed out is that there are "differences in compilers"; which boils down to, "know your tools".

The "standard" stresses "sequencing"; nothing else.
k5054 23-Apr-19 16:58pm    
Its not deterministic, though, is it? The same compiler could evaluate the operands in different order at different optimization levels. Worse, if you're hoping to create portable code, which might be compiled on other systems, perhaps with compilers you don't have access to, not knowing that the order of evaluation is up to the compiler, is going to cause bugs.
[no name] 23-Apr-19 17:51pm    
It is definitely deterministic if I use brackets; and that's all we're dealing with: sequencing, whether by default or forced.
k5054 23-Apr-19 18:02pm    
Still wrong, even b = a + (--a)
evaluates differently in gcc and clang. I think a true language lawyer would point out that this is undefined behavior, and so the implementation is free to do what it wants. Making hairy demons fly out of your nose, or launching nuclear missiles at Mount Doom in Mordor are both possible options. Neither are likely, but not prohibited by the standard.
Quote:
I really don't understand how it works.

This is a gray zone.
A C compiler is free to rewrite your code the way it sees fit inside a line of code.
So different compiler will get different result and all are correct, it is unpredictable, and most of the time, unexpected.
The rule of safety is to never use more than once a variable that get changed in a line of code.
 
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