C Notes

 

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;

 

 

 


Preprocessor directives

 

/* 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! */

 


SCOPE

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:

  1. #include<stdio.h>
  2.  
  3. int a;
  4. static int b;
  5.  
  6. main()
  7. {
  8. ....
  9. }
  10.  

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.

  1. int a;
  2. static int b;
  3.  
  4. int func(void) {
  5.    int c;
  6.    static int d;
  7.    ...
  8. }

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:

  1. extern void MyFunction(int arg)
  2. {
  3.     /* etc... */
  4. }
  5.  

The function prototype is:

  1. extern void MyFunction(int arg);

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:

  1. static void MyFunction(int arg)
  2. {
  3.     /* etc... */
  4. }
  5.  

The function prototype is:

  1. static void MyFunction(int arg);

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.

  1. extern int value;

This variable is accessible from another compilation unit by:

  1. extern int value;

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.


An object has a storage duration that determines its lifetime. There are three storage durations: static, automatic, and allocated. Allocated storage is described in 7.20.3 [Memory management functions: calloc, free, malloc, realloc].

The lifetime of an object is the portion of program execution during which storage is guaranteed to be reserved for it. An object exists, has a constant address, and retains its last-stored value throughout its lifetime. If an object is referred to outside of its lifetime, the behavior is undefined. The value of a pointer becomes indeterminate when the object it points to reaches the end of its lifetime.

An object whose identifier is declared with external or internal linkage, or with the storage-class specifier static has static storage duration. Its lifetime is the entire execution of the program and its stored value is initialized only once, prior to program startup.

An object whose identifier is declared with no linkage and without the storage-class specifier static has automatic storage duration.

For such an object that does not have a variable length array type, its lifetime extends from entry into the block with which it is associated until execution of that block ends in any way. (Entering an enclosed block or calling a function suspends, but does not end, execution of the current block.) If the block is entered recursively, a new instance of the object is created each time. The initial value of the object is indeterminate. If an initialization is specified for the object, it is performed each time the declaration is reached in the execution of the block; otherwise, the value becomes indeterminate each time the declaration is reached.

For such an object that does have a variable length array type, its lifetime extends from the declaration of the object until execution of the program leaves the scope of the declaration. If the scope is entered recursively, a new instance of the object is created each time. The initial value of the object is indeterminate.