🔒 Locked
const or #define
Advertisement
Original Post
This might be a dumb post but what is the difference of const and #define really? The only thing i know about both of them is that #define is a preprocessor keyword and const is typesafe. Wouldn''t a global const be exactly the same thing as #define or is there some sort of difference?
As you said yourself, const is typesafe.
And also, #define can be used for more than just constants - it can also be used for macros (which I generally wouldn't recommend, although they have some uses), and for conditional compilation (which can be very useful if you're careful)
Edit:
Also, #define's can be #undef'ed, whereas a constant will stay available throughout the scope that it's created in.
John B
[edited by - JohnBSmall on May 19, 2003 12:32:15 PM]
And also, #define can be used for more than just constants - it can also be used for macros (which I generally wouldn't recommend, although they have some uses), and for conditional compilation (which can be very useful if you're careful)
Edit:
Also, #define's can be #undef'ed, whereas a constant will stay available throughout the scope that it's created in.
John B
[edited by - JohnBSmall on May 19, 2003 12:32:15 PM]
The people above me are not wrong.
macro''s get a bad wrap, but they aren''t all bad. However, with template meta programming (i.e. a kludgy wannabe form of Lisp macros), #define is getting less and less useful and being relegated to merely determining conditional compilation.
They used to be used for things like max(x, y) and min(x, y) etc.
macro''s get a bad wrap, but they aren''t all bad. However, with template meta programming (i.e. a kludgy wannabe form of Lisp macros), #define is getting less and less useful and being relegated to merely determining conditional compilation.
They used to be used for things like max(x, y) and min(x, y) etc.
Yep, if you can do it without the preprocessor do it without the preprocessor.
I was trying write a class that made use of the CDC::TextOut function. I called my function TextOut. It wouldn''t compile.
I found that TextOut is actually a macro defined in wingdi.h, pulled in by windows.h It is either TextOutW or TextOutA depending on whether UNICODE is defined.
In my implementation file the function was getting wiped out by the macro and in my class'' header file there wasn''t any inclusion of the windows file, so the function kept its original name. Nice.
So anywhere you''ve included wingdi.h you can''t make use of the name TextOut regardless of if its private or in a namespace or any other normal hiding strategies.
C macros are on the whole evil, apart from a few limited and justifiable exceptions.
I was trying write a class that made use of the CDC::TextOut function. I called my function TextOut. It wouldn''t compile.
I found that TextOut is actually a macro defined in wingdi.h, pulled in by windows.h It is either TextOutW or TextOutA depending on whether UNICODE is defined.
In my implementation file the function was getting wiped out by the macro and in my class'' header file there wasn''t any inclusion of the windows file, so the function kept its original name. Nice.
So anywhere you''ve included wingdi.h you can''t make use of the name TextOut regardless of if its private or in a namespace or any other normal hiding strategies.
C macros are on the whole evil, apart from a few limited and justifiable exceptions.
Also, I remember hearing that in C (not C++) you''re supposed to use #define rather than const because it treats them differently. One other thing to keep in mind is that you can take the address of a const object but not a #define constant. Also, C doesn''t have templates, so macros are still useful for things like max and min.
the way i understood it, was that a #define would tell the compiler to find/replace every instance (without being typesafe of course), where a const global was just that- a global variable that was const.
if const''s initiated a find/replace like #defines do, i dont see any reason to actually use a #define (other than for a macro, ect)
-eldee
;another space monkey;
[ Forced Evolution Studios ]
if const''s initiated a find/replace like #defines do, i dont see any reason to actually use a #define (other than for a macro, ect)
-eldee
;another space monkey;
[ Forced Evolution Studios ]

Do NOT let Dr. Mario touch your genitals. He is not a real doctor!
There is a slight difference that was I believe omitted. The idea of #define is that it doesn''t really use memory the same way. I.e. having a const global will always take some amount of memory, while having a define will only use the memory when it is used (can be thought of as none at all).
-------
Homepage: http://students.washington.edu/andrey
-------
Homepage: http://students.washington.edu/andrey
I prefer consts or enums for specifying constants, and templates or inline functions instead of macros.
Why you shouldn''t use iostream.h - ever! | A Good free online C++ book
Why you shouldn''t use iostream.h - ever! | A Good free online C++ book
quote:
Original post by daerid
standard C doesn't even define the keyword "const"
Did you check? It seems that you did not:
quote:
C99 Standard Draft, section 6.4.1 (page 51)
6.4.1 Keywords
Syntax
keyword: one of
auto enum restrict unsigned
break extern return void
case float short volatile
char for signed while
const goto sizeof _Bool
continue if static _Complex
default inline struct _Imaginary
do int switch
double long typedef
else register union
Semantics
The above tokens (case sensitive) are reserved (in translation phases 7 and 8) for use as keywords, and shall not be used otherwise.
More on const in C can be found in section 6.7.3 (page 108). Edit: it figures that I'd transpose two characters while quoting

[edited by - Null and Void on May 19, 2003 6:25:34 PM]
quote:
MDI
C has very limited capacity for const, IIRC.
Would you (or anyone) be able to ellaborate on this?
quote:
eldee
the way i understood it, was that a #define would tell the compiler to find/replace every instance (without being typesafe of course), where a const global was just that- a global variable that was const. if const''s initiated a find/replace like #defines do, i dont see any reason to actually use a #define (other than for a macro, ect)
consts don''t do a find/replace, but some compilers will treat it as such.
#define STUFF 1
func(STUFF);
When the preprocessor sees STUFF it changes that to
func(1);
If you had written
const int STUFF = 1;
func(STUFF);
The compiler can think "hmm... STUFF is const, so it will never change, so if I just put a 1 in here nobody will ever notice" and changes it to
func(1);
I guess, it''s similar to how a macro is always inlined where an inline function tells the compiler that you think it would be a good idea to inline this.
quote:
siaspete
I prefer consts or enums for specifying constants, and templates or inline functions instead of macros.
They can do different things. For instance, #defines can be used in conditional compilation, you can take the address of a const variable, and enums allow only certain values to be past into the function. Also, #defines and enums don''t take any memory, which can be an issue.
quote:
daerid
standard C doesn''t even define the keyword "const"
It''s been part of standard C since at least C89, and I think it was even in K&R1 (which, in many ways, was the first C standard)
In the defense of #define
Finite State Machines...
I rest my case.
-Meto
p.s. not that anyone was attacking #define. I think this is one of the situations where #define can be used liberally.
If the question is const or #define then const.
[edited by - Metorical on May 19, 2003 6:41:18 PM]
Finite State Machines...
I rest my case.
-Meto
p.s. not that anyone was attacking #define. I think this is one of the situations where #define can be used liberally.
If the question is const or #define then const.
[edited by - Metorical on May 19, 2003 6:41:18 PM]
quote:
Original post by Metorical
In the defense of #define
Finite State Machines...
I rest my case.
-Meto
p.s. not that anyone was attacking #define. I think this is one of the situations where #define can be used liberally.
If the question is const or #define then const.
[edited by - Metorical on May 19, 2003 6:41:18 PM]
ok. now I''m curious.... why FSMs?
"That''s not a bug, it''s a feature!"
--me
Couldn''t one argue that #define doesn''t require one to create a temporary variable to store a value and then read from that value later and is thus faster than a const, even if the speed difference is negligible?
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement