Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

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
 

NickE

Member level 1
Joined
Aug 14, 2005
Messages
36
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,286
Activity points
1,502
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.
 

Code Warrior

Full Member level 4
Joined
Dec 30, 2004
Messages
230
Helped
10
Reputation
20
Reaction score
2
Trophy points
1,298
Activity points
1,831
difference between const and #define

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

banh

Advanced Member level 1
Joined
Dec 16, 2004
Messages
458
Helped
17
Reputation
34
Reaction score
5
Trophy points
1,298
Activity points
3,939
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:
http://www.embedded.com/showArticle.jhtml?articleID=9900352
 

eng_ahmed22

Full Member level 4
Joined
Jun 7, 2005
Messages
190
Helped
20
Reputation
40
Reaction score
5
Trophy points
1,298
Activity points
2,916
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.
https://www.edaboard.com/viewtopic.php?t=67899&highlight=embedded+c
good luck
Salam

Ahmed
www.i-g.org
 

Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top