Skip to content
  • Facebook
GeekCer Logo

GeekCer

The geek's Coding education and Review centre

  • Home
  • Tutorials
    • Java
    • Servlet
    • JSP
    • Python
    • C Tutorial
    • Spring
    • Spring Boot
    • MongoDB
    • Hibernate
    • Data Structure
  • General Knowledge
  • Biography
  • Grammar
  • Festival (त्योहार)
  • Interview
  • Differences
  • Important
  • Toggle search form

Home » C Language » Storage classes in C with examples (auto, extern, register, static)

  • Java Hibernate Framework Tutorial
    Java Hibernate Framework Tutorial | Hibernate For Beginners Hibernate
  • List Of National Animals Of Countries
    List Of National Animals Of Countries General Knowledge
  • Difference between TCP and UDP
    Difference between TCP and UDP Differences
  • Symptoms and Prevention of Coronavirus Disease (Covid-19)
    Symptoms and Prevention of Coronavirus Disease (Covid-19) Important
  • Difference between Comparable and Comparator
    Difference between Comparable and Comparator Differences
  • Yrkkh - Yeh Rishta Kya Kehlata Hai Serial, Episode, Cast
    Yrkkh – Yeh Rishta Kya Kehlata Hai Serial, Episode, Cast News
  • What is Internship? How to get Internship?
    What is Internship and how to get Internship? Important
  • Importance of Thread Synchronization in Java
    Importance of Thread Synchronization in Java Java

Storage classes in C with examples (auto, extern, register, static)

Posted on April 15, 2022April 15, 2022 By admin No Comments on Storage classes in C with examples (auto, extern, register, static)
Storage classes in C with examples (auto, extern, static, register)

When you declare a variable in C programming, you may additionally specify the variable’s lifetime or storage specification. In other words, the storage classes of variables in C specify the scope of variables, memory allocation and default value.

Variable Lifetime: The variable’s lifetime indicates how long it will be active during execution. If you define a variable inside a function, the value of the variable will be preserved until the function is run.

How many storage classes in C? In the C program, we have four separate storage classes.

  1. auto
  2. extern
  3. static
  4. register

Also Read: Basic of C Programming

Table of Contents

  • auto storage classes in c
  • extern storage classes in c
  • register storage classes in c
  • static storage classes in c

auto storage classes in c

The auto storage class in c is the default type of storage. When you use auto to declare a variable, its visibility is confined to the block or function in which it is declared.

When you declare a variable using auto, memory allocation is done for the variable when the function is called, and memory de-allocation id done after the function has completed its execution.

Because every local variable in C is auto by default if you don’t use the auto keyword, variable will be auto variable.


#include <stdio.h>

int main() {
  int intVar;
  char charVar;
  float floatVar;
  printf("Int Value: %d\nChar Value: %c\nFloat Value: %f",intVar, charVar, floatVar); 
  return 0;
}


Output:
Int Value: 0
Char Value: 
Float Value: 0.000000
Fast access is achieved by allocating registers, which is often done for loop counts. 
A global register variable cannot be declared.

extern storage classes in c

When we need to refer to a function or variable that we have defined elsewhere or will implement later, we use extern storage class.

An extern variable is just like a global variable.


#include<stdio.h>

int a = 15;
int main() {
  extern int b;
  printf("The value of a is %d", a);
  printf("\nThe value of b is %d", b);
  return 0;
}
int b = 20;


Output:
The value of a is 15
The value of b is 20

register storage classes in c

When you use register to declare a variable, that value of variable is saved in a register. If you need to access a variable several times and need it to be quick, register is the best way to go.

The time it takes to access a register is substantially shorter than it takes to reach main memory.

Because the CPU has a limited amount of registers, the allocation of variables to the registers cannot be guaranteed.

Example: A loop counter, such variable should be stored in register storage class.


#include <stdio.h>

int main() {
  register int i = 0;

  for (i = 0; i < 5; i++) {
    printf("Value of i is %d\n", i);
  }
  return 0;
}


Output:
Value of i is 0
Value of i is 1
Value of i is 2
Value of i is 3
Value of i is 4

