ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
'const' is used to prevent the variable from being modified. Yes they can be declared and then set elsewhere once.
'static' variables in a class are shared amongst all instances of the class. Static functions allow the function to be called without the class itself and can also be used for function pointers within a class.
err...
I'd also like to see others experience on this subject too...
They're two completely different things. "Static" generally means that the value persists. "Const" generally means that the value cannot be changed. "Static" was a keyword in the original C language; "const" was adopted from C++.
Longer answer: each means different things in different contexts.
Please look at the examples for "const" and for "static" in this FAQ:
'const' is used to prevent the variable from being modified. Yes they can be declared and then set elsewhere once.
'static' variables in a class are shared amongst all instances of the class. Static functions allow the function to be called without the class itself and can also be used for function pointers within a class.
err...
I'd also like to see others experience on this subject too...
It seems like you're answering the question in C++, not in C, which is what the OP's asked for (unless he's mistyped the subject ).
'const' and 'static' mean very different things.
A variable declared 'const' cannot be used to change its contents. This isn't the same as saying a const variable cannot be changed, just that you cannot use that variable to change it, as you can use a pointer to non-const (though any decent compiler will give you a warning). For example:
Code:
#include <stdio.h>
int main(void) {
const int x = 0;
printf("%d\n", x);
int *xp = &x; // pointer-to-non-const can change the
// const it points to
(*xp)++;
printf("%d\n", x);
// x++; // this is not allowed
}
static can mean different things in different contexts, but usually does a similar thing. For example a static variable declared in a function will remember its value from last time that function returned, so basically it's a global variable in the sense that it exists through the lifetime of the program, and local in the sense that it can only be accessed within the scope it was declared.
C recognizes "storage classes" which suggest to the compiler where to store variables. Standard K & R 2ed C recognizes storage-class specifiers
auto
register
static
extern
Variables declared with register storage class are expected to be used very frequently in the function. The compiler is encouraged to assign these variables directly to a register in the CPU. Since they are not expected to be stored in main memory, one cannot use the unary prefix address operator & on register variables.
Auto is the default storage class for variables declared within functions. The compiler typically allocates storage for auto variables in the stack frame (also known as the activation record) each time the function is called. Since the stack is in memory, one can take the address of the variable. But think carefully about what you do with that address, because after you return from the function, its stack frame will be popped from the stack and the stack space will be used for some other purpose. The address of an auto variable becomes invalid when the function exits. You can pass the address of an auto variable to a function, because the called function will not use the address after it returns to the calling function which passed the address of the auto variable. All addresses of the calling function's auto variables will remain valid while the called function is using them. No function should return the address of an auto variable to a calling function.
A variable declared with storage class extern implies storage for the variable is defined elsewhere. In order for a variable to be visible to satisfy the definition of the extern variable, it must be declared at global scope in one of the files linked with the file with the extern variable declaration. This creates a hazard that either the variable may be defined nowhere or that it may be defined more than once. I avoid this problem by declaring the variable in main() or in the outermost function where it is needed and pass the variable or a pointer to the variable as a parameter to any function that needs it.
As paulsm4 stated, the values of static variables declared in a function persist between calls to the enclosing function. Storage is typically allocated in the "data segment", not in the activation record on the stack. One may always take the address of a static variable, and the address of a static variable is always valid. If a static variable is initialized in its declaration, it is initialized only once, effectively at compile time or when the program is loaded from disk into memory. This contrasts with auto variables, which, if they are initialized in declaration, are initialized every time the function is entered. You may change the value of a static variable. The next time you examine that variable, it will contain the updated value, not the initialization value. Static variables contrast with dynamic variables, which are also persistent, but are created (e.g., by calling malloc() ) and possibly destroyed (by calling free() ) while the program is running.
Const and volatile are type qualifiers rather than storage class specifiers. Both const and volatile are recognized in C as defined in K & R 2ed. One common use of const is when defining and initializing a value which should not change. Another common use is in the parameter list of a function. In a function parameter list, const implies that the function will not change the value of the parameter. The effect of attempting to change a const value is explicitly implementation dependent, but it ought to at least cause a warning if it does not cause an error.
The type qualifier volatile is a warning to the compiler that it cannot optimize out reading a value even though the program may have just written or previously read that value. This is crucial in memory-mapped I/O, but can happen in some other cases as well.
The keyword typedef is also listed as a storage class specifier, but this is only done for syntactic convenience.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.