2. 2
Local Variables
Local variable
– Variables declared inside a function
– Accessible only from the function in which it is declared
– When a function starts, the variables in the function is
created, and when the function ends the variables are
destroyed
• If the function executes again, new memory is assigned to the
variable. It’s value is not continued
int sum( int k ) {
int a ;
a = k + 3 ;
return a ;
}
3. 3
Local Variables
Example
#include <stdio.h>
void function() ;
void main(void)
{
int i = 1, j = 0;
function() ;
printf(“main : i=%d, j=%d n”, i, j);
}
void function()
{
int j=0, k = 1;
printf(“in function : j=%d, k=%d n”, j, k);
}
The same variables
or
Different variables with the same name?
4. 4
Local Variables
Example
#include <stdio.h>
void function() ;
void main(void)
{
int i = 1, j = 0;
function() ;
printf(“main : i=%d, j=%d n”, i, j);
}
void function()
{
int j=0, k = 1;
printf(“in function : j=%d, k=%d n”, j, k);
i = 0 ;
}
i j
main
j k
function
5. 5
Local Variables
Parameters of a function
– The parameters of a function are local variable of the function
#include <stdio.h>
void function(int) ;
int main(void)
{
int i = 0, j = 0;
function(i, j) ;
printf(“i=%d, j=%d n”, i, j);
}
void function(int i, int j)
{
int k = 0 ;
i++ ; j++ ;
printf(“i=%d, j=%d, k=%d n”, i, j, k);
}
i j
main
i j
function
k
6. 6
Global Variables
Global variable
– Variables declared outside functions
• When the program starts, global variables are created.
• When the program ends, global variables are destroyed
– Accessible at any functions
– Automatically initialized with 0
int g ;
int main()
{
printf( “%dn”, g ) ;
my_func() ;
printf( “%dn”, g ) ;
}
void my_func()
{
g = 10 ;
}
7. 7
Global Variables
Example
#include <stdio.h>
int g = 0;
int main(void)
{
int i =0, j=0 ;
...
}
void function(int i)
{
int j = 3 ;
g++ ;
}
i j
main
i j
function
g
your program
8. 8
Global Variables
Example
#include <stdio.h>
int g = 0;
int main(void)
{
int i =0, j=0 ;
function(i) ;
printf(“main : %d %d %dn”, i, j, g);
}
void function(int i)
{
int j = 3 ;
i++ ;
printf(“in function : %d %d %dn”, i, j, g);
g++ ;
}
i j
main
i j
function
g
your program
9. 9
Static Variables
Static variable
– Variables declared with static
– Similar to global variables
• When the program starts, static variables are created
• When the program ends, static variables are destroyed
• Initialized to 0, automatically
– Static variables declared inside a function
• Accessible only from the function in which it is declared
– Static variables declared outside functions
• Accessible only from the same source file in which it is declared
(Later, refer to making program by many source files)
10. 10
Static Variables
Example
void func(void)
{
static int s ;
int k ;
printf( “%d %dn”, k, ++s ) ;
k = 10 ;
printf( “%d %dn”, k, ++s ) ;
}
#include <stdio.h>
void func(void)
void main(void)
{
func() ;
func() ;
}
11. 11
Static Variables
Example: How many times the function is
called?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void do_nothing(void) ;
void main(void)
{
int j ;
srand( time(NULL) ) ;
for( j = 0 ; j < 10 ; j++ )
if( rand() % 2 ) do_nothing() ;
}
void do_nothing(void)
{
static int cnt ;
printf( “do_nothing is called %d time(s)n”, ++cnt ) ;
}
12. 12
Register Variables
Register variable
– Request to use registers which are the most fastest memory
– Declarable as only integer data type
– Not always allocated to the registers
• Even though you declare register variables, it may not be
allocated into registers.
[Ex]
void main() {
register int i; /* same to register i */
for(i = 0; i < LIMIT; ++i) {
…
}
}
13. 13
Const
Const
– Using to declare variables that cannot be modified
– When it is declared, must initialize it
– Not a Constant !! It is a Variable.
[Ex] const int a = 1;
[Ex] const int a = 1;
a = 4; /* error!! */
a++; /* error!! */
14. 14
Scope Rules
Scope Rules
– Identifiers declared inside blocks
• Accessible only from the block in which it is declared
• When the block starts, the variables are available, and when
the block ends, the variables die
– Identifiers declared outside block
• Accessible from all blocks
• If the program starts, the variables are available, and when the
program ends, the variables die
15. 15
Scope Rules
Scope Rules
#include <stdio.h>
int g ;
void function() {
static int a ;
printf(“a=%d g=%d”, a, g);
}
int main(void) {
int a = 3, b = 4;
{
int b=5;
printf(“%d %d”, a, b, g);
}
printf(“%d %d”, a, b, g);
function() ;
}
When each variable is
initialized and destroyed?
Which variables are printed?