Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Static const and const variable (http://www.go4expert.com/forums/static-const-const-variable-t5559/)

biswajit.dash 31Jul2007 13:34

Static const and const variable
 
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

shabbir 31Jul2007 14:21

Re: Static const and const variable
 
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.

biswajit.dash 31Jul2007 15:30

Re: Static const and const variable
 
Hi shabbir,

I m using C , not C++.
I have declared them above main( ) in my .c file.

DaWei 31Jul2007 15:54

Re: Static const and const variable
 
Are you writing to C99, and does your compiler support that? C89 does not support the "const" keyword.

biswajit.dash 31Jul2007 15:57

Re: Static const and const variable
 
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

shabbir 31Jul2007 16:03

Re: Static const and const variable
 
Both are same as far as usage is concerned but the static variables are allocated in heap where as the other one is not.

biswajit.dash 31Jul2007 16:09

Re: Static const and const variable
 
but as far as i know static varibales are kept in DATA area of the memory segment.

DaWei 31Jul2007 17:47

Re: Static const and const variable
 
"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.

rgupta24 31Jul2007 23:11

Re: Static const and const variable
 
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


All times are GMT +5.5. The time now is 10:46.