LinuxQuestions.org
Review your favorite Linux distribution.
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 08-16-2006, 11:24 AM   #1
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Rep: Reputation: 38
static?


Im updating my bad skills in c and im confused with the static keyword.

As I understand in C++ and java static mean that no multipple instanses can be craeted only one for all. In c as I get it static means that its internal or that now one can see it from the outside of that source file. In C++ the protected or private is used.

Im I lost in this? Pretty big diffrence of the same keyword.
 
Old 08-16-2006, 11:31 AM   #2
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Original Poster
Rep: Reputation: 38
ANd by the way I do not get it why you can share variables in c between sourcefiles lik int i; in one and extern int i; in the other. Is it not better and cleaner to use functions like void
seti(int i) and
int geti()?
 
Old 08-16-2006, 11:39 AM   #3
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
The static keyword relates to the scope of the func/varaiable etc.
Quote:
Is it not better and cleaner to use functions like void
seti(int i) and int geti()?
Well you still have to make avaiable the functions which I would assume are in the same header as the int. Using get set funcs you are then incurring a function call for a global int(scope related again). As for cleaner, I would say no as it will be declared in the other files as extern which shows its scope.

http://www.cprogramming.com/tutorial/statickeyword.html

Last edited by dmail; 08-16-2006 at 11:41 AM. Reason: static link added
 
Old 08-16-2006, 11:51 AM   #4
rstewart
Member
 
Registered: Feb 2005
Location: Sunnyvale, CA
Distribution: Ubuntu
Posts: 205

Rep: Reputation: 38
Hi,

It has to do with the "scope" of the variable. In C, simply defining a variable outside of any function call gives that variable global scope. This means that it is available as is to any function that is defined with the same source file, and is also available to any function in any other source file provided that the variable is also declared in the other source file as extern. The static keyword imples that a globally defined variable (any variable defined outside the scope of a function) is only accessable to those functions defined within the same source file - the variable's scope is solely the source file. All other source files can not and will not see that variable even if the usage of the extern keyword is used. And lastly, variables that are defined within a function are only directly available within that function unless the defining function passes either the address or the contents of the variable on to another function.

In other words:

Variables defined outside of a function are defined in global memory and are accessable everywhere unless specifically instructed not to.
Variables defined inside of a function are defined on the stack, only exist as long as the function has scope, and are private to the function unless the function passes them on to another function.

I hope that this helps and does not confuse you more...
 
Old 08-16-2006, 11:55 AM   #5
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Original Poster
Rep: Reputation: 38
ok I got it now. Thanks.
 
Old 08-16-2006, 12:01 PM   #6
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Original Poster
Rep: Reputation: 38
Ok now I learnt that internal static is like C++. So static means diffrent things in c depending if its internal or external.
 
Old 08-16-2006, 12:48 PM   #7
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
Originally Posted by kalleanka
Ok now I learnt that internal static is like C++. So static means diffrent things in c depending if its internal or external.
thats correct. the static keyword is used in two different ways that are completely unrelated.

just another one of the quirks of C that we all grow to love (or is it hate?)
 
  


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
Howto Assign Multiple Static Public IP Addresses under SBC's PPPoE Static Ip system o trekgraham Linux - Networking 8 04-17-2007 10:51 AM
Java: non-static variable this cannot be referenced from a static context chief_officer Programming 4 05-29-2006 01:37 PM
C++: difference between static method variable and static class variable? Thinking Programming 3 01-16-2006 10:08 AM
Is there any static ARP for static IP network? linux_lover2005 Linux - Networking 2 05-18-2005 12:01 PM
FC2 Overriding static if in favor of dhcp system set for static pkraus109 Linux - Networking 8 09-21-2004 11:13 AM

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

All times are GMT -5. The time now is 06:20 PM.

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