how does this code work ?

Page 1 of 1

14 Replies - 1533 Views - Last Post: 20 June 2011 - 12:39 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=236097&amp;s=5281212925204f8b8c2343f0b9e0128b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 sat4ever0606

Reputation: 1
• Posts: 16
• Joined: 06-January 09

how does this code work ?

Posted 17 June 2011 - 08:59 PM

```include<stdio.h>
int main()
{

int a=5;
int b=5;
printf("a=%d\na--=%d\n--a=%d\n",a,a--,--a);
printf("b=%d\nb++=%d\n++b=%d\n",b,b++,++B)/>;

}

```

o/p is:

a=3
a--=4
--a=3
b=7
b++=6
++b=7

how does the execution proceed ???
Is This A Good Question/Topic? 0

Replies To: how does this code work ?

#2 Martyr2

• Programming Theoretician

Reputation: 5227
• Posts: 14,010
• Joined: 18-April 07

Re: how does this code work ?

Posted 17 June 2011 - 09:07 PM

Well a-- and --a are actual short syntax for decrements so those are done first from left to right. a-- subtracts 1 from 5 to make it 4, then it goes to --a which makes it 3 and then all values are put into the printf so this means "a" is 3.

Same with the ++b and b++. Those are evaluated first from left to right. Then they are put into printf which is why b = 7

#3 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,233
• Joined: 14-September 07

Re: how does this code work ?

Posted 17 June 2011 - 09:08 PM

I's undefined behavior. See the side effects section.

#4 sat4ever0606

Reputation: 1
• Posts: 16
• Joined: 06-January 09

Re: how does this code work ?

Posted 17 June 2011 - 09:16 PM

Martyr2, on 17 June 2011 - 09:07 PM, said:

Well a-- and --a are actual short syntax for decrements so those are done first from left to right. a-- subtracts 1 from 5 to make it 4, then it goes to --a which makes it 3 and then all values are put into the printf so this means "a" is 3.

Same with the ++b and b++. Those are evaluated first from left to right. Then they are put into printf which is why b = 7

but a-- actually means print a and then decrement right ? if for example the statement was just printf("%d",a--); the output is only 5.. but y does here it becomes 4 ?

#5 sat4ever0606

Reputation: 1
• Posts: 16
• Joined: 06-January 09

Re: how does this code work ?

Posted 17 June 2011 - 09:53 PM

@martyr2, using your same logic if i have a statement like this
```int c =10;
printf("\n\nc--=%d\n--c=%d\nc=%d\n--c=%d\nc--=%d\nc=%d\n",c--,--c,c,--c,c--,c);

```

following your logic the output should be

c-- = 9
--c = 8
c = 8
--c = 7
c-- = 7
c = 6

but y its not evaluated that way ??
also wen the printf's are given separate like below(the previous one line printf being commented)

```printf("\n\nc--=%d",c--);
printf("\n--c=%d",--c);
printf("\nc=%d",c);
printf("\n--c=%d",--c);
printf("\nc--=%d",c--);
printf("\nc=%d",c);

```

why does the above two outputs differ ?? how does printf evaluate the expression ??

#6 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,233
• Joined: 14-September 07

Re: how does this code work ?

Posted 17 June 2011 - 10:03 PM

POPULAR

#7 Martyr2

• Programming Theoretician

Reputation: 5227
• Posts: 14,010
• Joined: 18-April 07

Re: how does this code work ?

Posted 17 June 2011 - 10:05 PM

Sorry my mistake here... they are encountered from left to right but most compilers then evaluate right to left because of a stack implementation they use for evaluating. While the language itself doesn't specify an order of evaluation, the compilers often implement their own. To answer your questions here, you are not locking values in place. The side effects that KYA talked about is effecting values.

Lets see if you can follow me here.... starting at the far right "c" is 10. Next one to the left "c--" takes 10 uses that and decrements it. This means that the last argument is no longer 10, it is now 9. Now the third to the last "--c" takes the 9, decrements it to 8 and uses that. Which means the last value is no longer 9, it is 8. Next to the left is "c" which of course is 8 and next to the left is "--c" which takes 8 and decrements it to 7. This means that the last argument is now 7 but it also means that "c" is also 7 and the "--c" is now also 7. Next to the left is "c--" which uses the 7 and then decrements to 6 which effects the "--c" which now prints 6, the "c" is now 6 and the last argument "c" is also 6.

What I want you to take from this is that these statements are not in isolation. They are effecting one another. It is because they are all modifying "c"

Edit: And before anyone wants to start something, I am in full support of what KYA is saying here. I am just saying that compilers do implement their own evaluation and what you are seeing is increment/decrement having side effects that are effecting one another.

This post has been edited by Martyr2: 17 June 2011 - 10:08 PM

#8 jimblumberg

Reputation: 5488
• Posts: 17,064
• Joined: 25-December 09

Re: how does this code work ?

Posted 17 June 2011 - 10:08 PM

The following code fragment produces undefined behavior.
```printf("\n\nc--=%d\n--c=%d\nc=%d\n--c=%d\nc--=%d\nc=%d\n",c--,--c,c,--c,c--,c);
```

Jim

#9 NickDMax

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

Re: how does this code work ?

Posted 17 June 2011 - 11:09 PM

