473,386 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

#define versus const

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.
Jul 22 '05 #1
22 22444
"johny smith" <pr**************@charter.net> wrote in
news:10*************@corp.supernews.com:
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
Jul 22 '05 #2
"johny smith" <pr**************@charter.net> schrieb im Newsbeitrag news:10*************@corp.supernews.com...
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
Jul 22 '05 #3
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

Jul 22 '05 #4
"Heinz Ozwirk" <wa******@gmx.de> wrote in message
news:c9*************@news.t-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)

Jul 22 '05 #5
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
Jul 22 '05 #6
RM
int x = --42;


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

Jul 22 '05 #7
Unforgiven posted:
#define NUMBER 5+2;

If you have #define statements like that then enclose them in brackets.
#define NUMBER (5+2)
Jul 22 '05 #8
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>
Jul 22 '05 #9
"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)

Jul 22 '05 #10
"Paul Mensonides" <le******@comcast.net> wrote in message
news:1-********************@comcast.com
"John Carson" <do***********@datafast.net.au> wrote in message
news:40********@usenet.per.paradox.net.au...
"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)

Jul 22 '05 #11
"Duane Hebert" <sp**@flarn2.com> wrote in message
news:IC**********************@wagner.videotron.net
"Julie" <ju***@nospam.com> wrote in message
news:40***************@nospam.com...
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)

Jul 22 '05 #12
"John Carson" <do***********@datafast.net.au> wrote in message
news:40******@usenet.per.paradox.net.au...
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
Jul 22 '05 #13
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
Jul 22 '05 #14
JKop wrote:

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


Hi JKop,

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

[ ]

-Luther

Jul 22 '05 #15
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
Jul 22 '05 #16
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
Jul 22 '05 #17

"John Carson" <do***********@datafast.net.au> wrote in message
news:40********@usenet.per.paradox.net.au...
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.
Jul 22 '05 #18
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?


Here's another reason in addition to those already given:

#define MACRO 1.653

const double CONST = 1.653;

double Square(double const *p)
{
return (*p) * (*p);
}

int main()
{
double a = Square(&MACRO); // won't compile

double b = Square(&CONST); // compiles OK

return 0;
}

--
Mike Smith

Jul 22 '05 #19
Mike Smith <mi*******************@acm.DOT.org> wrote:
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?
Here's another reason in addition to those already given:

#define MACRO 1.653

const double CONST = 1.653;

double Square(double const *p)
{
return (*p) * (*p);
}

int main()
{
double a = Square(&MACRO); // won't compile


Certainly not! After the preprocessor finishes with this line it look
like this:

double a = Square(&1.653);

which is meaningless.

double b = Square(&CONST); // compiles OK
CONST is a variable, not a macro, so the preprocessor does nothing to
this line.
return 0;
}

--
Tim Slattery
Sl********@bls.gov
Jul 22 '05 #20
Tim Slattery wrote:

Here's another reason in addition to those already given:

#define MACRO 1.653

const double CONST = 1.653;

double Square(double const *p)
{
return (*p) * (*p);
}

int main()
{
double a = Square(&MACRO); // won't compile
Certainly not! After the preprocessor finishes with this line it look
like this:

double a = Square(&1.653);

which is meaningless.


Didn't the comment in this line say: won't compile ?

double b = Square(&CONST); // compiles OK


CONST is a variable, not a macro, so the preprocessor does nothing to
this line.


And that's why Mike marked this line as : compiles OK
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #21
JKop <NU**@NULL.NULL> wrote in message news:<GK***************@news.indigo.ie>...
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.

How about angle brackets? <g>

Dave Moore
Jul 22 '05 #22
Karl Heinz Buchegger wrote:
Tim Slattery wrote:
Here's another reason in addition to those already given:

#define MACRO 1.653

const double CONST = 1.653;

double Square(double const *p)
{
return (*p) * (*p);
}

int main()
{
double a = Square(&MACRO); // won't compile


Certainly not! After the preprocessor finishes with this line it look
like this:

double a = Square(&1.653);

which is meaningless.

Didn't the comment in this line say: won't compile ?

double b = Square(&CONST); // compiles OK


CONST is a variable, not a macro, so the preprocessor does nothing to
this line.

And that's why Mike marked this line as : compiles OK


I suppose I should simply have said that a const object is an object,
and can be treated like one, whereas a literal substitution cannot. <sigh>

--
Mike Smith

Jul 22 '05 #23

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: franky.backeljauw | last post by:
Hello, I am wondering which of these two methods is the fastest: std::copy, which is included in the standard library, or a manually written pointer copy? Do any of you have any experience with...
8
by: John Ratliff | last post by:
Let's say I had a program which uses some constants. Would it be "better" to declare them like this: #ifndef __MY_HDR_FILE #define __MY_HDR_FILE #define STRING_CONST "some string..." #define...
4
by: Rayer | last post by:
I have got some project src, and find it out that there is no ppl using "const" to define a constance, but using #define still more. Discussing with my friend, he said 1. #define is much easier in...
4
by: yancheng.cheok | last post by:
Recently, I try to replace #define with const in header file. However, there are concerns on, multiple const object will be created, if the header file is included in multiple cpp files. For...
10
by: Christian Christmann | last post by:
Hi, what is the difference between a "#define" and typedef? A "#define" is handled by the preprocessor and serves as a name substitude whereas "typedef" is a declaration of a new data type...
1
by: ThazKool | last post by:
I know that the "const int" declaration gives us more control with scoping. However if I make a file with thousands of const int declarations, will this increase exe size if I do not use them? I...
9
by: blangela | last post by:
Can somepoint me to a good discussion on the pros and cons of using #define versus a constant variable in your C+ application? Thanks, Bob
5
by: MoslyChang | last post by:
Hi, All When I look at effective c++,item2 and item3. I have some basic questions , Does anyone be familar with this topic? it suggests const is perfer to #define, then I think how to replace...
23
by: anon.asdf | last post by:
Hello! In the following code-snippet, is it possible to initialize each element of arr, with STRUCT_INIT? struct mystruct { int a; char b; };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.