What is auto variable in C and how it works

What is auto variable in C and how it works :

Auto variable or Automatic variables are actually local variable that is automatically allocated when the program control enters in its scope, and it is automatically deallocated when the control exits its scope.While declaring a variable, we can use auto type to mark it as a automatic variable :

auto int no;

Here, no is a auto variable.But even if we haven’t declared it as auto,it will still be a auto variable. i.e. any variable we used previously inside any method is an auto variable.

#include <stdio.h>

int main(){
	auto int i = 10;
	int j = 20;
}

In this example, both i and j variables are auto variables.

#include <stdio.h>

void myFunction(){
	int a;
	auto int b;
}

int mySecondFunction(){
	auto int c = 10;
	int d = 20;
}

Here, all a,b,c,d variables are auto variables.

Leave a Reply