468,496 Members | 1,791 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,496 developers. It's quick & easy.

Difference between enum and #define preprocessor

Hi
Can any one tell me the difference between preprocessor macros like
#define and enum.
If i want to define MAX_LIMIT=100 as preprocessor macro as
#define MAX_LIMIT 100
or if i define enum {MAX_LIMIT=100}; then what is the basic difference?
Is there any efficiency tradeoff between both the definitions.

Aug 11 '06 #1
3 11326
"ashwani" <as*********@gmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
: Hi
: Can any one tell me the difference between preprocessor macros like
: #define and enum.
: If i want to define MAX_LIMIT=100 as preprocessor macro as
: #define MAX_LIMIT 100
: or if i define enum {MAX_LIMIT=100}; then what is the basic difference?
: Is there any efficiency tradeoff between both the definitions.

No difference in efficiency.

enum-declared constants are to be preferred because they
obey scoping rules (e.g. they can be encapsulated within
a class or namespace).

In a modern compiler, using
const int MAX_LIMIT = 100;
or
static const int MAX_LIMIT = 100;
is also likely to be just as efficient (no space or time
overhead) as a define.
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Aug 11 '06 #2
ashwani wrote:
Hi
Can any one tell me the difference between preprocessor macros like
#define and enum.
If i want to define MAX_LIMIT=100 as preprocessor macro as
#define MAX_LIMIT 100
or if i define enum {MAX_LIMIT=100}; then what is the basic difference?
enum respects scope, #define doesn't.
Is there any efficiency tradeoff between both the definitions.
no.

Btw: If you want a constant, just define a constant and not a macro or an
enum.

Aug 11 '06 #3
ashwani posted:
Hi
Can any one tell me the difference between preprocessor macros like
#define and enum.
If i want to define MAX_LIMIT=100 as preprocessor macro as
#define MAX_LIMIT 100
or if i define enum {MAX_LIMIT=100}; then what is the basic difference?
Is there any efficiency tradeoff between both the definitions.

namespace MyNamespace {

#define ONE 1

enum { TWO = 2 };

int const three = 3;
}

int main()
{
ONE; /* No problem */

TWO; /* Can't find it */

three; /* Can't find it */
MyNamespace::ONE; /* Gibberish! */

MyNamespace::TWO; /* No problem */

MyNamespace::three; /* No problem */
}

--

Frederick Gotham
Aug 11 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

97 posts views Thread by s | last post: by
9 posts views Thread by bill | last post: by
9 posts views Thread by ccwork | last post: by
12 posts views Thread by Laurent Deniau | last post: by
12 posts views Thread by Cmtk Software | last post: by
1 post views Thread by Alex Vinokur | last post: by
6 posts views Thread by anirbid.banerjee | last post: by
2 posts views Thread by badc0de | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.