Storage Classes
Storage classes are used to define the scope, visibility and lifetime of variables.
There are four storage
classes in C, namely:
- Auto
- Extern
- Static
- Register
In addition to these four storage classes, C11 introduced a new storage class called _Thread_local, which is used to declare thread-local variables. These variables have a unique value for each thread that accesses them.
Auto
- It is automatically allocated memory and removed from memory as per its lifetime.
- By default all variables are auto.
int a;
auto int b;
Both the statements are same.
- Default value: Garbage value.
- Lifetime: From declared till end of block.
- Scope: From declared point till end of block.
- What happens in case of a conflict between the names of a local and global variable?
- The global variable is given a priority.
- The local variable is given a priority.
- Which one will get a priority depends upon which one is defined first.
- The compiler reports an error.
- What is the output of the following C code?
#include <stdio.h>
int main()
{
int i = 10;
{
int i = 5;
printf("Inside: %d\n", i);
}
printf("Outside: %d", i);
}
Extern
- Variables declared as extern have a global scope and can be accessed across multiple files.
- They are not initialized by default and need to be defined in a separate file.
- These are stored in data segment of the memory.
- Default value: For int: 0
For float: 0.000000
For char:
- What is the output of the following C code?
file1.c
#include <stdio.h>
int count;
extern void write_extern();
int main()
{
count = 5;
write_extern();
}
file2.c
#include <stdio.h>
extern int count;
void write_extern()
{
printf("count is %d", count);
}
Static
- Variables declared as static have a local scope and retain their value between function calls.
- Variables declared as static are stored in the data segment of the memory.
- Default value: For int: 0
For float: 0.000000
For char: - Lifetime: Declaration till end of program and not till end of function
- Scope: Local variables: within block
Global variables: within file, but cannot be accessed in multiple files
- What is the output of the following C code?
- What is the output of the following C code?
#include <stdio.h>
void func()
{
static int x = 5, y;
printf("x = %d, y = %d\n", x, y);
x++;
y++;
}
int main()
{
func();
func();
func();
}
#include <stdio.h>
int main()
{
static x = 10;
static int y = 2 + 3;
static int a = x;
static int b = x + y;
}
Register
- Variables declared as register are stored in the CPU registers, which provide faster access to the variable than memory.
- A space will be searched in registers, if space is available then space will be allocated else main memory space will be allocated.
- Default value: Garbage value.
- Lifetime: From declared till end of block.
- Scope: From declared point till end of block.
- Which of the following statements are correct?
- The value stored in the CPU register can always be accessed faster than that stored in memory.
- A register storage variable will always be stored in a CPU register.
- For which of the following situation should the register storage class be used?
- For local variable in a function.
- For loop counter.
- For collecting values returned from a function.
- For variables used in recursive function.
- Where will the space be allocated for an automatic storage class variable?
- In CPU register.
- In memory as well as in CPU register.
- In memory.
- On disk.
Comments
Post a Comment