---
description: "Storage Classes in C: learn different types of storage classes auto, extern, static, register class in C with examples. A storage class represents the visibility and a location of a variable."
title: "Storage Classes in C: Auto, Extern, Static, Register (Examples)"
image: https://www.guru99.com/images/c-storage-classes-1.png
---

[Skip to content](#main)

**⚡ Smart Summary**

Storage classes in C define the scope, lifetime, default value, and memory location of every variable, using four specifiers — auto, register, static, and extern — to control where data lives and how long it survives during execution.

- 🗂️ **Four storage classes:** C provides auto, register, static, and extern specifiers to describe every variable.
- 🏠 **Auto:** The default local class, confined to its block and holding a garbage value until assigned.
- ⚡ **Register:** Requests CPU-register storage for faster access to frequently used variables such as counters.
- ♻️ **Static:** Preserves a variable value between function calls and defaults to zero on first use.
- 🌐 **Extern:** Shares a global variable or function across multiple source files without redefining it.
- 📊 **Scope and lifetime:** Each class sets where a variable is visible and how long it lives.

Read More

![Storage Classes in C](https://www.guru99.com/images/c-storage-classes-1.png)

## What is Storage Class in C?

A storage class represents the visibility and a location of a variable. It tells from what part of code we can access a variable. A storage class in C is used to describe the following things:

- The variable scope.
- The location where the variable will be stored.
- The initialized value of a variable.
- A lifetime of a variable.
- Who can access a variable?

Thus a storage class is used to represent the information about a variable.

NOTE: A variable is not only associated with a [data type](https://www.guru99.com/c-variable-datatypes-constants.html), its value but also a storage class.

## What are the Types of Storage Classes in C?

There are total four types of standard storage classes. The table below represents the storage classes in C.

| **Storage class** | **Purpose** |
| --- | --- |
| auto | It is a default storage class. |
| extern | It is a global variable. |
| static | It is a local variable which is capable of returning a value even when control is transferred to the function call. |
| register | It is a variable which is stored inside a Register. |

## Auto Storage Class in C

The variables defined using auto storage class are called as local variables. Auto stands for automatic storage class. A variable is in auto storage class by default if it is not explicitly specified.

The scope of an auto variable is limited with the particular block only. Once the control goes out of the block, the access is destroyed. This means only the block in which the auto variable is declared can access it.

A keyword auto is used to define an auto storage class. By default, an auto variable contains a garbage value.

```
Example, auto int age;

```

The program below defines a function with has two local variables

```
int add(void) {
   int a=13;
   auto int b=48;
return a+b;}

```

We take another program which shows the scope level “visibility level” for auto variables in each block code which are independently to each other:

```
#include <stdio.h>
int main( )
{
  auto int j = 1;
  {
    auto int j= 2;
    {
      auto int j = 3;
      printf ( " %d ", j);
    }
    printf ( "\t %d ",j);
  }
  printf( "%d\n", j);}

```

**Output:**

```
 3 2 1
```

**Don't Miss:**

- [C Programming Tutorial](https://www.guru99.com/c-programming-tutorial.html)
- [Dynamic Memory Allocation in C: malloc(), calloc()](https://www.guru99.com/c-dynamic-memory-allocation.html)
- [21 Best C IDE (Editor) for Windows & Mac in 2026](https://www.guru99.com/best-c-ide.html)
- [Top 40 PowerShell Interview Questions and Answers (2026)](https://www.guru99.com/powershell-interview-questions.html)

## Extern Storage Class in C

Extern stands for external storage class. Extern storage class is used when we have global functions or variables which are shared between two or more files.

Keyword extern is used to declaring a global variable or function in another file to provide the reference of variable or function which have been already defined in the original file.

The variables defined using an extern keyword are called as global variables. These variables are accessible throughout the program. Notice that the extern variable cannot be initialized it has already been defined in the original file.

```
Example, extern void display();
```

### First File: main.c

```
#include <stdio.h>
extern i;
main() {
   printf("value of the external integer is = %d\n", i);
   return 0;}

```

### Second File: original.c

```
#include <stdio.h>
i=48;

```

**Result:**

```
 value of the external integer is = 48
```

In order to compile and run the above code, follow the below steps

Step 1) Create new project,

- Select Console Application
- Click Go

[![Create new project in Code Blocks]()](https://www.guru99.com/images/1/020819_0708_StorageClas1.png)

Step 2) Select C and click Next

[![Select C language]()](https://www.guru99.com/images/1/020819_0708_StorageClas2.png)

Step 3) Click Next

[![Click Next]()](https://www.guru99.com/images/1/020819_0708_StorageClas3.png)

Step 4) Enter details and click Next

[![Enter project details]()](https://www.guru99.com/images/1/020819_0708_StorageClas4.png)

Step 5) Click Finish

[![Click Finish]()](https://www.guru99.com/images/1/020819_0708_StorageClas5.png)

Step 6) Put the main code as shown in the previous program in the main.c file and save it

[![Add code to main.c]()](https://www.guru99.com/images/1/020819_0708_StorageClas6.png)

Step 7) Create a new C file \[File -> new -> Empty File , save (as original.c) and add it to the current project by clicking “OK” in the dialogue box .

[![Create original.c file]()](https://www.guru99.com/images/1/020819_0708_StorageClas7.png)

Step 8) Put and save the C code of the original.c file shown in the previous example without the main() function.

[![Add code to original.c]()](https://www.guru99.com/images/1/020819_0708_StorageClas8.png)

Step 9) Build and run your project. The result is shown in the next figure

[![Program output]()](https://www.guru99.com/images/1/020819_0708_StorageClas9.png)

## Static Storage Class in C

The static variables are used within function/ file as local static variables. They can also be used as a [global variable](https://www.guru99.com/local-vs-global-variable.html)

- Static local variable is a local variable that retains and stores its value between function calls or block and remains visible only to the function or block in which it is defined.
- Static global variables are global variables visible only to the file in which it is declared.

```
Example: static int count = 10;
```

Keep in mind that static variable has a default initial value zero and is initialized only once in its lifetime.

```
#include <stdio.h> /* function declaration */
void next(void);
static int counter = 7; /* global variable */
main() {
 while(counter<10) {
      next();
      counter++;   }
return 0;}
void next( void ) {    /* function definition */
   static int iteration = 13; /* local static variable */
   iteration ++;
   printf("iteration=%d and counter= %d\n", iteration, counter);}

```

Result:

```
iteration=14 and counter= 7
iteration=15 and counter= 8
iteration=16 and counter= 9

```

Global variables are accessible throughout the file whereas static variables are accessible only to the particular part of a code.

The lifespan of a static variable is in the entire program code. A variable which is declared or initialized using static keyword always contains zero as a default value.

## Register Storage Class in C

You can use the register storage class when you want to store local variables within functions or blocks in CPU registers instead of [RAM](https://www.guru99.com/different-types-ram-random-access-memory.html) to have quick access to these variables. For example, “counters” are a good candidate to be stored in the register.

```
Example: register int age;

```

The keyword register is used to declare a register storage class. The variables declared using register storage class has lifespan throughout the program.

It is similar to the auto storage class. The variable is limited to the particular block. The only difference is that the variables declared using register storage class are stored inside CPU registers instead of a memory. Register has faster access than that of the main memory.

The variables declared using register storage class has no default value. These variables are often declared at the beginning of a program.

```
#include <stdio.h> /* function declaration */
main() {
{register int  weight;
int *ptr=&weight ;/*it produces an error when the compilation occurs ,we cannot get a memory location when dealing with CPU register*/}
}
```

**Output:**

```
error: address of register variable 'weight' requested

```

The next table summarizes the principal features of each storage class which are commonly used in C programming

| **Storage Class** | **Declaration** | **Storage** | **Default Initial Value** | **Scope** | **Lifetime** |
| --- | --- | --- | --- | --- | --- |
| auto | Inside a function/block | Memory | Unpredictable | Within the function/block | Within the function/block |
| register | Inside a function/block | CPU Registers | Garbage | Within the function/block | Within the function/block |
| extern | Outside all functions | Memory | Zero | Entire the file and other files where the variable is declared as extern | program runtime |
| Static (local) | Inside a function/block | Memory | Zero | Within the function/block | program runtime |
| Static (global) | Outside all functions | Memory | Zero | Global | program runtime |

## FAQs

🔍 What is the difference between scope and lifetime in C?

Scope decides where a variable is visible in the code, while lifetime decides how long it stays in memory. A variable can have block scope yet a program-long lifetime.

⚖️ How do the auto and register storage classes differ?

Both create block-scoped local variables, but auto keeps them in memory with a garbage default, while register requests faster CPU-register storage and forbids taking the variable address.

🎯 Does the register keyword guarantee CPU register storage?

No. register is only a request; the compiler may ignore it when no register is free. Modern compilers optimize register use automatically, so the keyword is now rarely needed.

🔢 Why do static and extern variables default to zero but auto to garbage?

Static and extern variables live in a zero-initialized memory segment for the whole program, so they start at zero. Auto variables reuse stack memory, keeping whatever value was left behind.

🧷 What is the difference between declaring and defining an extern variable?

An extern declaration only announces a variable defined elsewhere and cannot be initialized. The real definition, in one file, allocates the storage, and linking connects every reference to it.

🔀 Are storage classes in C the same as in C++?

Mostly, but not exactly. C++ deprecated the register keyword and repurposed auto for type inference. static and extern behave similarly, though C++ adds thread\_local and class-member storage rules.

🤖 Can AI coding assistants explain and choose C storage classes?

Yes. AI assistants describe auto, register, static, and extern, suggest the right specifier for each variable, and flag misuse such as taking the address of a register variable.

🧠 How does GitHub Copilot help when writing C storage-class code?

[GitHub Copilot](https://github.com/features/copilot) autocompletes static counters, extern declarations across files, and register hints, then explains scope and lifetime inline as you type.

#### Summarize this post with:

ChatGPTPerplexityGrokGoogle AI

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.**

##### Sign up for the newsletter

Subscribe for Free

You have successfully subscribed.  
Please check your inbox.

![AI-Newsletter]() Chosen by over **350,000+** professionals

[Scroll to top](#wrapper)

×

Search for:

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/c-storage-classes-1.png","url":"https://www.guru99.com/images/c-storage-classes-1.png","width":"700","height":"250","caption":"C Storage Classes","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/c-storage-classes.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/c-programming","name":"C Programming"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/c-storage-classes.html","name":"Storage Classes in C: Auto, Extern, Static, Register (Examples)"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/c-storage-classes.html#webpage","url":"https://www.guru99.com/c-storage-classes.html","name":"Storage Classes in C: Auto, Extern, Static, Register (Examples)","dateModified":"2026-10-08T15:46:58+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/c-storage-classes-1.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/c-storage-classes.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/editorial","name":"Editorial Team","url":"https://www.guru99.com/author/editorial","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/favicon-new-logo.png","url":"https://www.guru99.com/images/favicon-new-logo.png","caption":"Editorial Team","inLanguage":"en-US"},"sameAs":["https://www.guru99.com/editorial-team"],"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"articleSection":"C Programming","headline":"Storage Classes in C: Auto, Extern, Static, Register (Examples)","description":"Storage Classes in C: learn different types of storage classes auto, extern, static, register class in C with examples. A storage class represents the visibility and a location of a variable.","keywords":"bigdata, programming, database, server","speakable":{"@type":"SpeakableSpecification","cssSelector":[".entry-title",".summary"]},"@type":"Article","author":{"@id":"https://www.guru99.com/author/editorial","name":"Editorial Team"},"dateModified":"2026-10-08T15:46:58+05:30","image":{"@id":"https://www.guru99.com/images/c-storage-classes-1.png"},"copyrightYear":"2026","name":"Storage Classes in C: Auto, Extern, Static, Register (Examples)","subjectOf":[{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"What is the difference between scope and lifetime in C?","acceptedAnswer":{"@type":"Answer","text":"Scope decides where a variable is visible in the code, while lifetime decides how long it stays in memory. A variable can have block scope yet a program-long lifetime."}},{"@type":"Question","name":"How do the auto and register storage classes differ?","acceptedAnswer":{"@type":"Answer","text":"Both create block-scoped local variables, but auto keeps them in memory with a garbage default, while register requests faster CPU-register storage and forbids taking the variable address."}},{"@type":"Question","name":"Does the register keyword guarantee CPU register storage?","acceptedAnswer":{"@type":"Answer","text":"No. register is only a request; the compiler may ignore it when no register is free. Modern compilers optimize register use automatically, so the keyword is now rarely needed."}},{"@type":"Question","name":"Why do static and extern variables default to zero but auto to garbage?","acceptedAnswer":{"@type":"Answer","text":"Static and extern variables live in a zero-initialized memory segment for the whole program, so they start at zero. Auto variables reuse stack memory, keeping whatever value was left behind."}},{"@type":"Question","name":"What is the difference between declaring and defining an extern variable?","acceptedAnswer":{"@type":"Answer","text":"An extern declaration only announces a variable defined elsewhere and cannot be initialized. The real definition, in one file, allocates the storage, and linking connects every reference to it."}},{"@type":"Question","name":"Are storage classes in C the same as in C++?","acceptedAnswer":{"@type":"Answer","text":"Mostly, but not exactly. C++ deprecated the register keyword and repurposed auto for type inference. static and extern behave similarly, though C++ adds thread_local and class-member storage rules."}},{"@type":"Question","name":"Can AI coding assistants explain and choose C storage classes?","acceptedAnswer":{"@type":"Answer","text":"Yes. AI assistants describe auto, register, static, and extern, suggest the right specifier for each variable, and flag misuse such as taking the address of a register variable."}},{"@type":"Question","name":"How does GitHub Copilot help when writing C storage-class code?","acceptedAnswer":{"@type":"Answer","text":"GitHub Copilot autocompletes static counters, extern declarations across files, and register hints, then explains scope and lifetime inline as you type."}}]}],"@id":"https://www.guru99.com/c-storage-classes.html#schema-1143046","isPartOf":{"@id":"https://www.guru99.com/c-storage-classes.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/c-storage-classes.html#webpage"}}]}
```
