Thread: define constants

  1. #1
    Registered User
    Join Date
    Mar 2007

    define constants

    What is the best way to define constants in a C++ header file? Currently I am using the following format.


    //Define constants

    #define Grids 70
    #define Hori_L 10.0
    #define delta_x Hori_L/(Grids - 1)

    #define P0 101325 //N/(m^2)
    #define T0 300 //kg/(m^3)

    #define P1 0.9395*P0
    #define T1 0.9823*T0 //K
    #define rho1 P1/(R*T1) //kg/(m^
    #define rho0 rho1/(0.9564)

    This some times gives me strange errors as i have to put (delta_x) around them when in use

    Q_predicted = Q - delta_t/(delta_x)*delta_X(F, 1) + delta_t*HH;


  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    So why not put the ()'s inside the defined value?

  3. #3

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    OK, so I change my answer to, "So why not actually read what you've already been told?" amongst a few insults about being lazy.....

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Toronto, ON
    Why use #define when a const double would do just fine?

  6. #6
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Also, if you want const unsigned int types, it's permissible to do
    enum { P0 = 101235, T0 = 300 };
    But if you're going to have floating points in the mix as well, then just use doubles for the whole ordeal. It'll be nice and clean.
    Last edited by CodeMonkey; 09-05-2007 at 10:50 PM. Reason: semicolon
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Also, if you want const unsigned int types, it's permissible to do
    I'm pretty sure that enum values can be ordinary ints, so don't depend on unsigned-ness. Actually, on all compilers I can think of at the moment enums are signed values.

    There are lots of threads about how to define constants, for example:
    Last edited by dwks; 09-07-2007 at 03:21 PM.

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Would someone solve my problem?
    By Lonners in forum C Programming
    Replies: 9
    Last Post: 01-19-2008, 06:58 PM
  2. C Programming 2d Array Question
    By jeev2005 in forum C Programming
    Replies: 3
    Last Post: 04-26-2006, 03:18 PM
  3. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 10:56 AM
  4. whats wrong here
    By sreetvert83 in forum C++ Programming
    Replies: 15
    Last Post: 09-21-2005, 10:05 AM
  5. DOS, Serial, and Touch Screen
    By jon_nc17 in forum A Brief History of
    Replies: 0
    Last Post: 01-08-2003, 04:59 PM