473,387 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 11649
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

97
by: s | last post by:
Can I do this: #define MYSTRING "ABC" .. .. .. char mychar = MYSTRING; .. .. ..
9
by: bill | last post by:
Forget the exact definition of difference between, #include <foo.h> and #include "bar.h" Normally foo.h is a standard header file, so it's path is not defined in compiler option, but I...
9
by: ccwork | last post by:
Hi all, We can define some magic number with #define: #define OPERATION_1 0x00000001 #define OPERATION_2 0x00000002 #define OPERATION_3 0x00000003 .... We can also do that with enum: enum...
10
by: Randy | last post by:
Hi, Can anyone point me to a complete, compilable example of Besser's ENUM++ mechanism? I downloaded it from CUJ and gave it a try but got errors just trying to compile the header enum.h. ...
12
by: Laurent Deniau | last post by:
If I understand well, an enumeration is only garantee to hold at most an int (6.7.2.2-2). So I would like to know: how to store a long in an enum? enum { p2_31 = 1L << 31 }; // boom how to...
12
by: Cmtk Software | last post by:
I'm trying to define an enum which will be used from unmanaged c++, C++/CLI managed c++ and from C#. I defined the following enum in a VS dll project set to be compiled with the /clr switch: ...
1
by: Alex Vinokur | last post by:
A program below prints "Second". A preprocessor doesn't know BBB, but it seems that it (preprocessor) uses 0 unstead of BBB. Why doesn't the preprocessor produce error/warning: "BBB is indefined"?...
6
by: anirbid.banerjee | last post by:
Hi, I need to write a macro which would have a lot many conditional #ifdef ... #endif blocks in it. #define _xx_macro (x) { ... \ ... \ /* some code (); */ #ifdef _SOME_STMT \ ... \ ... \
2
by: badc0de | last post by:
Hello.. a header file of one of my project has macro definitions like this (for example) #define PART_A_SORT_1_LABEL_STRING1 100 #define PART_A_SORT_1_LABEL_STRING2 200 #define...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.