• C

define or static

Hi,
how to choose between define and static and const for a constant ?
Thank You
LVL 9
matthew016Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
KurtVonConnect With a Mentor Commented:
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
 
manish_regmiConnect With a Mentor Commented:
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
 
manish_regmiCommented:
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 Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
KurtVonCommented:
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
 
bpmurrayConnect With a Mentor Commented:
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
 
cupConnect With a Mentor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.