We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Queries regading C coding for EMBEDDED systems.
If we define Variable of type "int constant i=10,
In Which segment it will be stored??( Code/data/stack )
Similarly for any variable declared as "static".
How it will be decided??
Could you please help me in knowing where the different types of variables in C language?
ANSI C requires all ints to have storage allocated, even if they're constant.
It doesn't. It only requires their value to be available. Only if the address of a constant is actually taken, it has to be allocated somewhere. In all other cases the general "as-if" rule means that as long as the program and its user can't tell the difference, the translator can do whatever it pleases.
Are you sure the "if address never used" rule is in C89? I know the as if appeared in C++, and got adopted into C99.
But I seem to remember this from running into it the hard way (back in the day), mostly because it surprised me, and because of the contrast with C++ (as one of the "C++ as a better C" advantages).
However, I can't seem to turn up an online C89 spec, so perhaps my memory is faulty.
Just another reason to conform to the C99 standard, already!
I'm reasonably sure the 'as-if' rule has been in the C Standard since its first edition (ANSI C89). Without it, optimization would never have been possible. That a const object whose address is never taken doesn't have to have memory allocated is a direct consequence of 'as-if' --- without taking the address, the program can never find out if the const has an address, so the compiler gets to choose whether to give it one or not.
The difference about 'const' between C++ and C is elsewhere. C++ consts can serve as compile-time constants, just like literals or enum members. C consts are just immutable, but they can't be used to, e.g., dimension an array or bitfield.