Difference between automatic (auto) and static variables in a C language

In this C tutorial, we are going to learn about the two storage classes auto (automatic) and static. What are automatic variables and static variables, what are the differences between them?
Submitted by IncludeHelp, on May 10, 2018 [Last updated : March 14, 2023]

Prerequisite: Storage classes in C

Automatic (auto) and static both are the keywords which are used under the storage classes, and they define the scope, lifetime, default value and memory segment of the variables.

Automatic ('auto') variable

The all local variables which are defined within the function are known as auto (automatic) variables unless not specified i.e. by default a local variable is an auto variable. There is no need to put the keyword auto (it's an optional) while declaring a local variable.

An auto variable created a new each time when the function (in which variable is declared) is called and destroyed when the program's execution leaves the function.

Example

#include <stdio.h>

int main()
{
	int a;
	auto int b;
	
	a=10; b=20;
	printf("a=%d, b=%d\n",a,b);
	
	return 0;
}

Output

    a=10, b=20

Note: Here, a and b both are automatic (auto) variables.

Static variables

It is much similar to automatic variable; all properties are almost same except few. A static variable does not create a new each time and does not destroy when program's execution leaves the function. It declares once and destroys once when program's execution finishes.

Declaration Syntax

static data_type variable_name = initial_value;

Declaration Example

static int count =0;

Example

#include <stdio.h>

void fun(void)
{
    auto int a=0;
    static int b=0;
    
    printf("a = %d, b = %d\n",a,b);
    
    a++;
    b++;
}

int main()
{
    int loop;
    
    //calling function 10 times
    for(loop=0; loop<5; loop++)
        fun();
	
	return 0;
}

Output

    a = 0, b = 0
    a = 0, b = 1
    a = 0, b = 2
    a = 0, b = 3
    a = 0, b = 4

In the function fun(), variable a is automatic and b is static. According to the property 'a' is initializing each time when function is called but b is static so it is initialized once.

Differences between static and auto variables

Automatic (auto) variables Static variables
By default all local variables are automatic variable. Keyword auto can be used to declare an automatic variable, but it is not required. static keyword must be used to declare a static variable.
Automatic variable's scope is always local to that function, in which they are declared i.e. automatic variable, can be accessible within the same block. Static variable's scope is also local to that function in which they are declared i.e. static variable can be accessible within the same block.
Automatic variable's life time is local (limited), automatic variables exit till the function execution time, when program's execution leaves the function execution, variables are destroyed. Static variable's life time is not limited. Since it's scope is local but the variable will be live (exist) till the program's execution.
Automatic variables create a new each time when program's execution enters in the function and destroys when leaves. Static variable create once, when program's execution enters in the function first time, destroys when program's execution finishes, they do not again.

C Language Tutorial »






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.