#define versus const

Discussion in 'C++' started by johny smith, May 29, 2004.

  1. johny smith

    johny smith Guest

    I have never really understood the difference between

    1.) #define NUMBER 1.653

    vs.

    2.) const double NUMBER = 1.653

    I know that the #define is taken care of by the pre-processor and the
    compiler is used in the second case but why would one be chosen over the
    other.

    One book I read suggested that your should prefer the compiler to the
    preprocessor, but why does it really matter?

    Thanks in advance.
    johny smith, May 29, 2004
    #1
    1. Advertising

  2. johny smith

    Gregg Guest

    "johny smith" <> wrote in
    news::

    > I have never really understood the difference between
    >
    > 1.) #define NUMBER 1.653
    >
    > vs.
    >
    > 2.) const double NUMBER = 1.653
    >
    > I know that the #define is taken care of by the pre-processor and the
    > compiler is used in the second case but why would one be chosen over

    the
    > other.
    >
    > One book I read suggested that your should prefer the compiler to the
    > preprocessor, but why does it really matter?
    >
    > Thanks in advance.
    >
    >
    >


    One reason is that you will typically get more meaningful diagnostics if
    you misuse the symbol.

    But a more important reason is that, unlike #define, a const symbol obeys
    scoping rules, so it won't have the potential for nasty side-effects,
    substituting itself where it doesn't belong.

    Gregg
    Gregg, May 29, 2004
    #2
    1. Advertising

  3. johny smith

    Heinz Ozwirk Guest

    "johny smith" <> schrieb im Newsbeitrag news:...
    > I have never really understood the difference between
    >
    > 1.) #define NUMBER 1.653
    >
    > vs.
    >
    > 2.) const double NUMBER = 1.653
    >
    > I know that the #define is taken care of by the pre-processor and the
    > compiler is used in the second case but why would one be chosen over the
    > other.
    >
    > One book I read suggested that your should prefer the compiler to the
    > preprocessor, but why does it really matter?


    One reason, among many others:

    const int NUMBER = -42;
    int main()
    {
    int x = -NUMBER;
    }

    Now replace the const by a #define and let a novice try to understand the compiler's message.

    Heinz
    Heinz Ozwirk, May 29, 2004
    #3
  4. johny smith

    Unforgiven Guest

    johny smith wrote:
    > I have never really understood the difference between
    >
    > 1.) #define NUMBER 1.653
    >
    > vs.
    >
    > 2.) const double NUMBER = 1.653
    >
    > I know that the #define is taken care of by the pre-processor and the
    > compiler is used in the second case but why would one be chosen over
    > the other.
    >
    > One book I read suggested that your should prefer the compiler to the
    > preprocessor, but why does it really matter?


    For one thing #define is not typesafe, while const is.

    Another thing to keep in mind is that #define is text substitution, not a
    variable.
    That means that if you do this:
    #define NUMBER 5+2;

    int x = 3 * NUMBER;

    The value of x is now 17, while with a const it would've been 21.

    --
    Unforgiven
    Unforgiven, May 29, 2004
    #4
  5. johny smith

    John Carson Guest

    "Heinz Ozwirk" <> wrote in message
    news:c99h3v$sph$01$-online.com
    >
    > One reason, among many others:
    >
    > const int NUMBER = -42;
    > int main()
    > {
    > int x = -NUMBER;
    > }
    >
    > Now replace the const by a #define and let a novice try to understand
    > the compiler's message.
    >
    > Heinz


    I assume that you expect that it will expand to

    int x = --42;

    and hence an error. Interestingly enough, neither VC++ 7.1 (which assigns 42
    to x) nor Comeau online give errors.


    --
    John Carson
    1. To reply to email address, remove donald
    2. Don't reply to email address (post here instead)
    John Carson, May 29, 2004
    #5
  6. johny smith

    Petec Guest

    Unforgiven wrote:
    <snip>
    >
    > #define NUMBER 5+2;
    >
    > int x = 3 * NUMBER;
    >
    > The value of x is now 17, while with a const it would've been 21.


    And with a semicolon on the end of your #define, if you do this:
    int x = NUMBER * 3;

    You will get a compile error.

    - Pete
    Petec, May 29, 2004
    #6
  7. johny smith

    RM Guest


    > int x = --42;
    >


    What's wrong with this? It seems perfectly legal to me.
    I would expect any compiler to perform double negation.
    RM, May 29, 2004
    #7
  8. johny smith

    JKop Guest

    Unforgiven posted:

    > #define NUMBER 5+2;



    If you have #define statements like that then enclose them in brackets.


    #define NUMBER (5+2)
    JKop, May 29, 2004
    #8
  9. johny smith

    Julie Guest

    JKop wrote:
    >
    > Unforgiven posted:
    >
    > > #define NUMBER 5+2;

    >
    > If you have #define statements like that then enclose them in brackets.
    >
    > #define NUMBER (5+2)


    You mean enclose them in parentheses.

    <nitpick>

    ( is a parenthesis, plural parentheses

    [ is a bracket

    { is a brace

    < is an angle bracket

    </nitpick>
    Julie, May 29, 2004
    #9
  10. johny smith

    John Carson Guest

    "RM" <I_am_not_@_home.com> wrote in message
    news:XM2uc.582962$Pk3.139545@pd7tw1no
    >> int x = --42;
    >>

    >
    > What's wrong with this? It seems perfectly legal to me.
    > I would expect any compiler to perform double negation.


    -- is interpreted as the prefix decrement operator, not as a double minus.
    You cannot apply the decrement operator to a constant (it must be a
    modifiable lvalue).

    To get double negation, you need a space between the two minus signs.


    --
    John Carson
    1. To reply to email address, remove donald
    2. Don't reply to email address (post here instead)
    John Carson, May 29, 2004
    #10
  11. "John Carson" <> wrote in message
    news:40b8ccf4$...
    > "RM" <I_am_not_@_home.com> wrote in message
    > news:XM2uc.582962$Pk3.139545@pd7tw1no
    > >> int x = --42;
    > >>

    > >
    > > What's wrong with this? It seems perfectly legal to me.
    > > I would expect any compiler to perform double negation.

    >
    > -- is interpreted as the prefix decrement operator, not as a double minus.
    > You cannot apply the decrement operator to a constant (it must be a
    > modifiable lvalue).
    >
    > To get double negation, you need a space between the two minus signs.


    The preprocessor operates on preprocessing tokens, not text. The "splicing"
    that you appears above happens after tokenization. This results in two adjacent
    minus tokens (-) with no intervening whitespace. It is absolutely not a
    pre-decrement operator.

    Regards,
    Paul Mensonides
    Paul Mensonides, May 30, 2004
    #11
  12. johny smith

    Duane Hebert Guest

    "Julie" <> wrote in message news:...
    > JKop wrote:
    > >
    > > Unforgiven posted:
    > >
    > > > #define NUMBER 5+2;

    > >
    > > If you have #define statements like that then enclose them in brackets.
    > >
    > > #define NUMBER (5+2)

    >
    > You mean enclose them in parentheses.
    >
    > <nitpick>
    >
    > ( is a parenthesis, plural parentheses
    >
    > [ is a bracket
    >
    > { is a brace
    >
    > < is an angle bracket
    >
    > </nitpick>

    Unless of course you're not in the states.
    In Canada for example.
    Duane Hebert, May 30, 2004
    #12
  13. johny smith

    John Carson Guest

    "Paul Mensonides" <> wrote in message
    news:
    > "John Carson" <> wrote in message
    > news:40b8ccf4$...
    >> "RM" <I_am_not_@_home.com> wrote in message
    >> news:XM2uc.582962$Pk3.139545@pd7tw1no
    >>>> int x = --42;
    >>>>
    >>>
    >>> What's wrong with this? It seems perfectly legal to me.
    >>> I would expect any compiler to perform double negation.

    >>
    >> -- is interpreted as the prefix decrement operator, not as a double
    >> minus. You cannot apply the decrement operator to a constant (it
    >> must be a modifiable lvalue).
    >>
    >> To get double negation, you need a space between the two minus signs.

    >
    > The preprocessor operates on preprocessing tokens, not text. The
    > "splicing" that you appears above happens after tokenization. This
    > results in two adjacent minus tokens (-) with no intervening
    > whitespace. It is absolutely not a pre-decrement operator.
    >
    > Regards,
    > Paul Mensonides


    I was referring to

    int x = --42;

    in the source code. Such source code will not compile because -- is
    interpreted as the prefix decrement operator.

    --
    John Carson
    1. To reply to email address, remove donald
    2. Don't reply to email address (post here instead)
    John Carson, May 30, 2004
    #13
  14. johny smith

    John Carson Guest

    "Duane Hebert" <> wrote in message
    news:ICauc.156569$
    > "Julie" <> wrote in message
    > news:...
    >> JKop wrote:
    >>>
    >>> Unforgiven posted:
    >>>
    >>>> #define NUMBER 5+2;
    >>>
    >>> If you have #define statements like that then enclose them in
    >>> brackets.
    >>>
    >>> #define NUMBER (5+2)

    >>
    >> You mean enclose them in parentheses.
    >>
    >> <nitpick>
    >>
    >> ( is a parenthesis, plural parentheses
    >>
    >> [ is a bracket
    >>
    >> { is a brace
    >>
    >> < is an angle bracket
    >>
    >> </nitpick>

    > Unless of course you're not in the states.
    > In Canada for example.


    I tend to call ( and ) round brackets. The Oxford dictionary agrees that
    this is a correct usage. However, the C++ standard takes a different view
    and I think it is the relevant authority in this case. It consistently uses
    the word "parentheses".


    --
    John Carson
    1. To reply to email address, remove donald
    2. Don't reply to email address (post here instead)
    John Carson, May 30, 2004
    #14
  15. "John Carson" <> wrote in message
    news:...

    > I was referring to
    >
    > int x = --42;
    >
    > in the source code. Such source code will not compile because -- is
    > interpreted as the prefix decrement operator.


    Okay, my apologies.

    Regards,
    Paul Mensonides
    Paul Mensonides, May 30, 2004
    #15
  16. johny smith

    JKop Guest

    Julie posted:

    > JKop wrote:
    >>
    >> Unforgiven posted:
    >>
    >> > #define NUMBER 5+2;

    >>
    >> If you have #define statements like that then enclose them in brackets.
    >>
    >> #define NUMBER (5+2)

    >
    > You mean enclose them in parentheses.
    >
    ><nitpick>
    >
    > ( is a parenthesis, plural parentheses
    >
    > [ is a bracket
    >
    > { is a brace
    >
    >< is an angle bracket
    >
    ></nitpick>



    I'm in Ireland, (and we call them brackets).


    -JKop
    JKop, May 30, 2004
    #16
  17. johny smith

    Luther Baker Guest

    JKop wrote:
    >
    > I'm in Ireland, (and we call them brackets).
    >


    Hi JKop,

    Curious, what do you call the squared *thingies* in Ireland?

    [ ]

    -Luther
    Luther Baker, May 30, 2004
    #17
  18. johny smith

    JKop Guest

    Luther Baker posted:

    > JKop wrote:
    >>
    >> I'm in Ireland, (and we call them brackets).
    >>

    >
    > Hi JKop,
    >
    > Curious, what do you call the squared *thingies* in Ireland?
    >
    > [ ]
    >
    > -Luther
    >
    >


    ( ) Brackets

    [ ] Square brackets / Block brackets

    { } Chain brackets


    Sin a nglaonn muid orthu in √Čirinn!


    -JKop
    JKop, May 30, 2004
    #18
  19. johny smith

    JKop Guest

    JKop posted:

    > Luther Baker posted:
    >
    >> JKop wrote:
    >>>
    >>> I'm in Ireland, (and we call them brackets).
    >>>

    >>
    >> Hi JKop,
    >>
    >> Curious, what do you call the squared *thingies* in Ireland?
    >>
    >> [ ]
    >>
    >> -Luther
    >>
    >>

    >
    > ( ) Brackets
    >
    > [ ] Square brackets / Block brackets
    >
    > { } Chain brackets
    >
    >
    > Sin a nglaonn muid orthu in √Čirinn!
    >
    >
    > -JKop



    As for:

    < >


    They're not used very much. We'd probably called them Arrow brackets or V
    brackets.


    -JKop
    JKop, May 30, 2004
    #19
  20. johny smith

    Duane Guest

    "John Carson" <> wrote in message
    news:40b96dea$...

    > I tend to call ( and ) round brackets. The Oxford dictionary agrees that
    > this is a correct usage. However, the C++ standard takes a different view
    > and I think it is the relevant authority in this case. It consistently uses
    > the word "parentheses".


    Except for where it refers to expressions being "bracketed" <g>

    It's funny in Quebec, in French it's basically the same as in English
    (parentheses etc.) but in English, parenthesis are brackets and
    brackets are square brackets and braces are curly brackets.
    Duane, May 31, 2004
    #20
    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. ThazKool
    Replies:
    1
    Views:
    430
  2. Ook
    Replies:
    8
    Views:
    389
    Victor Bazarov
    Mar 17, 2007
  3. Javier
    Replies:
    2
    Views:
    533
    James Kanze
    Sep 4, 2007
  4. paulo
    Replies:
    9
    Views:
    687
    James Kanze
    Mar 6, 2009
  5. Paul Butcher
    Replies:
    12
    Views:
    677
    Gary Wright
    Nov 28, 2007
Loading...

Share This Page