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)

  • जन्माष्टमी व्रत पूजा विस्तार से | दही हांडी | Krishna Janmashtami Puja
    जन्माष्टमी व्रत पूजा विस्तार से, दही हांडी: Krishna Janmashtami Puja Festival
  • Holi kyon manate hain in hindi? | Festival of colors in hindi
    Holi kyon manate hain in hindi? | Festival of colors in hindi Festival
  • Apj Abdul Kalam biography in Hindi, Life, Missile Man of India
    Apj Abdul Kalam biography in Hindi, Life, Missile Man of India Biography
  • Maharishi Valmiki in Hindi, Biography, Dacoit Ratnakar, Jayanti
    Maharishi Valmiki in Hindi, Biography, Dacoit Ratnakar, Jayanti Spiritual
  • What is Verb
    What is Verb? Types of Verbs and Examples Grammar
  • State the Universal law of gravitation | Law of Gravity, Formula
    State the Universal law of gravitation | Law of Gravity, Formula Science
  • P V Sindhu Biography in Hindi
    P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति Biography
  • Newton's laws of Motion, State and Explained, Formula
    Newton’s laws of Motion, State and Explained, Formula Science

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

Posted on April 15, 2022April 15, 2022 By GeekCer Education 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

Statements in C Language | Types of statements in C Language Statements in C Language | Types of statements in C Language C Language
Programming with C Language Tutorial Programming with C Language Tutorial C Language
Constants and Operators in C Language Constants and Operators in C Language C Language
Array in C programming | Character Array in C Array in C programming | Character Array in C C Language
Pointers in C Programming, Definition, Types & Example Pointers in C Programming, Definition, Types & Example C Language
Top 100 C programs for geeks Top 100 C programs for geeks 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
  • National Farmers Day in Hindi | राष्ट्रीय किसान दिवस पर निबंध | चौधरी चरण सिंह जयंती
  • Human rights day in Hindi: 10 दिसंबर ह्यूमन राइट्स डे
  • Unicef day is celebrated on December 11 | Speech on unicef day
  • Indian Navy Day: जल सेना दिवस कब और क्यों मनाया जाता है?
  • P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
  • IPv4 Vs IPv6 | Difference between IPv4 and IPv6
    IPv4 Vs IPv6 | Difference between IPv4 and IPv6 Differences
  • Network kya hai (नेटवर्क क्या है)
    Network kya hai (नेटवर्क क्या है) Networking
  • Similarities and difference between OSI and TCP/IP model
    OSI vs TCP/IP Model, Similarities and difference between OSI and TCP/IP model Networking
  • TCP/IP Model, Full Form, Layers and their Functions
    TCP/IP Model, Full Form, Layers and their Functions Networking
  • Difference between Internet and Intranet
    Difference between Internet and Intranet Differences
  • Difference between TCP and UDP
    Difference between TCP and UDP | TCP vs UDP examples Differences
  • OSI Model | 7 Layers of OSI Model in Computer network
    OSI Model | 7 Layers of OSI Model in Computer network, Functions Networking
  • 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