Static const and const variable

Light Poster
31Jul2007,13:34   #1
biswajit.dash's Avatar
Hi,

Can anybody tell me the diffrence between a variable/array declared as <static const> and a variable declared as <const>.

Example:
=====

static const int arr1[]={1,2,3,4,5,6,7};
const int arr1[]={1,2,3,4,5,6,7};

Thanks
Biswajit
Go4Expert Founder
31Jul2007,14:21   #2
shabbir's Avatar
First I would like to know where you are declaring the static. Is in inside a function or a class member? But for some general explanation, when declared with static the variable becomes static and whose value is preserved through the program execution.
Light Poster
31Jul2007,15:30   #3
biswajit.dash's Avatar
Hi shabbir,

I m using C , not C++.
I have declared them above main( ) in my .c file.
Team Leader
31Jul2007,15:54   #4
DaWei's Avatar
Are you writing to C99, and does your compiler support that? C89 does not support the "const" keyword.
Light Poster
31Jul2007,15:57   #5
biswajit.dash's Avatar
no i m just writting that in VC++.
Both the statements are written above all function defination in my .c file.

i just want to know how compiler treats both these variables when it counters
Go4Expert Founder
31Jul2007,16:03   #6
shabbir's Avatar
Both are same as far as usage is concerned but the static variables are allocated in heap where as the other one is not.

Last edited by shabbir; 31Jul2007 at 16:04.. Reason: Typo
Light Poster
31Jul2007,16:09   #7
biswajit.dash's Avatar
but as far as i know static varibales are kept in DATA area of the memory segment.
Team Leader
31Jul2007,17:47   #8
DaWei's Avatar
"const" does not affect the static keyword. It is merely an instruction to the compiler that you should not be allowed to vary the value in your code. "Const" does not have any runtime effect. It's strictly a compile-time rule.
Newbie Member
31Jul2007,23:11   #9
rgupta24's Avatar
Y do u want to use const with static.. doesnt it defy the use of const at all.. y do u want to persist with value when it cannot be changed at all... as mentioned above by Dawei, const is just compile time variant of #define