auto in C,static in C,extern in C,register in C.
Previous | Home | Next |
auto variable is declared with its initial valve . The scope of the auto variable with in the block. By default if a variable declare with initial value then that variable is known as implicit auto variable. For explicit use auto key word.
Example:
int main()
{
auto int i = 0; // Explicitly declared as auto.
int j = 0; // Implicitly auto.
}
main() { auto int a=10; printf(“%d”,a); }Output: 10
Example:
main() { auto int a1; printf(“%d”,a1); }Output: 1002
In example 1, a value is initialized to 10.So,output is 10.
In example 2, a1 value is not initialized.So compiler reads a value is a1 garbage value.
static
Static Storage class is the default
storage class for global variables. Static Storage class variable will be
initialized to NULL in pointer. static variable by default initialized
with 0. Static Storage class can be initialized at the time of compiled. Static
variable have only one value during the program. If static variable declare as
global then that's value cannot be changed during the program for local
declaration value can be change.
int main()
{
// Initial value is zero; a is visible only within main:
static int a;
}
Example :
main()
{
add();
add();
}
add()
{
static int a=10;
printf("\n%d",a);
a+=1;
}
10
11
extern
external variable is if defined with in the block then the scope of the variable is with in the block .if the external variable is global then the scope of the variable is in entire program. for external variable used extern keyword.
Example:
int main(
)
{
// Reference to a, defined below:
extern int a;
}
Example :
int i=10; main() { int i=2; printf("%d",i); display(); } display() { printf("\n%d",i); }Output: 2 10
In above example i declared in two time .One is above the main(),second is
within main().Within main() i is used in within main() only. Before main() i is
used to out of the main() functions i.e, display() uses the i value as 10.
register storage classes are used in the case of frequently used variables. register variable stored in CPU registers. . Initially register variable is use for global variables value. Access of the register variable is faster then other type of storage classes. Scope of the register variable is with in the block where that variable is defined when register variable declare as local
Previous | Home | Next |