# Thread: Incrementation Questions

1. ## Incrementation Questions

First, I know this is bad programming and most people say to not use it, but my teacher uses this stuff on his test a bunch.
Code:
```#include <stdio.h>
#define max 52
main(){
int a=10,b,c,d,e,f,g;
printf("%d %d %d\n",a,a++,++a);
a=1;
b = ++a + ++a + ++a;
a=1;
c = ++a + ++a + --a;
a=1;
d = --a + ++a + ++a;
a=1;
e = ++a + --a + ++a;
f=max+max;
g=++max;
printf("%d\n",b);
printf("%d\n",c);
printf("%d\n",d);
printf("%d\n",e);
printf("%d\n",f);
printf("%d\n",g);
getchar();

/* This prints
12 11 11
10
8
4
4
104          */
}```
Can someone explain step by step why b,c,d,e are what they are? If you can break it down to 1+1=2 (step by step at the lowest level) i would love it because I am lost when it comes to this.

I don't really understand incrementations added together.

And is my understanding of printf("%d %d %d\n",a,a++,++a);
correct. So with this since incrementations are right priority (so like reading right to left), the printf does ++a first and saves it as 11 then does a++ which saves it as 11 then increments by 1 then saves a which is 12 all in temp memory and then displays them?

And why can't you increment max when it is defined as 52?

Thank you for all your help
Zach

2. UNDEFINED BEHAVIOR. Google it.

3. Originally Posted by std10093
UNDEFINED BEHAVIOR. Google it.
Okay. All I can say is that we use dev c++
My teacher tests on this, so what am i suppose to do

4. Since you're forced to work out code that can result in undefined behavior, maybe you'll get some good ideas from an older post:
http://cboard.cprogramming.com/c-pro...erators-c.html

5. Tell him/her that this program doesn't compile with a compiler which isn't crap:
Code:
```\$ make foo
cc -Wall -Wextra -ggdb3    foo.c   -o foo
foo.c:3:1: warning: return type defaults to ‘int’ [-Wreturn-type]
foo.c: In function ‘main’:
foo.c:5:31: warning: operation on ‘a’ may be undefined [-Wsequence-point]
foo.c:5:31: warning: operation on ‘a’ may be undefined [-Wsequence-point]
foo.c:7:15: warning: operation on ‘a’ may be undefined [-Wsequence-point]
foo.c:7:15: warning: operation on ‘a’ may be undefined [-Wsequence-point]
foo.c:9:15: warning: operation on ‘a’ may be undefined [-Wsequence-point]
foo.c:9:15: warning: operation on ‘a’ may be undefined [-Wsequence-point]
foo.c:11:15: warning: operation on ‘a’ may be undefined [-Wsequence-point]
foo.c:11:15: warning: operation on ‘a’ may be undefined [-Wsequence-point]
foo.c:13:15: warning: operation on ‘a’ may be undefined [-Wsequence-point]
foo.c:13:15: warning: operation on ‘a’ may be undefined [-Wsequence-point]
foo.c:15:7: error: lvalue required as increment operand
foo.c:23:1: warning: control reaches end of non-void function [-Wreturn-type]
make: *** [foo] Error 1
\$ clang foo.c
foo.c:3:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
main(){
^~~~
foo.c:15:7: error: expression is not assignable
g=++max;
^ ~~~
1 warning and 1 error generated.```
Bye, Andreas

6. If you take out g=max++ it does compile.

That's why I asked why C won't let you do this.

I defined max as 52 but you can't increment that and I wanted to know why

7. Originally Posted by Zachary May
If you take out g=max++ it does compile.

That's why I asked why C won't let you do this.

I defined max as 52 but you can't increment that and I wanted to know why
The idea of a #define is that you can easily see and change a constant value, used throughout the program.

If the program could CHANGE the #define, into another value, that would make debugging and their status as a constant, much more difficult and confusing.

In the max+max - that's fine. You aren't trying to change the define at all.

But ++max IS trying to change a define, and defines are constant values. I'm not saying the #define value (or any constant value) can't be changed by surreptitious means - it is a machine after all, and we built it, so we can quite possibly "crack" it if we try hard enough.

But we shouldn't be allowed to do it easily.

8. Thank you for your help

9. > My teacher tests on this, so what am i suppose to do
Send your teacher here for re-education.

10. Originally Posted by Zachary May
First, I know this is bad programming and most people say to not use it, but my teacher uses this stuff on his test a bunch.
Originally Posted by Zachary May
All I can say is that we use dev c++
Your copy of Dev-C++ probably uses the MinGW port of gcc. Set the warning level of the compiler to the maximum, then compile. You will probably get a warning about the operation being undefined. Ask your teacher to explain this warning.

If your teacher cannot explain the warning or otherwise brushes it off, refer him to this C FAQ: expressions. Afterwards, you can then write down "undefined behaviour" as the correct answer to these questions. If your teacher insists on marking you as incorrect, you should take this up to higher authority to have the teacher removed from teaching this subject. Presumably, if your teacher is any good, he would do some reading up way before that happens and inform the class about his/her mistake.

11. Originally Posted by Adak
But ++max IS trying to change a define, and defines are constant values.
#define's are not constant values. They are directions to the preprocessor to perform substitution of text in source code.

So
Code:
```#define max 52
int main()
{
++max;
}```
is effectively seen by the compiler - after completion of preprocessing - as
Code:
```int main()
{
++52;
}```
which (by attempting to increment a literal value, which is not permitted) causes a compilation error.

As to the original question, any change of a variable twice in one statement causes undefined behaviour. Anything is allowed to happen, including crashing your hard drive or producing the output that your teacher expects.

All the teacher is doing is assuming that the various increment operators are performed in some order. To give the explanation s/he seeks, simply find an order of operations that might explain the particular output.

However, your teacher is getting you to learn rubbish. Apart from the (hopefully remote) possibility of crashing your hard drive, the behaviour from that code is not predictable. The behaviour can change between versions of a compiler, change when you use another compiler, and all sorts of things.

EDIT: As to whether you tell the teacher that, it depends. Some (poor) teachers do not take kindly to being legitimately corrected. Some accept that in good grace. If your teacher is one who does not take kindly to being corrected, you might be better off saying nothing, and trying to learn C properly once you get past this class.

Popular pages Recent additions