Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to use const variables in the definitions of other constants?

Tags:

c

constants

I would like to use some previously defined constants in the definition of a new constant, but my C compiler doesn't like it:

const int a = 1;
const int b = 2;
const int c = a;         // error: initializer element is not constant
const int sum = (a + b); // error: initializer element is not constant

Is there a way to define a constant using the values of other constants? If not, what is the reason for this behavior?

like image 619
e.James Avatar asked Dec 03 '22 08:12

e.James


1 Answers

Const vars can't be defined as an expression.

#define A (1)
#define B (2)
#define C (A + B)

const int a = A;
const int b = B;
const int c = C;
like image 55
phillc Avatar answered Dec 04 '22 20:12

phillc