Electronic – where are constant variables stored in microcontroller


As per the memory layout of C program ,constant variables are stored in the Initialized data segment of the RAM. But as per some of the Microcontroller memory layout ,const variables are stored in FLASH Memory.
I have also seen that the size of BIN file of my code increases when I change
const variables to Macro definitions? Help me clear this ambiguity. I used Nuc2240 Cortex M0

Best Answer

If you were writing assembler, you would decide which locations are used to hold which constants or variables, through org or similar directives.

When you are writing C, the C tool chain you're using will store stuff where it's been programmed to. Variables obviously tend to go in RAM, constants can go in either RAM or Flash, program tends to go in Flash.

It's all down to how the tool for that particular target processor has been designed, where it gets its instruction from where to put stuff. It might all be baked into the code, in which case you don't get any say, it goes where the tool chain's creators chose to send it. It may take directives from somewhere (source code, command line, make file, environmental variable) so that you can rebase stuff.

Read your specific documentation. It's not physics, it's all down to the whim of man.