Thread: understanding the book

  1. #1
    back? dbaryl's Avatar
    Join Date
    Oct 2001
    Posts
    597

    understanding the book

    I was just reading a C book, and came across this:
    Do not use increment, decrement, or compound assignment operators as subexpressions in complex expressions.
    I'm having a hard time understanding the wording... would it be something like this?
    Code:
    //Don't do this:
    a = b + c++
    This is my signature. Remind me to change it.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yeah, that's pretty much what they're saying. You don't want to use ++ / -- operators in complex expressions generally because you may confuse yourself on how soon they're incremented.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    back? dbaryl's Avatar
    Join Date
    Oct 2001
    Posts
    597
    Thanks, that was a quick response
    This is my signature. Remind me to change it.

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    And also not every compiler treats ++ and -- the same. So in a calculation like:

    a = i+++j;

    The result may not always be the same.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >because you may confuse yourself on how soon they're incremented.
    Or you could inadvertently invoke undefined behavior by modifying a variable more than once between sequence points. This is generally a hard one to spot.

    -Prelude
    My best code is written with the delete key.

  6. #6
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Do not use increment, decrement, or compound assignment operators as subexpressions i

    From what i too have been studying is that eg; static const int k = 3;
    Yes you can initialize k but you can not assign nor ++ or -- it. Even though a variable has been qualified with const, it still cannoit be used to specify an array size in another declaration.
    Just a little FYI ...
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: Do not use increment, decrement, or compound assignment operators as subexpressio

    Originally posted by correlcj
    From what i too have been studying is that eg; static const int k = 3;
    Yes you can initialize k but you can not assign nor ++ or -- it.
    Of course you can't. You've declared it const. This goes without saying. You cannot modify a constant variable.

    As for your second part, I believe with the latest version of the standard you can. Then again, I've never bothered to read the standard, so I may be off there.

    Also, it depends on your compiler. For example, with gcc, you can do the following:

    Code:
    #include <stdio.h>
    int main ( void )
    {
        int x = 5;
        {
            char array[x]={0};
            int y;
            for( y = 0; y < x-1; y++ )
                array[y]='a'+y;
            printf("%s",array);
        }
        return 0;
    }
    And it will compile and execute.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. URGENT: Help wanted...in C
    By iamjimjohn in forum C Programming
    Replies: 16
    Last Post: 05-18-2007, 05:46 AM
  2. Looking for a c++ book, didn't were to post this...
    By Rune Hunter in forum C++ Programming
    Replies: 6
    Last Post: 09-24-2004, 06:32 PM
  3. Replies: 5
    Last Post: 09-19-2004, 06:52 AM
  4. Must read book!
    By RealityFusion in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 07-15-2004, 09:05 PM
  5. Should i get a new reference book?
    By Raison in forum Windows Programming
    Replies: 2
    Last Post: 06-15-2004, 10:43 PM