C : PROGRAM STRUCTURE


PROGRAM STRUCTURE



STORAGE CLASSES :


  1. Every variable declared comprises of :

    1. Data type that define whether it is capable of storing integer value or character value or fractional value (float).

    2. Storage type defines the existence of variable in the memory as well as its visibility in the program.

  2. On the basis we can categorize storage class as :

    • auto
    • register
    • extern
    • static


AUTOMATIC VARIABLES :


  1. The variable that is defined inside a function is known as an automatic variable.
    We may or may not use the auto keyword before the datatype.Because compiler will automatically assume it to be auto.

  2. The scope is only inside the function in which it is declared.

  3. This is the default storage class for all the variables declared inside a function or a block.

  4. Hence, the keyword auto is rarely used while writing programs in C language.

  5. Auto variables can be only accessed within the block/function they have been declared and not outside them.

  6. They are assigned with a garbage value by default whenever they are declared.


    1. SCOPE : Local to Block where it is defined.

    2. INITIAL VALUE : GARBAGE VALUE.

    3. LIFETIME : Till the end of a function.



EXTENDED VARIABLES :


  1. Variable of this storage class are global variables.

  2. Extern storage class will have global scope and hence will be available to all the functions.

  3.   Extern variable will exist as long as program execution continues.


    1. SCOPE : AVAILABLE EVERYWHERE.

    2. INITIAL VALUE : ZERO.

    3. LIFETIME : Till the program did not finish execution.



STATIC VARIABLES :


  1. When a variable within a function is declared with the static, it instructs the compiler to keep a local variable in existence permanently.

  2. It means that the variable has a permanent duration during the lifetime of program.

  3. In other words, the memory storage allocated for the variable is not destroyed when the scope of the variable is exited.


    1. SCOPE : Local to Block where it is defined.

    2. INITIAL VALUE : ZERO.

    3. LIFETIME : Till the program did not finish execution.



REGISTER :


  1. Local variables are stored in register instead of RAM.

  2. Computer has a certain number of registers to hold data and perform arithmetic or logical calculations.

  3. Because registers are located within the CPU (Central Processing Unit) chip, it’s much quicker to access a register than a memory location which resides outside the chip.

  4. Therefore, storing variables in register might help to speed up your program.


    1. SCOPE : Local to the Function.

    2. INITIAL VALUE : GARBAGE VALUE.

    3. LIFETIME : Till the end of the FUNCTION.



Comments