Storage Classes in C
C Programming

Storage Classes in C

 

Storage classes in a C Programming Language are classified into four main types according to the storage area and their behavior, in this c programming tutorial we use these storage classes in many programs:-

  1. Automatic storage class
  2. Static storage class
  3. Register
  4. Extern

Automatic storage class:

Automatic storage class variables in C Programming Language will be created and destroyed automatically. In the Automatic storage class, variables are stored in the stack area of the data segment or processor register.
Features:

Storage Memory
Scope Local / Block Scope
Lifetime Exists as long as Control remains in the block

Static storage class:

Static storage class variables in c language will only exist once and throughout the c program, this storage variable will be always active. Static storage class variables are stored in a static area of a data segment.
In C programming, there are four types of scopes available such as

  1. Body scope
  2. Function scope
  3. Program scope
  4. File scope

Static Storage Class:

The value of the static variable lingers until the execution of the whole c program. A static variable can be declared using a static keyword as shown below:

Syntax:

static int i;

Example:

#include <stdio.h>

void Check();   //prototype

void main()

{

Check();   //function calling

Check();  //function calling

Check();   //function calling

getch();

}

void Check(){

static int c=0;

printf(“c=%d\n”,c);

c+=10;

}

Static storage class in C programming

Register storage class:

The keyword ‘register’ in C programming language is used to define a local variable.
Syntax
register int count;

Example

#include<stdio.h>

void main()

{

int num1,num2;

register int sum;

printf(“\nEnter the Number 1 : “);

scanf(“%d”,&num1);

printf(“\nEnter the Number 2 : “);

scanf(“%d”,&num2);

sum = num1 + num2;

printf(“\nSum of Numbers : %d”,sum);

getch();

}

Register Storage class in C programming

Extern: Global variable

Example:

#include<stdio.h>

void f1();

extern int i=10; //global variable

void main()

{

clrscr();

printf(“inside main=%d”,i);

f1();

getch();

}

void f1()

{

printf(“\noutside main method=%d”,i);

}

Extern storage class in c programming

Recommended Posts

C++ Programming

Visibility modes in C++

In C++, visibility modes refer to the accessibility of class members (such as variables and functions) from different parts of a program. C++ provides three visibility modes: public, private, and protected. These modes control the access levels of class members concerning the outside world and derived classes. Public: Members declared as public are accessible from […]

Rekha Setia 
C++ Programming

Inheritance in C++

In C++, inheritance is a fundamental concept of object-oriented programming (OOP) that allows you to create a new class based on an existing class, known as the base or parent class. The new class is called the derived or child class. Inheritance facilitates code reuse and supports the creation of a hierarchy of classes. There […]

Rekha Setia 
C++ Programming

C++ Classes and Objects

In C++, classes and objects are fundamental concepts that support object-oriented programming (OOP). Here’s a brief overview of classes and objects in C++: Classes: In C++, a class is a user-defined data type that allows you to encapsulate data members and member functions into a single unit. Classes are the building blocks of object-oriented programming […]

Rekha Setia 

Leave A Comment