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.
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.
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()?
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.
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...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.