Like Tree11Likes
  • 2 Post By std10093
  • 2 Post By Matticus
  • 1 Post By AndiPersti
  • 1 Post By Adak
  • 3 Post By Salem
  • 2 Post By laserlight

Incrementation Questions

This is a discussion on Incrementation Questions within the C Programming forums, part of the General Programming Boards category; First, I know this is bad programming and most people say to not use it, but my teacher uses this ...

  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,675
    UNDEFINED BEHAVIOR. Google it.
    rags_to_riches and Salem like this.
    Code - functions and small libraries I use


    Its 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
    2,282
    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
    Zachary May and Salem like this.

  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
    Salem likes this.

  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.
    Zachary May likes this.

  8. #8
    Registered User
    Join Date
    Feb 2013
    Posts
    18
    Thank you for your help

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    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.
    AndiPersti and Matticus like this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    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%.

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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21