Why does the following code throw an error?
const int a = 5;
int b[a]={1,2,3,4,5};
And also when I tried to compile the above code without "const" keyword, I got the same error:
int a = 5;
int b[a]={1,2,3,4,5};
why is it so?
What is the mistake that I am doing here?
And also another question:
When are constants replaced with their actual values in a code, i.e if I declare a variable say: const int x= 5;
I know that no memory is allocated in RAM for the variable x, but constant variable area in ROM holds the value 5 and that x is simply replaced by the value 5 everywhere x appears in the code. But when does this happen? Compilation time? Boot up time? Preprocessing time?
PS: I am talking about Embedded C (running on a Microcontroller etc), not C running on my desktop. So the embedded system is bound to have a ROM (Flash, EEPROM...). What would happen then?
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…