Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between local variables auto int a and int a?

Tags:

c

Use case of storage class identifier auto?I understand that all local variables are auto by default. But whats makes difference by writing explicitly auto int a ?

like image 700
vkesh Avatar asked Mar 29 '13 14:03

vkesh


People also ask

What is the difference between int and auto int?

There is strictly no difference. The common practice is not to put the auto specifier.

What's the difference between auto variable and local variable?

auto variables (not to be confused with auto keyword) are typically non-static local variables. They are stored in what is usually called "stack" space. "Local variables are non existent in the memory after the function termination", You probably mean Scope, { , } and not function termination.

What is the difference between an auto and non auto variable?

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.

What is the difference between local variable?

Variables are classified into Global variables and Local variables based on their scope. The main difference between Global and local variables is that global variables can be accessed globally in the entire program, whereas local variables can be accessed only within the function or block in which they are defined.


2 Answers

There is strictly no difference.

{
   auto int a;
   /* ... */
}

and

{
   int a;
   /* ... */   
}

are equivalent.

The common practice is not to put the auto specifier.

like image 100
ouah Avatar answered Sep 28 '22 03:09

ouah


There are two possible cases:

  1. auto is the default, and explicitly adding the keyword accomplishes nothing
  2. auto isn't allowed (e.g., on a global variable) in which case adding auto prevents the code from compiling
like image 42
Jerry Coffin Avatar answered Sep 28 '22 03:09

Jerry Coffin