Quote

Edit: And before anyone wants to start something, I am in full support of what KYA is saying here. I am just saying that compilers do implement their own evaluation and what you are seeing is increment/decrement having side effects that are effecting one another.

The problem with the multiple side-effects per-line and undefined sequence of execution is that compilers not are even trying for any kind of logic to "how" they evaluate it. That is to say they are not even consistent within the same compiler. For example compiling with optimizations often produces one set of results and without produces another.

Sometimes its even stranger with 1 little symbol changing the way an expression was evaluated. Basically in this particular case (using multiple ++ and -- operators in the same statement) really is very undefined and there just is no way to consistently "guess" what output a given compiler will generate for a given expression. Just changing the code around the expression may change the results.

Just when you think you understand the logic of what your compiler did, something will change and your compiler will evaluate it differently and you have to start all over.

Here are three different compilers and three different evaluations:
```VC++2010 -O2
> "C:\CProjects\Forum Help\236097-how-does-this-code-work.exe "
a=4
a--=4
--a=4
b=6
b++=6
++b=6

MinGW (not sure settings)
> "C:\CProjects\Forum Help\236097-how-does-this-code-work.exe "
a=3
a--=4
--a=3
b=7
b++=6
++b=7

Borland 5.5 -O
> "C:\CProjects\Forum Help\236097-how-does-this-code-work.exe "
a=3
a--=4
--a=4
b=7
b++=6
++b=6
```

If you want to know how your compiler evaluated it, compile to assembly language and look, but don't expect anything you find to be consistent with other such expressions.

#10 sat4ever0606

Reputation: 1
• Posts: 16
• Joined: 06-January 09

Re: how does this code work ?

Posted 18 June 2011 - 08:56 AM

so moral of the story "we cant predict arithmetic expressions output" ?

#11 Oler1s

• D.I.C Lover

Reputation: 1397
• Posts: 3,884
• Joined: 04-June 09

Re: how does this code work ?

Posted 18 June 2011 - 09:35 AM

> so moral of the story "we cant predict arithmetic expressions output" ?

Which story? Can't be this thread, given all the information specifically on postfix increment and decrement operators. As pointed out, the issue is that i++ says that i is incremented afterwards, but the exact time of afterwards is not defined precisely enough. That's the problem.

#12 NickDMax

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

Re: how does this code work ?

Posted 18 June 2011 - 09:03 PM

sat4ever0606, on 18 June 2011 - 11:56 AM, said:

so moral of the story "we cant predict arithmetic expressions output" ?

You CAN predict arithmetic expressions so long as their operations are defined within C/C++.

The moral of the story is: "We can't predict, with certainty, undefined behavior."

Basically we are saying, if you break the laws of physics then we can't predict what will happen. If you break the laws of C\C++ then we can't predict what will happen.

Expressions like the one shown are "UNDEFINED" meaning that the compiler can do anything it wants. The compiler could print out "Rabbits Rabbits Rabbits" and it would still be a perfectly valid compiler because when it comes to undefined behavior the compiler can do anything it wants.

If on the other hand, you write a proper program with defined behavior and the program does not function as defined, then the compiler is broken.

#13 sat4ever0606

Reputation: 1
• Posts: 16
• Joined: 06-January 09

Re: how does this code work ?

Posted 19 June 2011 - 09:21 AM

then does that mean whenever i am working with prefix and postfix inc/dec operators i am always in the danger of getting into undefined behaviour and that even simple expressions like a = i++ * i++ would give a result which can be anything because the result is compiler dependent ?

• Saucy!

Reputation: 6246
• Posts: 24,014
• Joined: 23-August 08

Re: how does this code work ?

Posted 19 June 2011 - 10:23 AM

It means, if your education system in India is teaching you this crap -- and they are, we see it ALL THE TIME -- and they're not teaching you that you should NOT be doing this, they are FAILING you on so many levels. This code is utter nonsense, and nothing you will ever see in any real, live production code...unless it was written by "freshers" who were taught that this is valid code.

Which sadly means this code probably IS out in the wild, in some Fortune 500 company's offshore-developed product.

#15 NickDMax

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

Re: how does this code work ?

Posted 20 June 2011 - 12:39 AM

sat4ever0606, on 19 June 2011 - 12:21 PM, said:

then does that mean whenever i am working with prefix and postfix inc/dec operators i am always in the danger of getting into undefined behaviour and that even simple expressions like a = i++ * i++ would give a result which can be anything because the result is compiler dependent ?

Yes. Anytime you go and use the prefix and postfix expressions in an invalid - undefined - manner you can expect the unexpected to happen.

This *simple* expression is undefined:

a = i++ * i++;

Why? Because you used ++ twice on the SAME variable. However if you use the prefix and postfix inc/dec operators as designed with simple expressions such as:

a = i++ * j++;

then the operation IS defined, and there is a set of rules that all compilers will follow to produce consistent results.
So you don't have to avoid using the pre/post increment/decrement, you have to avoid breaking the rules.

Better yet. Avoid ambiguous code all together. Your code should not require detailed knowledge of C/C++ compiler design to parse out its meaning. Programs should be made so that humans can read and understand them without having to pull out a chart of operator precedence and Ouija board to understand.