static storage classes in c

The value of a static variable persists until the end of the program if it is declared with a static.


#include<stdio.h>

void showValue();
int main() {
  showValue();
  showValue();
  showValue();
  return 0;
}
void showValue() {
  static int x = 10;
  printf("%d ", x);
  x = x + 10;
}


Output:
10 20 30 

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Also Read

C Language Tags:What are storage classes in C with examples?, What is significance of storage class in C?

Post navigation

Previous Post: Structure in C program with example | Nested structure in C
Next Post: File Handling in C with examples | File Operations in C

More Related Articles

Structure in C program with example | Nested structure in C Structure in C program with example | Nested structure in C C Language
Constants and Operators in C Language Constants and Operators in C Language C Language
String Array in C | What is an array of string | String Functions String Array in C | What is an array of string | String Functions C Language
Programming with C Language Tutorial Programming with C Language Tutorial C Language
Statements in C Language | Types of statements in C Language Statements in C Language | Types of statements in C Language C Language
Linked List in Data Structure and Algorithm, Types, Operations Linked List in Data Structure and Algorithm, Types, Operations C Language

Related Posts

  • Structure in C program with example | Nested structure in C
    Structure in C program with example | Nested structure in C C Language
  • Constants and Operators in C Language
    Constants and Operators in C Language C Language
  • String Array in C | What is an array of string | String Functions
    String Array in C | What is an array of string | String Functions C Language
  • Programming with C Language Tutorial
    Programming with C Language Tutorial C Language
  • Statements in C Language | Types of statements in C Language
    Statements in C Language | Types of statements in C Language C Language
  • Linked List in Data Structure and Algorithm, Types, Operations
    Linked List in Data Structure and Algorithm, Types, Operations C Language

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • C Home
  • Constants and Operators
  • Statements in C Language
  • Array in C
  • String Array in C
  • Functions in C
  • Pointers in C
  • Structure in C
  • Storage Classes
  • File Handling in C
  • Top C Programs

Recent Posts

  • Structured Vs Unstructured Data in Hindi | Key Difference
  • Jhansi Ki Rani Lakshmi Bai History, Story, Information in Hindi
  • Elon musk Hindi : एलन मस्क हिंदी में, Autobiography,  Net Worth
  • World Environment Day in Hindi : Objective, Importance, Theme
  • Thomas Edison Biography in Hindi – थॉमस एडिसन जीवनी
  • International Nurses Day in Hindi | नर्स दिवस क्यों मनाते हैं?
  • Fork/Join Framework in Java | RecursiveTask, RecursiveAction
  • DBMS in Hindi | DBMS क्या है? | DBMS की विशेषताएं और प्रकार
  • Java Hibernate Framework Tutorial
    Java Hibernate Framework Tutorial | Hibernate For Beginners Hibernate
  • List Of National Animals Of Countries
    List Of National Animals Of Countries General Knowledge
  • Difference between TCP and UDP
    Difference between TCP and UDP Differences
  • Symptoms and Prevention of Coronavirus Disease (Covid-19)
    Symptoms and Prevention of Coronavirus Disease (Covid-19) Important
  • Difference between Comparable and Comparator
    Difference between Comparable and Comparator Differences
  • Yrkkh - Yeh Rishta Kya Kehlata Hai Serial, Episode, Cast
    Yrkkh – Yeh Rishta Kya Kehlata Hai Serial, Episode, Cast News
  • What is Internship? How to get Internship?
    What is Internship and how to get Internship? Important
  • Importance of Thread Synchronization in Java
    Importance of Thread Synchronization in Java Java
  • Java Tutorial
  • Servlet Tutorial
  • JSP Tutorial
  • Maven Tutorial
  • HTML Tutorial
  • Programs
  • Hindi/English Grammar
  • Difference Between ... and ...
  • HR Interview
  • Important Articles

Write to Us:
geekcer.code@gmail.com

  • About Us
  • Privacy and Policy
  • Disclaimer
  • Contact Us
  • Sitemap

Copyright © GeekCer 2022 All Rights reserved