Where are const and volatile variables stored?

Hello everyone,
So I have two variables:

volatile unsigned char x = 2
const unsigned char y = 123

My question is: Where is each variable stored? Are they both stored in the SRAM or flash? I tried to research online but I didn't manage to find anything useful

Thanks

on an AVR, both will probably be stored in SRAM. On an ARM, the volatile will be stored in SRAM and the const will probably be stored in flash. This is mainly because the AVR flash is not accessible as "normal memory" to C programs, and you can't put any variables there.
For more fun, consider that "static const unsigned char z = 210;" will probably not be "stored" anywhere, but instead will be optimized in a way where it will be incorporated into expressions "as needed" (in a way, that's "stored in flash", but you won't be able to find it in a debugger, for example...)

westfw:
For more fun, consider that "static const unsigned char z = 210;"

https://www.google.com/search?q=static+const+vs+const

:wink:

As far as I know the only deviation occurs when ampersand is involved.

OK, so there is no difference when it comes to where the variables are stored because the Arduino follows the AVR architecture. That means that both variables will be stored in the SRAM. Is that correct?

Thanks for the quick answer

I did not think CONST values are stored anywhere - aren't they just used by the compiler as literal values wherever they are needed in the code?

...R

The answer is "it depends".

@PizzaGuy, not including a context, like which language, makes it impossible to give you a precise answer.

Assuming the two definitions are file-scope, the file is C++, and the address of y is never reference, then y is subjected to constant folding by the compiler and essentially disappears from existence. Which means it is not stored in SRAM. If y is referenced then the constant value (123) is stored in Flash as a load-constant machine instruction.