Hi All,
Where is an identifier with a 'const' keyword given physical space in
RAM ?
First, identifiers don't need physical space anywhere, anytime. They
are a source code, compiler, and linker thing. They do not exist at
runtime. Now if the identifier is used to name a defined object, than
that object might or might not need some space somewhere.
Who says it is in RAM at all? On some platforms it will wind up in
ROM or EPROM or some other type of physically read only memory. In
many cases, if its address is never taken and it does not have
external linkage, it will not occupy any memory at all. The compiler
will merely substitute its initial value directly into any code that
uses the value.
Is it on stack or heap or data segment depending on storage class or is
C does not define things like "stack" or "heap" or "data segment". It
defines the characteristics of an abstract machine that has "storage",
without even specifying what that is. In face, in modern
multi-tasking desktop operating systems, it is quite possible that
your const object might be living on a hard drive in a swap file while
some other program is executing and yours is suspended.
it treated as a part of code ,so kept in code segment?
Again, the C language does not specify such details, leaving them up
to the implementation. Your const object, if it occupies storage at
all, is in whatever area of storage the writers of your compiler
decided it should occupy.
What is the case with #define statements?
Macros are handled by the preprocessor, a separate phase of the
compilation process that runs before the compiler proper. It works
primarily by the equivalent of simple text substitution. The compiler
proper never sees the macro identifier, only its replacement.