CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

#define vs. const

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
bwhiten



Joined: 26 Nov 2003
Posts: 151
Location: Grayson, GA

View user's profile Send private message

#define vs. const
PostPosted: Thu Mar 20, 2008 2:29 pm     Reply with quote

I have observed other programmers code using both methods to instantiate a constant value to a name:

#define Float_err 0.0001

const float Float_err = 0.0001;

I cannot determine from looking at the manual for #define if the defined takes on the type of the definition or some other type.
Any pros or cons to these two methods?
Ttelmah
Guest







PostPosted: Thu Mar 20, 2008 3:57 pm     Reply with quote

They are very different.
The 'const' declaration, declares a value that is stored in the ROM, which can be accessed just like a variable, but can't change.
The 'define', is a _preprocessor_ directive. The actual number defines, is effectively typed into every location where the name is used.
Both allow a single definition to provide data to numerous locations. The 'const' declaration, will generally use less space, if the value is used in a lot of locations (there is a significant overhead to defining the const, and retrieving it). The #define, will generally give the faster result, and smaller code, if the value is only used in a few locations.
Generally, the 'const' declaration, will be favoured by programmers who also work with other languages (where this is the more common form), and is in some quarters, considered 'better practice', but in C, the #define form is probably the more common.
The #define can be very useful in other ways. The macro language it actually invokes is quite powerful, and can do a lot more than just define numbers.

Best Wishes
Pret



Joined: 18 Jul 2006
Posts: 92
Location: Iasi, Romania

View user's profile Send private message

PostPosted: Fri Mar 21, 2008 7:04 am     Reply with quote

Still there are some interesting things that you could do with const. I mention the fact that is not ansi C standard, but most CCS compiler specifics.

First of all... not all const values are not stored in ROM. Example:
Code:
const int8 Hello = 2;
....
int8 x;
....
x = Hello;
and:
Code:
const int8 array[] = {1,2,3,4};
....
x = array[1];
In both cases, compiler replaces the value directly inline, as it was x = 2;. You may say that this situation is exactly as a regular #define.

But if you use something like this:
Code:
i = 1;
x = array[i];
here you'll have all array vector stored in ROM and you'll have overhead for this. Use this method only if you have enough amount of data to make it worth.

Also, there are some other interesting things you may do with consts, like:
Code:
const int8 EventCount = 0;

// put this macro in .h file

#define EVENT_DEFINE(Name)                   \
  const int8 Event_##Name = EventCount;      \
  const int8 EventCntAux = EventCount;       \
  #undef EventCount                          \
  const int8 EventCount = (EventCntAux + 1); \
  #undef EventCntAux

// and use later in other file

EVENT_DEFINE(TcpTask)
EVENT_DEFINE(ScriptTask)
EVENT_DEFINE(TcpRead)
EVENT_DEFINE(TcpWrite)
EVENT_DEFINE(PppOk)
EVENT_DEFINE(PppFail)

// after that you'll have the EventCount representing the number of
// EVENT_DEFINE that you used. Use this value to create a vector of it like (.c file):

int8 EventList[EventCount];

// and some routines
void EventSet(int8 index)
{
   EventList[index] = 1;
}
Later you can simply call:
Code:
EventSet(Event_TcpTask);
So, you do not need to increase manually EventCount each time you add an event. This will be done automatically. Note that EventCount or any Event_XXX will not be stored in ROM, they are interpreted by the precompiler. This cannot be done with #define(s) and i don't think it would work on other compilers like C18...
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group