Skip to main content
gamedev.net
🔒 Locked

const or #define

Started by SumDude 29 replies 2.2k views
Original Post
SumDude
SumDude
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?
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                                                          
Looking for video game music? Check out some of my samples at http://www.youtube.c...ser/cminortunes            
                                                          
I'm currently looking to create music for a project, if you are inte
intransigent-seal
intransigent-seal
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]
The best thing about the internet is the way people with no experience or qualifications can pretend to be completely superior to other people who have no experience or qualifications.
krez
krez
#define x y 

just tells the preprocessor to replace all occurances of x with y.. doing it with const actually creates a variable that holds that const value.

some people get mad if you #define things that could be const instead.
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
flangazor
flangazor
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.
Qw3r7yU10p!
Qw3r7yU10p!
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.
Way Walker
Way Walker
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.
eldee
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)

-eldee
;another space monkey;
[ Forced Evolution Studios ]

::evolve::

Do NOT let Dr. Mario touch your genitals. He is not a real doctor!

daerid
daerid
standard C doesn''t even define the keyword "const"
daerid@gmail.com
Wreakon
Wreakon
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://www.pclx.com
Null and Void
Null and Void
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]
Way Walker
Way Walker
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)
Metorical
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]
Zipster
Zipster
"Find and replace" is very lazy terminology. It isn''t a raw find and replace, it''s more like a smart tokenized find and replace. I knew what you meant, but it''s kinda sloppy to call it that.
Miserable
Miserable
If I recall correctly, C did not originally include the keyword const. However, in the current C standard, the const keyword is a part of the language, having been borrowed from C++ (clearly, the borrowing goes both ways to some degree).
EdR
EdR
#define is good for one use.

#define endless_loop while(1)

I drive my CS teach nuts with that.
http://edropple.com
ChaosEngine
ChaosEngine
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
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
Sneftel
Sneftel
quote:
Original post by Metorical
In the defense of #define

Finite State Machines...

I rest my case.

-Meto

Er? I''ve never had a need for #define when programming FSMs. enums, yeah.... no defines.


How appropriate. You fight like a cow.
Khaos Dragon
Khaos Dragon
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.