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.

[Moved] how to change avalue of macro during execution?

Status
Not open for further replies.

A.Rashad

Member level 4
Joined
Jul 27, 2011
Messages
69
Helped
6
Reputation
10
Reaction score
5
Trophy points
1,288
Location
Pune
Activity points
1,792
Is there any means to change value of a macro depending on some execution during run time ?

e.g #define VALUE A

now I want this A to get different values during code execution ,
say in main ()
A=x+y;
A can be different values based on x or y, how can I assign Value of this variable to macro VALUE ?
 

Re: how to change avalue of macro during execution?

AFAIK macros are compile time, not run time. So no, that's not possible. But you can obviously use a function or variable to take care of all your runtime needs.
 

Re: how to change avalue of macro during execution?

Maybe you should try asking in Microcontroller forums....
 

Re: how to change avalue of macro during execution?

He could ask in the gcc mailing list, answer is still going to be no. :p
 
Re: how to change avalue of macro during execution?

It will work if A is a variable. "VALUE" will be replaced with "A" early at compile time, so all references to "VALUE" will use the number stored in variable A.

Normally a macro is used to set a constant value like this:
#define VALUE 32

A macro in C is just a text substitution done at compile time, so a fixed number is not necessary.
A variable name will work:


Code C - [expand]
1
2
3
4
5
6
7
8
#define VALUE A
int A;
....
A = x + y;
if(VALUE > 123){
  printf("Above 123\n");
}
....


The compiled code will look like this:

Code C - [expand]
1
2
3
4
5
6
7
int A;
...
A = x + y;
if(A > 123){
  printf("Above 123\n");
}
....


As shown, the variable "A" can be declared after the #define since it is just a text string in the #define.
"A" must be declared before the macro VALUE is used.
 
I wonder how you guys learn HDL and also C togethor and yet live without confusion. I have not learnt C myself yet much, but still just finding it tough than learning HDL,
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top