Compiler: MPLABX IDE V5.30 Operating system: Windows 10
What i'm trying to do is define some constant values (to make future changes easier) and create some other constants via arithmetic operations during preprocessing. Then use those constants during runtime.
Here is an example version of my intentions;
#include <stdio.h>
#include <math.h>
#define foo 100 //in case you change FOO in circuit, change this too!
#define bar (sqrt(foo))
int main(void) {
if ( bar > user_input)
{
do();
}
}
The problem is, I thought, since the input was a constant value, defined things would be calculated by the compiler and bar
would be replaced by 10
instead of (sqrt(foo))
. But when i compile it, data and program size dramatically changed. When i disassembly it, there are tons of instructions instead of simply putting directly a number there.
Then i followed a suggestion from another question's answer and place a const squareroot()
function and const int
declaration but the compiler gave an alert like;
main.c:50:38: error: initializer element is not a compile-time constant
here is the second try;
#include <stdio.h>
#include <squareroot.h>
#define foo 100 //in case you change FOO in circuit, change this too!
const int bar=squareroot(foo);
int main(void) {
if ( bar > user_input)
{
do();
}
}
const int squareroot(const int input)
{
do()
}
How can i express myself to my compiler to make it understand some of the lines in my code are constant whatever happens during runtime, so that it can do the aritmetic instead of just simply passing the token/text to the function body?