C Foundation

What is C? C Compiler Installation C Extensions C Compiler C Interpreter C Program Structure

C Basics

C Keywords C Data Types C Identifiers C Variables C Constant C Escape Sequences C Constant and Volatile C Typecast

Operators

What is Operator C Comma Operator C Arithmetic Operators C Relational Operators C Logical Operators C Bitwise Operators C Conditional Operators C : : Operator C Operator Priority

Basic IO's

Basic IO's C Formatted Functions C Unformatted Functions C Common Functions

Control Statements

What is Control Statement C if Statement C if else Statement C Nested if Statement C Else if Statement C Break Statement C Continue Statement C Switch Statement C Goto Statement

Looping

What is Control Loop C for Loop C Nested for Loop C while Loop C Nested while Loop C do while Loop C Nested do while loop

Functions

What is Function C User Defined Functions C Recursion C Passing Parameters

Scope

Scope C Local Scope C Global Scope

Storage Classes

What is Storage Class C Auto C Extern C Static C Register

Array

What is Array C One Dimensional Array C Two Dimensional Array C Multi Dimensional Array C Arrays Of Strings

String

What is String C String Functions

Pointer

What is Pointer C Pointers Arithmetic C Pointer to Pointer C Pointers and Arrays C Pointers and Strings C Pointer to Functions Void Pointers Null Pointers C Null and Void Pointer

Structure

What is Structure C Struct within Struct C Array within Structure C Pointer to Structure C Structure and Function C Enum C Bitfield Structure C Type def

Union

What is Union

Files

What is File C read a file C write a file C File Handling C Error Handling C Low Level Disk I/O C Other file functions

Memory Allocation

What is Memory Allocation C Malloc() C Calloc() C Free() C Realloc() C Coreleft()

C Reference

All ASCII Code Basic C Questions

C Interview

C Interview Sets All Star Patterns All Number Patterns All Alphabet Patterns All Series Patterns
The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

C Storage Classes

Why Storage Class

A storage class defines the scope, visibility and life-time of variables.

Types of storge classes

There are four types of storage classes in c programming. They are

Keyword Storage Classes
auto automatic storage class
static static storage class
register register storage class
extern external storage class
C Functions

Storage class Defines Scope

We already defined that a scope of a variable is defined by its storage classes.

What is meant by scope?

A scope is defined as the area in which the declared variable is available.In c programing we can encounter five different types of scopes. They are

  • program scope
  • file scope
  • function scope
  • block scope
  • prototype scope

Program To Define Scope In Storage Classes

Let's workout a program to explain all 5 different scope in storage class.

C program - Storage Class

storageclass.c
#include <stdio.h>
void display(int num);
// here 'num' has prototype scope
static void fun();
int main()
{
int num=2;
void display(int d){
// here 'display' has program scope
printf("\n%d", d);
}
display(1);
fun();
bark:{
//here 'bark' has function scope
int i = 0;
printf("\nBow Bow");
if(i = 1){
return 0;
}
i++;
}
goto bark;
return 0;
}
static void fun(){
//here 'fun' has file scope
int i = 0;
//here 'i' has block scope
printf("\n%d", i);
i++;
}
  • 1
  • 0
  • Bow Bow

Note:

Program scope

display has a program scope because all non-static function have program scope.

File Scope

fun has a file scope because all static function can only accessed within a file.

Function Scope

bark is a label which has a function scope because there can be only one bark label within a function hence it has function scope.

Block Scope

i has a block scope because every function can have a variable with name i

Prototype Scope

num has a prototype scope because one cannot declare a variable j within the same function.

Storage class Defines Visibility

We already defined that visibility of a variable is defined by its storage class.

What is meant by Visibility?

Visibility is the way of hidding or showing a variable inside or outside a scope. Visibility is otherwise said to be accessibility.

Program To Define Visibility In Storage Classes

Let's workout a simple program to explain visibility of a variable.

storageclassvisibile.c
#include <stdio.h>
int main()
{
int i = 9;
//visible inside all function.
void display(){
//visible inside this function only.
int j = 10;
printf("%d",i);
}
display();
printf("%d",j);
return 0;
}
  • Error message will occur as a variable j is accessible only within that function.

Storage class Defines Life-time

We already defined that life-time of a variable is defined by its storage class.

What is meant by life-time?

Life-time is defined as a period of time in which the variable lives. There are three types of life-times in storage classes. They are

  • static life-time
  • automatic life-time
  • dynamic life-time

Program To Define Life-time In Storage Classes

Let's workout a simple program to explain all 3 life-time in storage class.

storageclasslifetime.c
#include <stdio.h>
int main()
{
int *numbers = (int*)malloc(4* sizeof(int));
// *number act as a automatic and dynamic as well
static int i;
// i act as a static variable
numbers[0] = 0;
numbers[1] = 1;
numbers[2] = 2;
numbers[3] = 3;
free(numbers);
printf("\nStored integers are ");
for(i = 0; i < 4; i++)
printf("\numbers[%d] = %d ", i, numbers[i]);
return 0;
}
  • Stored integers are
  • numbers[0] = 0
  • numbers[1] = 0
  • numbers[2] = 0
  • numbers[3] = 0

Note:

Static life-time

Here i has static life-time because its life-time is within a program.

Automatic life-time

*numbers has automatic life-time because its life-time starts when *number variable is allocated by malloc() inbuilt memory function.

Dynamic life-time

*numbers has dynamic life-time too because free() memory function can be used any time to clear data in allocated memory.

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report