Thread: Incrementation Questions

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    18

    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
    Last edited by Zachary May; 03-21-2013 at 04:15 PM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    UNDEFINED BEHAVIOR. Google it.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    18
    Quote Originally Posted by std10093 View Post
    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. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    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:
    increment and decrement operators in c

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    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. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    18
    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. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Zachary May View Post
    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. #8
    Registered User
    Join Date
    Feb 2013
    Posts
    18
    Thank you for your help

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > My teacher tests on this, so what am i suppose to do
    Send your teacher here for re-education.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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.
    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Adak View Post
    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.
    Last edited by grumpy; 03-22-2013 at 01:36 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Incrementation in conditionals
    By Niels_M in forum C Programming
    Replies: 2
    Last Post: 07-20-2010, 11:19 AM
  2. Pointer incrementation
    By newbie30 in forum C Programming
    Replies: 6
    Last Post: 08-14-2009, 09:51 PM
  3. HEX-numbers after incrementation x in for-loop.
    By Jelte in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2009, 10:36 AM
  4. string::size_type incrementation
    By cunnus88 in forum C++ Programming
    Replies: 2
    Last Post: 05-01-2007, 10:59 PM

Tags for this Thread