Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

define or static

Posted on 2006-07-04
6
484 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
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 125 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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 125 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 125 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

861 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