Const Vs Macro

A

arut

I would like to know when a const should be used and when a #define is necessary
in a program using a constant. What are the pros and cons?
 
R

Richard Delorme

arut a écrit :
I would like to know when a const should be used and when a #define is
necessary in a program using a constant. What are the pros and cons?

'const' means that an object is read-only, not that its value is constant.
Macro are mostly here to make typing easier. In C, a number written with
digits (and optionnaly a type specifier) is called a constant.

for example :

- 3.1415926 is a constant with type double and the value 3.1415926.

- #define M_PI 3.1415926

allows you to type only M_PI instead of 3.1415926 in your code.

- On the other hand :

const double K_PI = 3.1415926;

creates a variable that is read-only. If you use K_PI in your code, you are
using a variable with the value 3.1415926, not directly the value
3.1415926.

In place where the C language need a constant, you can use a macro that will
be replaced by a constant, but you cannot use a const variable :

const int K_SIZE = 10;
#define M_SIZE 10
enum {E_SIZE = 10};
int array1[K_SIZE]; /* wrong, K_SIZE is read-only but not a constant */
int array2[M_SIZE]; /* ok, M_SIZE will be replaced by the constant 10 */
int array3[E_SIZE]; /* ok, enumerations are constant too */

IMHO, using const as much as you can and avoiding macros is a good
programming practice. const help to document the code and can make the code
more robust. macros often make the code more obfuscated.
 
V

Vivek

Richard Delorme said:
arut a écrit :
I would like to know when a const should be used and when a #define is
necessary in a program using a constant. What are the pros and cons?

'const' means that an object is read-only, not that its value is constant.
Macro are mostly here to make typing easier. In C, a number written with
digits (and optionnaly a type specifier) is called a constant.

for example :

- 3.1415926 is a constant with type double and the value 3.1415926.

- #define M_PI 3.1415926

allows you to type only M_PI instead of 3.1415926 in your code.

- On the other hand :

const double K_PI = 3.1415926;

creates a variable that is read-only. If you use K_PI in your code, you are
using a variable with the value 3.1415926, not directly the value
3.1415926.

In place where the C language need a constant, you can use a macro that will
be replaced by a constant, but you cannot use a const variable :

const int K_SIZE = 10;
#define M_SIZE 10
enum {E_SIZE = 10};
int array1[K_SIZE]; /* wrong, K_SIZE is read-only but not a constant */
int array2[M_SIZE]; /* ok, M_SIZE will be replaced by the constant 10 */
int array3[E_SIZE]; /* ok, enumerations are constant too */

IMHO, using const as much as you can and avoiding macros is a good
programming practice. const help to document the code and can make the code
more robust. macros often make the code more obfuscated.

"const help to document the code and can make the code more robust. macros
often make the code more obfuscated."
Could you explain the statement above in more detail.......
And other places where you cannot you const.
 
M

Mark Haigh

arut said:
I would like to know when a const should be used and when a #define is necessary
in a program using a constant. What are the pros and cons?

In general, think of macros as a purely textual replacement. Think of a
const variable as being a read-only variable.

Because of the textual nature of preprocessing, macros are usually
considered less type-safe, beause type information is often not included
in the #define. You could try to get around this by casting the value
within the macro itself, but this is usually considered bad form because
it circumvents the C type system. Using type suffixes for numerical
values alleviates this problem, for the most part (ie 123UL notifies the
compiler that the type of 123 is unsigned long).

Scoping rules are also different. Macros have file scope, where const
variables have scope that corresponds to where they are declared.

There are several rules of thumb I use when deciding which to use.
There aren't that many hard and fast rules; for the most part it's all a
matter of degree.

1. If the scope that the 'constant' is used in is small or isolated,
prefer a const variable allocated in automatic storage (ie stack). It
will most likely be optimized away by your compiler.

2. If the constant will be used to specify the size for an array in
automatic storage, choose a macro (until I can recommend C99 VLA's in
good concience).

3. If you need a constant across a bunch of functions or files, use a
macro. Using a const variable with static storage (even if it's
declared static) will result in a permanent static storage location
being allocated for it.

4. If your expression is complicated, try to break it up into smaller
chunks of const variables that are initialized via macros:

/* Example of breaking down a complex calculation */
void do_calculation(float circle_radius)
{
/* Do some kind of complex calculation: */
const float area = PI * (circle_radius * circle_radius);
const float foo = (area * FROB_CONSTANT) % FLUX_DIVISOR;
/* Now do something with it... */
}


Now for the rules just about everybody agrees on. These are mandatory!

1. Always use all caps for macro names.
2. Always use underscores to seperate words in macro names.
3. Always use type suffixes for numerical constants.


I think I hit about everything. Did I leave anything out?

Mark


Mark Haigh
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top