Find the answer to your Linux question:
Results 1 to 5 of 5
Dear friends, can anyone tell me what is diff between Code: static int a=10; const int a=10;...
  1. #1
    Linux User
    Join Date
    Aug 2008
    Location
    Trichy,India
    Posts
    308

    static and const in c

    Dear friends,
    can anyone tell me what is diff between
    Code:
    static int a=10;
    const int a=10;
    Thanks in advance...

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    I justed googled C const variable and C static variable and this is the results

    Static variable - Wikipedia, the free encyclopedia

    const-correctness - Wikipedia, the free encyclopedia
    Make mine Arch Linux

  3. #3
    Linux User
    Join Date
    Aug 2008
    Location
    Trichy,India
    Posts
    308
    yes i too saw this link previously. still i have some doubt. it seems same in some condition. i want what is the exact difference between these two.
    Thanks in advance...

  4. #4
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    So just in case there's confusion, "static" and "const" do completely different things.

    Let's start with const because it's easy. const means that the value is a constant, so it cannot change. For instance:
    Code:
    const int PI = 3.14;
    
    int circle_area(double radius)
    {
        return PI * radius * radius;
    }
    We declare PI as a constant because its value will never change. This would be an error:
    Code:
    const int PI = 3.14;
    
    int circle_area(double radius)
    {
        PI = 3.15; /* will not compile */
        return PI * radius * radius;
    }
    You also frequently see something like:
    Code:
    void foo(const int bar)
    {
        printf("You gave me the value %d\n", bar);
    }
    Here the paramter is declared as "const" to ensure that it is always the same value that was passed in.

    For pointers, const is a bit different:
    Code:
    char *a = malloc(4);
    a[0] = 'f';
    a[1] = 'o';
    a[2] = 'o';
    a[3] = '\0';
    This is obviously allowed. This, however, is not:
    Code:
    const char *a = malloc(4);
    a[0] = 'f';
    a[1] = 'o';
    a[2] = 'o';
    a[3] = '\0';
    Here, "a" is a pointer to "const char"s. Therefore, the characters that a points to cannot be changed. However:
    Code:
    const char *a = "hello";
    a = "goodbye";
    This is allowed, because a is not itself constant. Only the characters that a points to are constant. Therefore, we would have to do something like this:
    Code:
    const char * const a = "hello";
    a = "goodbye"; /* will not compile! */
    Now a is a constant pointer to constant characters, and a cannot be modified.


    For "static", there's really only one reason to use static: to declare something local to a file. By default in C, anything declared in one file is visible to everything else. Therefore, if I declare a function foo() in file1.c, then in file2.c, I can write:
    Code:
    void bar()
    {
        foo();
    }
    However, if foo() was declared as "static" in file1.c, file2.c would not compile (the error would be that I never declared a function foo()).

    Technically, "static" has other meanings in C related to persistant storage of local variables in functions across function calls. However, I barely understand it myself, and I have _never_ seen it used. In object-oriented languages, "static" does have a very important meaning (it turns instance variables into class variables), but that's not in C, obviously.

    Does this help?
    DISTRO=Arch
    Registered Linux User #388732

  5. #5
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    const int a = 10;

    The variable 'a' is a constant. It cannot be written to.

    static int a = 10;

    If the declaration is in a header file, it is a local read-write variable in every translation unit (non-header source file) than includes that header either directly or indirectly. If it is declared in a source (not header) file but not in a function, then it is a local variable in that translation unit only. If it is inside a function, then it is a persistent variable in that function and will maintain its value, if changed, between calls to that function.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...