Thread: Constants

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    65

    Constants

    i have defined constants in a header file

    //Define constants

    #define Grids 70
    #define Hori_L 1.0
    #define delta_x Hori_L*pow((Grids - 1), -1) //m

    However when i call them in my .cc files I need to place a bracket around them (delta_x). Why is that?

    Cheers

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    3
    What happens when you use constants without brackets? What error messages do you get?

    If only the third one is the problem:

    Code:
    #define delta_x Hori_L*pow((Grids - 1), -1) //m
    Then you need to remove the comment from it because it may cause problems like this:

    Code:
    #define Grids 70
    #define Hori_L 1.0
    #define delta_x Hori_L*pow((Grids - 1), -1) //m
    
    int main(int argc, char **argv) {
        printf("%d\n", delta_x);
        return 0;
    }
    will become this after preprocessing:

    Code:
    #define Grids 70
    #define Hori_L 1.0
    #define delta_x Hori_L*pow((Grids - 1), -1) //m
    
    int main(int argc, char **argv) {
        printf("%d\n", Hori_L*pow((Grids - 1), -1) //m);
        /*notice that the end of the statement gets cut off*/
        return 0;
    }
    and you should have brackets around that anyway, so that the order of operations doesn't get messed up


    it should look like this:
    Code:
    #define delta_x (Hori_L*pow((Grids - 1), -1))

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    you could add C-style comments and be ok.
    Code:
    #define delta_x (Hori_L*pow((Grids - 1), -1)) /*m*/

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you should use const int or const double in C++
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For one thing, I note that x to the power of -1 is actually 1/x. So the expression:
    Code:
    Hori_L*pow((Grids - 1), -1)
    can be simplified to:
    Code:
    Hori_L / (Grids - 1)
    Then as vart suggested, you should prefer const to #define here, so it would be:
    Code:
    const int Grids = 70;
    const double Hori_L = 1.0;
    const double delta_x = Hori_L / (Grids - 1);
    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

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > However when i call them in my .cc files I need to place a bracket around them (delta_x). Why is that?
    Because #define is just a dumb search/replace operation.

    Eg.
    #define ADD(x,y) x + y

    Then you do
    x = ADD(1,2) * ADD(3,4);
    What the compiler sees is
    x = 1 + 2 * 3 + 4;
    At which point, the precedence is all screwed and you end up with something completely different to what you intended.

    Anything more complicated that a simple constant should have () around the whole thing to prevent any kind of precedence mishap later on.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with variables and constants.
    By dave1339 in forum C Programming
    Replies: 2
    Last Post: 12-21-2008, 09:18 PM
  2. Replies: 7
    Last Post: 11-12-2008, 11:00 AM
  3. COnstants
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 09-01-2007, 04:59 AM
  4. Help with Constants and enum constants. Newbie learning C++.
    By UnregJDiPerla in forum C++ Programming
    Replies: 5
    Last Post: 01-07-2003, 08:29 PM
  5. winsock message constants
    By the Wookie in forum Windows Programming
    Replies: 10
    Last Post: 11-08-2002, 03:30 PM