Storage Classes:
storage class specifiers:
auto
register
static
extern
----------------------
storage class specifier decides:
storage: where to store variables/data?
scope: where can be used?
lifetime: creation to deletion
initial value: garbage or zero
--------------------------
auto vs. register
storage: stack - CPU registers
scope: function/block - function/block
lifetime: function/block - function/block
initial value: garbage value - garbage value
extern:
storage: data
scope: local extern declaration function/block, global external declaration - file (any function)
lifetime: program
initial value: zero
global static:
storage: data
scope: file (within same file where global static variables declared/defined)
lifetime: program
initial value: zero
local auto vs. local static:
storage: stack - data
scope: function/block - function/block
lifetime: function/block - program
initial value: garbage value - zero
register:
register is like a request
to increase execution speed
if CPU registers are not available, those variables will be stored in stack segment.
we cant read address of a register variable
scanf() cant be used to read variables
pointers can not be used for register variables
----------------
auto, register can be used only for local variables
static - global function, global variables, local variables
extern - to access global variables or function defined in another file. (definition vs. declaration)
default storage class specifier:
auto for local variables
extern for global variables.
-----------------
definition vs. declaration:
definition will create memory, but declaration will not create memory.
definition must be global, but declaration can be local or global.
definition can be initialized, but declaration cant be initialized.
extern can be used with declaration, but should not be used with definition.
------------------
memory segments:
external memory
code - program instructions
data - extern variables, static variables
stack - local variables
heap - dynamically allocated memory (malloc(), calloc(), realloc())
internal memory:
CPU Registers - for register variables
#include <stdio.h>
void fun(void);
int main(void)
{
fun();
fun();
fun();
return 0;
}
void fun(void)
{
int a = 10;
a++;
printf("a value = %d\n", a);
}
#include <stdio.h>
void fun(void);
int main(void)
{
fun();
fun();
fun();
return 0;
}
void fun(void)
{
auto int a = 10;
a++;
printf("a value = %d\n", a);
}
#include <stdio.h>
void fun(void);
int main(void)
{
fun();
fun();
fun();
return 0;
}
void fun(void)
{
static int a = 10;
a++;
printf("a value = %d\n", a);
}
Type Qualifiers:
const
volatile
volatile:
---------------
int time;
while (time != T)
{
; // do nothing till value of variable time equals T (100)
}
where time is variable (updated by timer hardware), T is constant.
Issue: infinite loop.
-----------------
declaration of volatile variables:
volatile int time;
-----------------
solution:
volatile int time;
while (time != T)
{
; // do nothing till value of variable time equals T (100)
}
where time is variable (updated by timer hardware), T is constant.
-----------------
If a variable is declared with the qualifier volatile, then we are instructing the compiler to turn off the optimization process for that variable i.e we are forcing the compiler to read the value of that variable from memory only, each time it is encountered in the program.
-------------------------
Applications of volatile keyword:
Memory mapped I/O
Variables shared among multiple processes/threads
Variables that can be modified by interrupt routines
------------------------
Question: Can we apply both const and volatile keywords to same variable?
const volatile int x = 10; // valid/invalid?
Ans: Valid, ex: hardware status registers
---------------------
what is default storage class for local variables? auto
what is default storage class for global variables? extern
what are storage class specifiers can be used for local variable? auto, register, static
what are the 3 different uses of static keyword?
global variables
global functions
local variables