A personal repository of random information in compensation for a fatigued biological computer
Special Operators : There are many special operators like
& - Pointer Operator - Address Of Operator
* - Pointer Operator - Value at Operator
-> - Pointer Operator - Member Selection operator
. - Member Selection operator
, - Comma Operator
sizeof() - SizeOf Operator
Pointers
int x,z;
int* y;
y = &x // y will be point to x
*y = 10; // x = 10
Member selection operators
struct gridpoint {
int xdim;
int ydim;
} ;
gridpoint m;
gridpoint* p;
p = &m;
m.xdim = 20;
p->xdim = 25;
/* Lines that start with a # character are preprocessor
directives; the include directive brings the contents of
another file in at this point. If you use < ... > then
it's from a central library, and if you use " ... " it's
from your own directory */
/* If you are going to #define a negative number, put it
in brackets!!! The use of # define is before compile
time and if you don't use the brackets you could end up
with two minus signs ... oops! */
http://bytes.com/topic/c/answers/860211-global-variable-static-global-variable
I heard that all global variables are static..
If its so then what is the difference between the 2 declarations:
What is the difference between there visibility and how long they remain in memory?
----------
They are both in memory for the entire lifetime of the program. The variable that is declared static only has scope in the file in which it is declared where as the variable declared without static can be accessed from other files using an extern declaration.
----------
The meaning of the "static" keyword in C depends on whether or not it appears within a brace-delimited block. Refer to the following code snippet.
Variables 'a' and 'b' are declared outside of any brace-delimited blocks, therefore they both persist for the life of the program and are accessible from anywhere in the compilation unit. In this case, the "static" keyword has the effect of making variable 'b' local to the compilation unit. Variable 'a' can be accessed from any other compilation units that contain an extern declaration for it.
Variables 'c' and 'd' are declared within a brace-delimited block, therefore they are only accessible from within that block. In this case, the "static" keyword has the effect of making variable 'd' persist for the life of the program. Variable 'c' only persists while execution is within that brace-delimited block. There is no reason to expect variable 'c' to be stored at the same location for successive executions of the brace-delimited block.
----------
One last note. Neither variable 'c' nor 'd' are accessible from other compilation units. Any attempt to add an extern declaration for either of them to another compilation unit will result in a link error.
---------
No one has mentioned linkage so far, which is odd. Linkage comes in two flavors, external and internal. External linkage means the variable or function is accessible (sharable) from outside the compilation unit. Internal linkage means the variable/function is accessible onlt from inside the compilation unit.
So a static function cannot be called from outside the compilation unit but an external one can:
The function prototype is:
The extern is the default. External functions (aka global functions) are callable by using the function prototype.
Static functions cannot be called from outside the compilation unit:
The function prototype is:
You may place the static function prototype at the top of the file but the definition of the function must reside somewhere in the file.
The above applies to variables.
This variable is accessible from another compilation unit by:
Static variables have internal linkage and are accessible only in the current scope. If the static variable is inside a function, it is accesssible only from inside the function. Here the static keyword also causes the local variable to persist after the function completes execution. It's kind of like a local global.
---------
Global static variables are accessable from other files. The static is to make the variable retain the last assigned value. But what is given below says global static variables are not accesable from other file.
ex.
http://knol.google.com/k/vivek-bhadr...4lj4klzp0d/36#
----------
An identifier declared in different scopes or in the same scope more than once can be made to refer to the same object or function by a process called linkage. There are three kinds of linkage: external, internal, and none.
In the set of translation units and libraries that constitutes an entire program, each declaration of a particular identifier with external linkage denotes the same object or function. Within one translation unit, each declaration of an identifier with internal linkage denotes the same object or function. Each declaration of an identifier with no linkage denotes a unique entity.
If the declaration of a file scope identifier for an object or a function contains the storage class specifier static, the identifier has internal linkage.
For an identifier declared with the storage-class specifier extern in a scope in which a prior declaration of that identifier is visible, if the prior declaration specifies internal or external linkage, the linkage of the identifier at the later declaration is the same as the linkage specified at the prior declaration. If no prior declaration is visible, or if the prior declaration specifies no linkage, then the identifier has external linkage.
If the declaration of an identifier for a function has no storage-class specifier, its linkage is determined exactly as if it were declared with the storage-class specifier extern. If the declaration of an identifier for an object has file scope and no storage-class specifier, its linkage is external.
The following identifiers have no linkage: an identifier declared to be anything other than an object or a function; an identifier declared to be a function parameter; a block scope identifier for an object declared without the storage-class specifier extern.
If, within a translation unit, the same identifier appears with both internal and external linkage, the behavior is undefined.