C – Storage Classes

What are storage classes in ‘C’ language?

  • automatic
  • static

How many storage class specifiers in “C” language?

  • auto, register, static and extern are the storage class specifiers in “C

 

How many variables scopes are there in “C” language?

  • body, function, program and file scope in “C”.

Storage class

  • Storage classes only decides scope, visibility and lifetime of that variable. Storage classes tells where will be the variable to stored. What is the default initial value of the variable
  • Scope : The scope of a variable refers extent to which different part of programs have access to the variable,(Where the variable is visible).
  • Lifetime: Lifetime means how long the variable persist in memory (or) when the variable’s storage is allocated and deallocated, scope also affects a variable’s lifetime.
  • variable types(Global variable)
    • Global variable. (outside of the function, include main() is called it)
      • life time : only destroy when program terminates
      • scope : only the programs
    • local variable. (inside the function
      • life time : when enters inside the function its allocated the memory, and destroy when goes to out of function.
      • scope : only that inside the function.
  • syntax:
    • storage_class_specifier data_type variable_name;
  • Specifiers : auto
    • Lifetime : Block (or) inside function (or) within function
    • Scope : Block
    • Default initialize : Uninitialized
  • Specifiers : register
    • Lifetime : Block (stack or CPU register)
    • Scope : Block
    • Default initialize : Uninitialized
  • Specifiers : static
    • Lifetime : Program
    • Scope : Block or compilation unit
    • Default initialize : Zero
  • Specifiers : extern
    • Lifetime : Program
    • Scope : Block or compilation unit
    • Default initialize : Zero
  • Specifiers : (none)1
    • Lifetime : Dynamic (heap)
    • Scope : nil
    • Default initialize : Uninitialized

auto keyword

  • A variable declare in the inside a function without any storage class specification or specified auto called auto variable.
  • Auto can only be used within functions
  • They are crated when a function is called and are destroyed automatically when the functions exits.
  • By defalt they are assigned garbage value by the compiler.
  • local variable (All the local variables are stored in a memory called as stack memory)
  • By default every local variable of the function is auto storage class.
  • Example

 

Global variable

  • A variable declared outside any function is a global variable.
  • its value can be changed by any function in the program.
  • initializing
    • int
    • char\0
    • float
    • double -
    • pointernull

register keyword

Used to define local variables that should be stored in a register instead of RAM memory (eg :Quick access such as counters). register variable has faster access than a normal variable. frequently used variables are kept in register. only few variable can be placed inside register. . The keyword register hints to compiler that a given variable can be put in a register. It’s compiler’s choice to put it in a register or not. Generally, compilers themselves do optimizations and put the variables in register.

Register Note 1 :

  • accessing address of a register is invalid : Note : if we try to take “Register’s” Address its gives Compile error, we cannot take the address of a register variable. See below Example.

 

Register Note 2

  • 2 register keyword can be used with pointer variables. Obviously, a register can have address of a memory location. There would not be any problem with the below program. See Example.

 

Resistor Note 3:

  • Register is a storage class, and C doesn’t allow multiple storage class specifiers for a variable. So, register can not be used with static .

 

Register Note 4:

  • There is no limit on number of register variables in a C program, but the point is compiler may put some variables in register and some not.

 


static keyword

  • static means its take only single or same memory.
  • static is initialized only once and remains into existence till the end of program
  • static assigned 0 (zero) as default value by the compiler.
  • A static local variables retains its value between the function call and the default value is 0.
  • If a global variable is static then its visibility is limited to the same source code.
  • Static variables stored in Heap(Not in Stack memory)
  • The following function will print 1 2 3 if called thrice.
  • Example

 

  • static int a[20];  here variable a is declared as an integer type and static. If a variable is declared as static and it will be automatically initialized to value '0' (zero).

What is a static function?

  • A function’s definition prefixed with static keyword is called as a static function.
  • You would make a function static if it should be called only within the same source code (or) that are only visible to other functions in the same file
  • static has different meanings in in different contexts.
  • When specified on a function declaration, it makes the function local to the file.
  • When specified with a variable inside a function, it allows the vairable to retain its value between calls to the function

storage class specifier

  • typedeftypedef is the storage class specifier.
    • its does not reserve the storage.
    • It is used to alias the existing type. Also used to simplify the complex declaration of the type.
  • reference links

volatile

  • Objects declared as volatile are omitted from optimization because their values can be changed by code outside the scope of current code at any time


 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

if you have any queries call us +918300026060 or WhatsApp Dismiss

0

Your Cart

%d bloggers like this: