LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-26-2010, 10:02 AM   #1
shariefbe
Member
 
Registered: Aug 2008
Location: Tiruchirappalli, India
Distribution: UBUNTU 11.04
Posts: 368

Rep: Reputation: 31
static and const in c


Dear friends,
can anyone tell me what is diff between
Code:
static int a=10;
const int a=10;
 
Old 05-26-2010, 10:25 AM   #2
brazilnut
Member
 
Registered: Nov 2007
Posts: 113

Rep: Reputation: 16
I'm still new to it but...

'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...
 
Old 05-26-2010, 10:59 AM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Short answer:

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:

http://c-faq.com/decl/index.html

'Hope that helps .. PSM
 
Old 05-26-2010, 11:02 AM   #4
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by brazilnut View Post
I'm still new to it but...

'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
}
 
Old 05-26-2010, 03:33 PM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
const means you can't change the value.

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.
 
Old 05-29-2010, 03:15 PM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
well, in this x will be 'remembered' on
subsequent calls, y will be 0;

Code:
void function()
{
    static int x =0;
    int y = 0;

    printf("x=%d y=%d\n", x++, y++);
}
one generally doesn't use static too often.
 
Old 05-29-2010, 03:19 PM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by JohnGraham View Post
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 ).
...
AFAIK C99 supports 'const'.
 
Old 05-30-2010, 12:53 AM   #8
ArthurSittler
Member
 
Registered: Jul 2008
Distribution: Slackware
Posts: 124

Rep: Reputation: 31
storage class specifiers

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.
 
Old 05-30-2010, 01:24 AM   #9
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Aren't you glad you asked ....
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linking error. static const int kerenLinuxForum Linux - Software 1 08-28-2008 07:18 AM
global static const - c++ emarri Programming 5 09-07-2006 03:11 AM
const? kalleanka Programming 9 08-22-2006 11:10 AM
const/static members in classes lucky6969b Programming 2 03-27-2006 05:43 PM
const in C++ lazyboy0001 Programming 5 04-08-2004 04:43 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration