Static const and const variable

biswajit.dash's Avatar
Light Poster
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
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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.
0
biswajit.dash's Avatar
Light Poster
Hi shabbir,

I m using C , not C++.
I have declared them above main( ) in my .c file.
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
Are you writing to C99, and does your compiler support that? C89 does not support the "const" keyword.
0
biswajit.dash's Avatar
Light Poster
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
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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
0
biswajit.dash's Avatar
Light Poster
but as far as i know static varibales are kept in DATA area of the memory segment.
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
"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.
0
rgupta24's Avatar, Join Date: Jul 2007
Newbie Member
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