Arindam Biswas wrote:
> const int a = 10; // global
> my_function()
> {
> const int b = 20; // local
> }
>
> Will both 'a' and 'b' be stored in data segment?
I suppose it depends on the compiler. K&R say that the const keyword may even
be ignored by a compiler! This sort of thing is very implementation-dependent
and therefore non-portable. You shouldn't rely on it.
If your aim is to ensure that the values of a and b can never be changed, you
could do this:
#include <stdio.h>
#define a 10
void
my_function(void)
{
#define b 20
printf("b = %i\n", b);
return;
#undef b
}
int main(void)
{
printf("a = %i\n", a);
my_function();
/* This would give an error:
printf("b = %i\n", b);
*/
return 0;
}
The #undef b tells the preprocessor to forget that b was ever defined.
Therefore it's effectively local to my_function(). It's OK to put it after the
return statement if you think about it.
David
[Non-text portions of this message have been removed]