473,804 Members | 2,109 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

#define versus a constant variable

Can somepoint me to a good discussion on the pros and cons of using
#define versus a constant variable in your C+ application?

Thanks,

Bob

May 11 '07 #1
9 3738
blangela wrote:
Can somepoint me to a good discussion on the pros and cons of using
#define versus a constant variable in your C+ application?
I guess you could search for "Macros are evil", both in the FAQ and
in the archives.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 11 '07 #2
On May 11, 2:37 pm, blangela <Bob_Langel...@ telus.netwrote:
Can somepoint me to a good discussion on the pros and cons of using
#define versus a constant variable in your C+ application?

Thanks,

Bob
pros: none
cons: #define is not type safe

And more importantly, a constant variable is an object, a #define is
not

May 11 '07 #3
On May 11, 2:37 pm, blangela <Bob_Langel...@ telus.netwrote:
Can somepoint me to a good discussion on the pros and cons of using
#define versus a constant variable in your C+ application?
There are no pros to using #define.

The most likely "con" is that symbols defined by the preprocessor are
not available in the debugger (except for one compiler I have worked
with).

The next most likely "con" is unexpected text substitutions.

Another con is your value might get redefined.

May 11 '07 #4
blangela <Bo***********@ telus.netwrote:
Can somepoint me to a good discussion on the pros and cons of using
#define versus a constant variable in your C+ application?
Well, a short list is given in the FAQ:
http://www.parashift.com/c++-faq-lit....html#faq-29.7

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
May 11 '07 #5
Salt_Peter wrote:
On May 11, 2:37 pm, blangela <Bob_Langel...@ telus.netwrote:
>Can somepoint me to a good discussion on the pros and cons of using
#define versus a constant variable in your C+ application?

Thanks,

Bob

pros: none
cons: #define is not type safe
The con above is bullshit. The type is just as safe as anything
else (at least if we are talking numerics and string literals which
is usually what is involved).

The biggest problem is that they don't have scoping.
May 12 '07 #6
blangela wrote:
Can somepoint me to a good discussion on the pros and cons of using
#define versus a constant variable in your C+ application?
A #define has always global scope in the current compilation unit
(ie. the source file where it's defined) regardless of where you
define it, while a const variable can have any scope (all the way
from global to the whole program to local to one {} block).

You can't have a pointer to a #defined constant, but you can to a
const variable.

A #define can basically only define internal types (ints, etc),
while a const variable can define anything, such as std::strings
and so on.
May 12 '07 #7
Juha Nieminen wrote:
blangela wrote:
>Can somepoint me to a good discussion on the pros and cons of using
#define versus a constant variable in your C+ application?

A #define has always global scope in the current compilation unit
(ie. the source file where it's defined) regardless of where you
define it, while a const variable can have any scope (all the way
from global to the whole program to local to one {} block).
Actually it totally disregards scope. It doesn't even obey the syntax
rules that an identifier would.
May 12 '07 #8
ajk
On 11 May 2007 11:37:51 -0700, blangela <Bo***********@ telus.net>
wrote:
>Can somepoint me to a good discussion on the pros and cons of using
#define versus a constant variable in your C+ application?

Thanks,

Bob
constants are more flexible than #define's

#define C "1"

const char *C = "1";

char const *C = "1";

so u can decide what the user can do with it.
--

#define C myclass(123)

const myclass C = myclass(123);

in the former case typically several instances are created of
myclass(123) however in the latter, only one is created.

br/ajk
May 12 '07 #9

ajk <fo*@nomail.com wrote in message ...
On 11 May 2007 11:37:51 -0700, blangela <Bo***********@ telus.net>
wrote:
Can somepoint me to a good discussion on the pros and cons of using
#define versus a constant variable in your C+ application?
Thanks, Bob

constants are more flexible than #define's
#define C "1"

const char *C = "1";
char const *C = "1";
Those two are identical. Did you mean:
char const *C = "1"; // same as 'const char'. 'char' is ro.
char * const C = "1"; // pointer is ro. 'char' is rw.

[ ro == read-only, rw == read-write ]
--
Bob R
POVrookie
May 12 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

97
27825
by: s | last post by:
Can I do this: #define MYSTRING "ABC" .. .. .. char mychar = MYSTRING; .. .. ..
1
2344
by: Christopher M. Lusardi | last post by:
Hello, How is this possible. If I have two separate files that include the same dot h file as described below why am I allowed to access the same value for a constant. I compiled this program using a makefile. (Sorry, I can't post the long makefile or program.) What the code is suppose to do is: main () includes the file and the variable multible_defined_var actually gets defined. file2.c will get the external definition only because...
19
16307
by: Robert | last post by:
Greetings everyone, I was wondering if a const variable or object took up space. I know that a #define'd macro doesn't, as it's basically just interpreted by the compiler. If a const does take up space, is there any reason to choose it over a #define'd constant? -- Thank you P.S. if it makes any difference, I ssh to a SunOs machine where I use
34
3217
by: BQ | last post by:
Hello Is there a way to declare 'FUNCT' via a define so that if its parameter x, a constant, is greater than 35, it returns 56, if not, 20. I would like that at compile time, not at run time. So: #define FUNCT(x) x>35?56:20
7
7011
by: Peng Yu | last post by:
I'm not very clear about when to use #define and when to use const? #define number 4 const int number = 4; If I just want to define a global constant, which way of the above is better? Thanks, Peng
8
29208
by: Joe HM | last post by:
Hello - I would like to find a way to use a pre-processor directive like the #define in C++. It seems like the #const in VB can only be used within other # statements but not outside. The problem is that I want to set a variable at the very top of the *.vb file so users will see it and can adjust it if necessary. Then I want to use the variable in a function call later in the code.
4
2782
by: yancheng.cheok | last post by:
Recently, I try to replace #define with const in header file. However, there are concerns on, multiple const object will be created, if the header file is included in multiple cpp files. For example: In version.h ------------ #ifndef VERSION #define VERSION
4
2787
by: venkat | last post by:
I have come across some preprossor statements, such as #define PPTR_int #define PPTR_str #define DDAR_baddr & #define DDAR_caddr & What do they mean, but when i compile the code with these i am not getting any errors .
5
3235
by: MoslyChang | last post by:
Hi, All When I look at effective c++,item2 and item3. I have some basic questions , Does anyone be familar with this topic? it suggests const is perfer to #define, then I think how to replace #define with const. example: 2 header file StringGrid1.h StringGrid2.h
0
9716
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10354
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9177
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7643
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6870
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4314
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3005
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.