Solved

define or static

Posted on 2006-07-04
6
462 Views
Last Modified: 2010-04-15
Hi,
how to choose between define and static and const for a constant ?
Thank You
0
Comment
Question by:matthew016
6 Comments
 
LVL 11

Accepted Solution

by:
KurtVon earned 125 total points
Comment Utility
Gnerally I use a #define if it will be used in multiple files, or exported, or used in any kind of formula.  A static const is useful if you need something that will be used univerally, but may change (static referrs to memory location, notvalue) and a const is for objects than need to be variables but will never change value, like objects or enums.

So the following questions need to apply:

Will the value change?  If so, use a static.

Is the "type" of the constant important (e.g. it must be a long or an unsigned char*)?  If so, use a const.

Otherwise, use a #define.
0
 
LVL 8

Assisted Solution

by:manish_regmi
manish_regmi earned 125 total points
Comment Utility
static is really different thing than the other two.
In a function use this if you want to retain the values in subsequent calls.
At the global scope use this to make variable only visible to that c file.

I like to use enum rather then define.

defines are resolved at compile time and do not take space. constant takes space.
It is better to use defines for masks values.

regards
Manish Regmi
0
 
LVL 8

Expert Comment

by:manish_regmi
Comment Utility
The other thing is you cannot use constant if you want to process that value in compile time.

#define VERSION 4

#if VERSION == 5
do_this();
#else
do_that();
#endif

There is a really good article on this subject on embedded.com
http://www.embedded.com/showArticle.jhtml?articleID=9900402


regards
Manish Regmi
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 11

Expert Comment

by:KurtVon
Comment Utility
A define will take space, it just takes space in the code segment instead of the data segment.  But in general, a define is preferred if you can get away with it.

A #define can also define macros which, obviously, and static or const variable cannot.  But I don't think that's what the question is about.

Oh, and if this is for homework/test preparation, all these answers are probably too vague.  Check your notes (or someone else's in the class) and find teh exact wording teh teacher used since teachers tend to look for special keywords when they hand out questions like this.  As a former teacher I know this isn't the way to really tell if someone understands the subject, but grading 100 papers the correct way is impossible.
0
 
LVL 15

Assisted Solution

by:bpmurray
bpmurray earned 125 total points
Comment Utility
A define does not take space. A define is an instruction to the preprocessor to make a textual substitution in the input source with the specified string. This is then compiled. If the define refers to a value or a variable or similar, that will take space, but otherwise, it won't, and the define itself won't take any space.

static has two meanings. In OO programming (C++) it refers to an element in a class that's shared by all instances of the class. In C it has two uses: within a function, it refers to a variable that does not lose its value between calls; within a module, it hides a global variable from anything outside the module. However, its value can be changed.

const refers to an immutable object. It should not be changed - the compiler will spot any attempt to overwrite a const value. Watch out for pointers: you can have a comst pointer that always points to a particular piece of memory which itself can be changed, or you can have a pointer to a const value.
0
 
LVL 11

Assisted Solution

by:cup
cup earned 125 total points
Comment Utility
I normally use define for constants that can be assigned to different types without casting.  If possible try not to cast as it hides errors.  If it is an incrementing sequence, I'd use an enum.

eg

static const int sone = 1;
#define DONE 1
enum EEE
{
   ezero,
   eone
};
 
int one = sone;   /* this is OK */
int one = DONE;  /* this is OK */
int one = eone;   /* this is OK on some compilers */
unsigned int uone = sone;  /* this will give a warning */
unsigned int uone = DONE; /* this is OK */
unsigned int uone = eone;  /* this is OK on some compilers */
int xx[sone];                      /* this will work on C99 but will give an error on C89 */
int xx[DONE];                     /* this is OK */
int xx[eone];                      /* this is OK but you will only get 1 element: not two */
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now