auto keyword in C?
In C programming, auto is a storage-class specifier used to
indicate that a local variable has automatic storage duration.
In modern C programming, the keyword is rarely written explicitly because
local variables declared inside a block have automatic storage duration by
default.
#include
int main()
{
int number = 10;
printf("Number = %d", number);
return 0;
}
Here, number is a local variable declared inside the
main() function. Since it is not declared with
static, it has automatic storage duration by default.
The auto keyword can also be written explicitly:
auto int number = 10;
Both of the following declarations specify an automatic local variable:
int number = 10;
auto int number = 10;
Therefore, writing auto is normally unnecessary.
auto Variableauto keyword explicitly.
#include
void display()
{
int number = 10;
printf("Number = %d\n", number);
}
int main()
{
display();
display();
return 0;
}
Every time display() is called, the local variable
number is created as an automatic variable. When the function
returns, its lifetime ends. A subsequent call creates a new instance of the
variable.
auto Variable vs static Variable| Feature | auto Variable |
static Variable |
|---|---|---|
| Storage duration | Automatic | Entire program execution |
| Typical declaration | int x; |
static int x; |
| Initialization | Occurs each time execution reaches the declaration | Occurs only once |
| Value between function calls | Not retained | Retained |
| Default initialization | Indeterminate if not initialized | Zero if not explicitly initialized |
#include
void test()
{
auto int a = 0;
static int b = 0;
a++;
b++;
printf("a = %d, b = %d\n", a, b);
}
int main()
{
test();
test();
test();
return 0;
}
The output will be:
a = 1, b = 1
a = 1, b = 2
a = 1, b = 3
The automatic variable a is initialized again each time
test() is called, so its value remains 1. The
static variable b retains its previous value, so it increases
from 1 to 2 and then to 3.
auto Variable Is Not Initialized?An uninitialized automatic variable has an indeterminate value. It is not correct to assume that it contains a specific "garbage value" that can safely be used.
int main()
{
int number;
printf("%d", number); // Undefined behavior
}
The variable number has not been initialized before it is read.
Therefore, its value is indeterminate, and reading it in this way can result
in undefined behavior.
The correct approach is to initialize the variable before using its value:
int number = 0;
The auto keyword in C should not be confused with the
auto keyword in modern C++. Although both languages use the
same keyword, their roles are different. In C, auto is a
storage-class specifier for automatic local variables and is usually
omitted.
An automatic variable is a local variable whose storage is automatically managed according to the execution of its block. It normally exists only while execution is inside that block.
Therefore, the key points to remember are:
auto → Automatic storage duration
Local variable → Usually automatic by default
Uninitialized automatic variable → Indeterminate value
static variable → Retains its value for the entire
program execution