1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Static const and const variable

Discussion in 'C' started by biswajit.dash, Jul 31, 2007.

  1. biswajit.dash

    biswajit.dash New Member

    Joined:
    Jul 31, 2007
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
    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.
     
  3. biswajit.dash

    biswajit.dash New Member

    Joined:
    Jul 31, 2007
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Hi shabbir,

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

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    Are you writing to C99, and does your compiler support that? C89 does not support the "const" keyword.
     
  5. biswajit.dash

    biswajit.dash New Member

    Joined:
    Jul 31, 2007
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
    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: Jul 31, 2007
  7. biswajit.dash

    biswajit.dash New Member

    Joined:
    Jul 31, 2007
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    but as far as i know static varibales are kept in DATA area of the memory segment.
     
  8. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    "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.
     
  9. rgupta24

    rgupta24 New Member

    Joined:
    Jul 31, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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
     

Share This Page