define constants

This is a discussion on define constants within the C++ Programming forums, part of the General Programming Boards category; What is the best way to define constants in a C++ header file? Currently I am using the following format. ...

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

    define constants

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

    constant.H

    //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
    ie

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

    Shuo

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

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    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
    Location
    Toronto, ON
    Posts
    3,545
    Why use #define when a const double would do just fine?

  6. #6
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    917
    Also, if you want const unsigned int types, it's permissible to do
    Code:
    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 11: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
    Location
    Canada
    Posts
    8,048
    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: Marching coordinates
    Last edited by dwks; 09-07-2007 at 04:21 PM.
    dwk

    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: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    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, 04: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, 11:05 AM
  5. DOS, Serial, and Touch Screen
    By jon_nc17 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-08-2003, 04:59 PM

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