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. 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
"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
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
"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)
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 int x = --42;
What's wrong with this? It seems perfectly legal to me.
I would expect any compiler to perform double negation.
Unforgiven posted: #define NUMBER 5+2;
If you have #define statements like that then enclose them in brackets.
#define NUMBER (5+2)
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>
"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)
"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)
"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)
"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
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 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 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 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
"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.
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
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
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;
};
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
| |