Constants

This is a discussion on Constants within the C++ Programming forums, part of the General Programming Boards category; 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
    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
    University of Waterloo
    Posts
    1,903

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    you should use const int or const double in C++
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,728
    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);
    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

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

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, 08:18 PM
  2. Replies: 7
    Last Post: 11-12-2008, 10: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, 07:29 PM
  5. winsock message constants
    By the Wookie in forum Windows Programming
    Replies: 10
    Last Post: 11-08-2002, 02:30 PM

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