Quick links
Latest articles
Internship
Members
New...
Algorithms
Discrete Mathematics
Big data
Languages
C
C++
C++ STL
Java
Data Structure
C#.Net
Android
Kotlin
SQL
Web
PHP
Python
JavaScript
CSS
Ajax
Node.js
Web prog.
Programs
C
C++
DS
Java
C#
Python
Aptitude
C
C++
Java
DBMS
Interview
C
Embedded C
Java
SEO
HR
CS Subjects
CS Basics
O.S.
Networks
DBMS
Embedded Systems
Cloud Computing
Machine learning
CS Organizations
Linux
DOS
More...
Articles
Puzzles
News/Updates

Home » C programming language

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

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.

1) 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.



2) 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.





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Featured post:
Introduction to Linux (Its modes, Safety, Most popular Applications)
Linux Best Distribution Software (Distros) of 2018

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.