Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

define or static

Posted on 2006-07-04
6
Medium Priority
?
513 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 500 total points
ID: 17037415
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 500 total points
ID: 17037446
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
ID: 17037546
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:KurtVon
ID: 17037581
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 500 total points
ID: 17038252
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 500 total points
ID: 17038442
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 how to create, access, and change arrays in the C programming language.

824 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