Const Vs Macro

Discussion in 'C Programming' started by arut, Aug 21, 2003.

  1. arut

    arut Guest

    I would like to know when a const should be used and when a #define is necessary
    in a program using a constant. What are the pros and cons?
    arut, Aug 21, 2003
    #1
    1. Advertising

  2. arut a écrit :

    > I would like to know when a const should be used and when a #define is
    > necessary in a program using a constant. What are the pros and cons?


    'const' means that an object is read-only, not that its value is constant.
    Macro are mostly here to make typing easier. In C, a number written with
    digits (and optionnaly a type specifier) is called a constant.

    for example :

    - 3.1415926 is a constant with type double and the value 3.1415926.

    - #define M_PI 3.1415926

    allows you to type only M_PI instead of 3.1415926 in your code.

    - On the other hand :

    const double K_PI = 3.1415926;

    creates a variable that is read-only. If you use K_PI in your code, you are
    using a variable with the value 3.1415926, not directly the value
    3.1415926.

    In place where the C language need a constant, you can use a macro that will
    be replaced by a constant, but you cannot use a const variable :

    const int K_SIZE = 10;
    #define M_SIZE 10
    enum {E_SIZE = 10};
    int array1[K_SIZE]; /* wrong, K_SIZE is read-only but not a constant */
    int array2[M_SIZE]; /* ok, M_SIZE will be replaced by the constant 10 */
    int array3[E_SIZE]; /* ok, enumerations are constant too */

    IMHO, using const as much as you can and avoiding macros is a good
    programming practice. const help to document the code and can make the code
    more robust. macros often make the code more obfuscated.

    --
    Richard
    Richard Delorme, Aug 21, 2003
    #2
    1. Advertising

  3. arut

    Vivek Guest

    "Richard Delorme" <> wrote in message
    news:3f449215$0$9625$-internet.fr...
    > arut a écrit :
    >
    > > I would like to know when a const should be used and when a #define is
    > > necessary in a program using a constant. What are the pros and cons?

    >
    > 'const' means that an object is read-only, not that its value is constant.
    > Macro are mostly here to make typing easier. In C, a number written with
    > digits (and optionnaly a type specifier) is called a constant.
    >
    > for example :
    >
    > - 3.1415926 is a constant with type double and the value 3.1415926.
    >
    > - #define M_PI 3.1415926
    >
    > allows you to type only M_PI instead of 3.1415926 in your code.
    >
    > - On the other hand :
    >
    > const double K_PI = 3.1415926;
    >
    > creates a variable that is read-only. If you use K_PI in your code, you

    are
    > using a variable with the value 3.1415926, not directly the value
    > 3.1415926.
    >
    > In place where the C language need a constant, you can use a macro that

    will
    > be replaced by a constant, but you cannot use a const variable :
    >
    > const int K_SIZE = 10;
    > #define M_SIZE 10
    > enum {E_SIZE = 10};
    > int array1[K_SIZE]; /* wrong, K_SIZE is read-only but not a constant */
    > int array2[M_SIZE]; /* ok, M_SIZE will be replaced by the constant 10 */
    > int array3[E_SIZE]; /* ok, enumerations are constant too */
    >
    > IMHO, using const as much as you can and avoiding macros is a good
    > programming practice. const help to document the code and can make the

    code
    > more robust. macros often make the code more obfuscated.
    >
    > --
    > Richard


    "const help to document the code and can make the code more robust. macros
    often make the code more obfuscated."
    Could you explain the statement above in more detail.......
    And other places where you cannot you const.
    Vivek, Aug 21, 2003
    #3
  4. arut

    Mark Haigh Guest

    arut wrote:
    > I would like to know when a const should be used and when a #define is necessary
    > in a program using a constant. What are the pros and cons?


    In general, think of macros as a purely textual replacement. Think of a
    const variable as being a read-only variable.

    Because of the textual nature of preprocessing, macros are usually
    considered less type-safe, beause type information is often not included
    in the #define. You could try to get around this by casting the value
    within the macro itself, but this is usually considered bad form because
    it circumvents the C type system. Using type suffixes for numerical
    values alleviates this problem, for the most part (ie 123UL notifies the
    compiler that the type of 123 is unsigned long).

    Scoping rules are also different. Macros have file scope, where const
    variables have scope that corresponds to where they are declared.

    There are several rules of thumb I use when deciding which to use.
    There aren't that many hard and fast rules; for the most part it's all a
    matter of degree.

    1. If the scope that the 'constant' is used in is small or isolated,
    prefer a const variable allocated in automatic storage (ie stack). It
    will most likely be optimized away by your compiler.

    2. If the constant will be used to specify the size for an array in
    automatic storage, choose a macro (until I can recommend C99 VLA's in
    good concience).

    3. If you need a constant across a bunch of functions or files, use a
    macro. Using a const variable with static storage (even if it's
    declared static) will result in a permanent static storage location
    being allocated for it.

    4. If your expression is complicated, try to break it up into smaller
    chunks of const variables that are initialized via macros:

    /* Example of breaking down a complex calculation */
    void do_calculation(float circle_radius)
    {
    /* Do some kind of complex calculation: */
    const float area = PI * (circle_radius * circle_radius);
    const float foo = (area * FROB_CONSTANT) % FLUX_DIVISOR;
    /* Now do something with it... */
    }


    Now for the rules just about everybody agrees on. These are mandatory!

    1. Always use all caps for macro names.
    2. Always use underscores to seperate words in macro names.
    3. Always use type suffixes for numerical constants.


    I think I hit about everything. Did I leave anything out?

    Mark


    Mark Haigh
    Mark Haigh, Aug 21, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    11
    Views:
    1,099
  2. Javier
    Replies:
    2
    Views:
    559
    James Kanze
    Sep 4, 2007
  3. 0m
    Replies:
    26
    Views:
    1,111
    Tim Rentsch
    Nov 10, 2008
  4. fungus
    Replies:
    13
    Views:
    886
    fungus
    Oct 31, 2008
  5. Replies:
    2
    Views:
    540
    Andrew Koenig
    Feb 9, 2009
Loading...

Share This Page