Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics 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.

What is the difference between #define n 100 and int n=100 ?

Status
Not open for further replies.

fatma1000

Banned
Full Member level 2
Joined
Apr 30, 2006
Messages
147
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Location
eygpt
Activity points
0
help2 in c++

what is the difference between

#define N 100
double H[N];

and
int N=100;
double H[N];
 

help2 in c++

hi
secound code does not works
cuz when u declear an static array compiler should knows the dimention of that and in this condition the value of N is unknown in compile time until run time.

but in first code u use from precompiler expresion #define that is same as u write int H[100];

if u want to create an array with dynamic dimention use from new and delete operations.


int *H=NULL;
int N=100;
H=(int*) new int[N];
.
.
.
use from H like an array and when u dont need it any more. u should free memory of it:

delete[] H;
H=NULL;
 

help2 in c++

Hi,

int N=100;
double H[N];

this will not work, however

const int N=100;
double H[N];

this will work and the difference between

1)
#define N 100
double H[N];

and

2)
const int N=100;
double H[N];

in case 1, the compiler simply replace the N with the number defined and compiles the code, this is done during the precompilation phase of the compiler

in case 2, the compiler compiles the code as it is and i would recommend this because the code is more type safe this way.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top