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;...
- 05-26-2010 #1Linux 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...
- 05-26-2010 #2
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 encyclopediaMake mine Arch Linux
- 05-26-2010 #3Linux 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...
- 05-26-2010 #4
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:
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) { return PI * radius * radius; }
You also frequently see something like:Code:const int PI = 3.14; int circle_area(double radius) { PI = 3.15; /* will not compile */ return PI * radius * radius; }
Here the paramter is declared as "const" to ensure that it is always the same value that was passed in.Code:void foo(const int bar) { printf("You gave me the value %d\n", bar); }
For pointers, const is a bit different:
This is obviously allowed. This, however, is not:Code: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 = malloc(4); a[0] = 'f'; a[1] = 'o'; a[2] = 'o'; a[3] = '\0';
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 *a = "hello"; a = "goodbye";
Now a is a constant pointer to constant characters, and a cannot be modified.Code:const char * const a = "hello"; a = "goodbye"; /* will not compile! */
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:
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()).Code:void bar() { 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
- 05-30-2010 #5Linux Guru
- 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!


Reply With Quote