473,756 Members | 4,511 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

#define or const

What is preferable to use for constants, #defines or const identifiers. And
where is it customary to place const indentifiers in your files? thanks.
Jul 23 '05 #1
11 2559
Andy White wrote:
What is preferable to use for constants, #defines or const identifiers.
And where is it customary to place const indentifiers in your files?
thanks.


constants.
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #2


Andy White wrote:
What is preferable to use for constants, #defines or const identifiers. And
where is it customary to place const indentifiers in your files? thanks.


I would go for constants. It would help in during debugging since the
symbol table would be generated for constants. However, if you are
using #defines then the replacement is done before compilation and no
sysmbol table gets generated. Moreover, no typechecking is performed
for them.

Soo, I would suggest for constants.

Jul 23 '05 #3


Andy White wrote:
What is preferable to use for constants, #defines or const identifiers. And
where is it customary to place const indentifiers in your files? thanks.


C++ recommends the usage of constants and place them on the header
file.

http://www-inolab.sys.es.osaka-u.ac...._better_c.html

Jul 23 '05 #4
> What is preferable to use for constants, #defines or const identifiers. And
where is it customary to place const indentifiers in your files? thanks.


http://www.parashift.com/c++-faq-lit....html#faq-29.7
Jonathan

Jul 23 '05 #5
"Andy White" <br***********@ msn.com> wrote in message
news:Uv******** *********@fe06. lga...
What is preferable to use for constants, #defines or const identifiers.
As others have said, constants.
And where is it customary to place const indentifiers in your files?
thanks.


A clarification to what some of the others said: don't blindly put the
constants in header files. Like any other name, put them to the innermost
scope possible. Only when a constant is used at more than one place, move it
to an outer scope as needed.

Ali

Jul 23 '05 #6
Andy White wrote:
What is preferable to use for constants, #defines or const identifiers. And
where is it customary to place const indentifiers in your files? thanks.


Though const identifier is much better in many ways like debugging, you
can pass them by reference and more, in some scenarios where memory is
also a constraint, #define can be used, as const may take some memory
in address space.

-Wg-

Jul 23 '05 #7
In article <11************ ********@g47g20 00cwa.googlegro ups.com>,
WittyGuy <wi**********@g mail.com> writes
Andy White wrote:
What is preferable to use for constants, #defines or const identifiers. And
where is it customary to place const indentifiers in your files? thanks.


Though const identifier is much better in many ways like debugging, you
can pass them by reference and more, in some scenarios where memory is
also a constraint, #define can be used, as const may take some memory
in address space.


In practice they only take memory when you do something that involves
the value having an identity such as taking its address or passing it be
reference.

An additional consideration is whether you are using C or C++ as the
semantics of const are different (in particular, const in C++ creates a
compile time constant but this is not the case in C)

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
Jul 23 '05 #8

"Francis Glassborow" <fr*****@robint on.demon.co.uk> wrote in message
news:So******** ******@robinton .demon.co.uk...

[snip]

(in particular, const in C++ creates a
| compile time constant but this is not the case in C)

True, but even that can have it's quirks:

class Array
{
private:
const int NrElems;
int MyArray[ NrElems ];
public:
Array( int n ) : NrElems( n ) {}
};

[C++ Error] ConstTest.cpp(8 ): E2313 Constant expression required

Cheers,
Chris Val
Jul 27 '05 #9
[cross-posting removed...]

On Wed, 13 Jul 2005 00:17:03 -0700, Ali Çehreli <ac******@yahoo .com>
wrote:
A clarification to what some of the others said: don't blindly put the
constants in header files. Like any other name, put them to the innermost
scope possible. Only when a constant is used at more than one place, move it
to an outer scope as needed.


For constants needed in more than one source file, declare them with
the "extern" specifier in a header file, then define their values in a
separate .cpp file.

For constants needed only in one source file, you should declare them
as "static" directly in the .cpp source file.

--
Bob Hairgrove
No**********@Ho me.com
Aug 5 '05 #10

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

Similar topics

8
2557
by: John Ratliff | last post by:
Let's say I had a program which uses some constants. Would it be "better" to declare them like this: #ifndef __MY_HDR_FILE #define __MY_HDR_FILE #define STRING_CONST "some string..." #define INT_CONSTANT 4852 #endif
4
2253
by: Rayer | last post by:
I have got some project src, and find it out that there is no ppl using "const" to define a constance, but using #define still more. Discussing with my friend, he said 1. #define is much easier in modify values in program for several propose, and 2. const takes memory but #define not. However, most modern textbook (for example, C++ Primal Plus 4/e) still suggest to use const to replace #define. I know that use #define will have some...
4
2779
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
2
7999
by: Jason | last post by:
What is the difference between 'const' and 'define()' ? When would I prefer using 'const' over 'define', or vice versa? It seems if i do: const secondsInMinute = 60; define("secondsInMinute", 60); Aren't these two the same thing? - Jason
5
3232
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
23
3916
by: anon.asdf | last post by:
Hello! In the following code-snippet, is it possible to initialize each element of arr, with STRUCT_INIT? struct mystruct { int a; char b; };
5
2574
by: alan | last post by:
Hello world, I'm wondering if it's possible to implement some sort of class/object that can perform mapping from class types to strings? I will know the class type at compile time, like so: const char *s = string_mapper<thetype>(); However I do not know the string to be associated with the type at compile time, and will need a way to set up the mapping, to be created at run time, possibly like so: void foo(char*...
6
2166
by: wongjoekmeu | last post by:
I need to rewrite some typedef to #define. For instance I rewrote typedef void* handle to #define handle void* But I saw for instance another typedef in my code which I don't understand, which is
27
5419
by: pereges | last post by:
I need to define the plancks constant 6.67 X 10 exp -34. Is it possible to do it using #define or would you suggest using a (static ?)const double.
1
5225
by: sophia | last post by:
Dear all, the following are the differences b/w #define and typedef ,which i have seen in Peter van der lindens book. is there any other difference between thes two ? The right way to think about typedef as being a complete encapsulated type - you can't add to it after you have declared it.
0
9455
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
9271
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10031
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9869
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...
1
9838
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8709
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
7242
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
5302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3354
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.