What to use: const or #define ?

Status
Not open for further replies.

john2020

Full Member level 5
Joined
Nov 13, 2005
Messages
292
Helped
12
Reputation
24
Reaction score
8
Trophy points
1,298
Activity points
4,911
const vs define

Hi ,
I would like to know why is const used can we use #define LPT1 0x378 instead ??
If so which one is better why?

const int LPT1 = 0x378;
const int CONTROL = 0x37A;
const int NUM_BITS = 8;
const int CERO = 0;
const int onfiltered= 1;
const int TIME_DELAY = 0;

the program is given below....
// PC Display Driver
Max7219
//
// Signal DB25 pin Signal
IC pin
// D0................. 2
CLOCK............ 13
// D1................. 3
DATA............. 01
// D2................. 4 /DATA
ENEABLE.... 12

#include
#include
#include
#include
#include
#include
#include
#include "MAX7219.H"

const int LPT1 = 0x378;
const int CONTROL = 0x37A;
const int NUM_BITS = 8;
const int CERO = 0;
const int onfiltered= 1;
const int TIME_DELAY = 0;

const char REG_DECODE = 0x09;
const char REG_INTENSITY = 0x0A;
const char REG_SCAN_LIMIT= 0x0B;
const char REG_SHUTDOWN = 0x0C;
const char REG_DISPLAY_TEST= 0x0F;

const char INTENSITY_MIN = 0x00;
const char INTENSITY_MAX = 0x0F;



thanks & regards
john
 

const vs #define

const = constant
#define = macro

When you use const, it will be stored as like as variable. The only difference between the const and variable, const cannot be on the left side - cannot change its value.

When you use #define, the precompiler will replace all LPT1 to 0x378.

In this case the function(LPT1); is in the source, then the precompiler will replace it to function(0x378) , so the compiler will get a numeric value(literal), not a variable.
 

eng_ahmed22

Salam

see this link. it describes the constants in C. and the defference between #define and const:
**broken link removed**
hope that help u
Ahmed
www.i-g.org
 

difference between const and #define

Nice link eng_ahmed22. Is there any link for embedded c tutorial. Please provide me if you know.
 

difference between #define const char *

john2020 said:
Hi ,
I would like to know why is const used can we use #define LPT1 0x378 instead ??
If so which one is better why?

john

hi,

for defining constants there are a few ways:
- using macro #define
- using constant variable const
- using enum

pros and cons of each method is well discussed here:
**broken link removed**
 

Re: const Vs #define

Code Warrior said:
Nice link eng_ahmed22. Is there any link for embedded c tutorial. Please provide me if you know.
hi code worrior
this is a good book, it is good for beginners,
it depend on the 8051 family microcontrollers. and it describes the Keil C compiler www.keil.com it full by exapmles.(start from blinking LED to creat your own operating system). it called Embedded C you can download it from the next link.

good luck
Salam

Ahmed
www.i-g.org
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…