![]() |
|
|
|
#1 |
|
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 |
|
|
|
|
#2 |
|
Posts: n/a
|
"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 |
|
|
|
#3 |
|
Posts: n/a
|
"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 |
|
|
|
#4 |
|
Posts: n/a
|
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 |
|
|
|
#5 |
|
Posts: n/a
|
"Heinz Ozwirk" <> wrote in message
news:c99h3v$sph$01$ > > 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 |
|
|
|
#6 |
|
Posts: n/a
|
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 |
|
|
|
#7 |
|
Posts: n/a
|
> int x = --42; > What's wrong with this? It seems perfectly legal to me. I would expect any compiler to perform double negation. RM |
|
|
|
#8 |
|
Posts: n/a
|
Unforgiven posted:
> #define NUMBER 5+2; If you have #define statements like that then enclose them in brackets. #define NUMBER (5+2) JKop |
|
|
|
#9 |
|
Posts: n/a
|
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 |
|
|
|
#10 |
|
Posts: n/a
|
"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 |
|
![]() |
| Thread Tools | Search this Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Low Latency DDR2 800MHz versus 1,066MHz | Admin | Front Page News | 0 | 10-28-2008 01:22 PM |
| Re: Athlon XP 64 (Mobile) versus Pentium (Mobile) | Jamco | A+ Certification | 0 | 02-01-2005 09:02 PM |
| Benefit of a DVD recorder versus a burner?? | RichA | DVD Video | 4 | 01-09-2005 08:23 PM |
| 'superbit' DVD's versus ordinary DVD's... how much better? | Mike | DVD Video | 37 | 07-13-2004 10:45 AM |
| Windows XP versus Windows 2000 | Joe | A+ Certification | 6 | 12-21-2003 04:21 AM |