Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

C Tutorial

C HOME C Intro C Get Started C Syntax C Output C Comments C Variables C Data Types C Constants C Operators C Booleans C If...Else C Switch C While Loop C For Loop C Break/Continue C Arrays C Strings C User Input C Memory Address C Pointers

C Functions

C Functions C Function Parameters C Scope C Function Declaration C Recursion C Math Functions

C Files

C Create Files C Write To Files C Read Files

C Structures

C Structures C Structs & Pointers C Unions

C Enums

C Enums

C Memory

C Memory Management

C Errors

C Errors C Debugging C NULL C Error Handling C Input Validation

C More

C Date C Macros C Organize Code C Storage Classes

C Projects

C Projects

C Reference

C Reference C Keywords C <stdio.h> C <stdlib.h> C <string.h> C <math.h> C <ctype.h> C <time.h>

C Examples

C Examples C Real-Life Examples C Exercises C Quiz C Compiler C Syllabus C Study Plan C Certificate

C Storage Classes


C Storage Classes

Storage classes define the lifetime, visibility, and memory location of variables.

There are four main storage class specifiers in C:

  • auto
  • static
  • register
  • extern

Difference Between Scope and Storage Classes

Scope defines where a variable can be used, and storage classes define how long it lasts and where it's stored. This chapter continues from the C Scope chapter.


auto

The auto keyword is used for local variables. It is the default for variables declared inside functions, so it's rarely used explicitly.

Example

int main() {
  auto int x = 50;  // Same as just: int x = 50;
  printf("%d\n", x);
  return 0;
}

Try it Yourself »


static

The static keyword changes how a variable or function behaves in terms of lifetime and visibility:

  • Static local variables keep their value between function calls.
  • Static global variables/functions are not visible outside their file.

Example

void count() {
  static int myNum = 0; // Keeps its value between calls
  myNum++;
  printf("num = %d\n", myNum);
}

int main() {
  count();
  count();
  count();
  return 0;
}

Result:

num = 1
num = 2
num = 3

Try it Yourself »

Try to remove the static keyword from the example to see the difference.


register

The register keyword suggests that the variable should be stored in a CPU register (for faster access).

You cannot take the address of a register variable using &.

Note: The register keyword is mostly obsolete - modern compilers automatically choose the best variables to keep in registers, so you usually don't need to use it.

Example

int main() {
  register int counter = 0;
  printf("Counter: %d\n", counter);
  return 0;
}

Try it Yourself »


extern

The extern keyword tells the compiler that a variable or function is defined in another file.

It is commonly used when working with multiple source files.

File 1: main.c

#include <stdio.h>

extern int shared;  // Declared here, defined in another file

int main() {
  printf("shared = %d\n", shared);
  return 0;
}

File 2: data.c

int shared = 50;  // Definition of the variable

Compile both files together:

gcc main.c data.c -o program


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.