lecture 3-5
lecture 3-5
Scope of a variable
Program depends upon where it
has been defined
Lifetime:
• During program execution
• During function execution
Memory:
• Function Stack
• Heap
• (Initialized & Uninitialized) Data Segment
• Code Segment
• Auto • Static
• Register • Extern
4
BITS Pilani, Pilani Campus
Automatic Storage Class (1/2)
#include<stdio.h>
• Default storage class for block
int main()
{
• Keyword: auto
{ • Scope (Visibility): definition block
• Lifetime (Duration): definition block
auto int i=5;
• Initial Value: Garbage (“indeterminate”
} according to C standard)
{ • Memory: Function Stack
printf("\n%d",i);
} • Output:
Compilation error, i undeclared at printf
return 0;
}
#include<stdio.h>
• local variable gets precedence over
int main()
non-local variable
{
int i=1;
• Output:
{
• 3
int i=2;
• 2
{
• 1
int i=3;
printf("\n%d",i);
}
printf("\n%d",i);
}
printf("\n%d",i);
return 0;
}
• Auto • Static
• Register • Extern
8
BITS Pilani, Pilani Campus
Register storage class
• Output:
Compilation error
• Auto • Static
• Register • Extern
10
BITS Pilani, Pilani Campus
Static Storage Class (1/3)
Static Local Variable
#include<stdio.h> • Keyword: static
int fun() • Scope: definition block
{ • Lifetime: program
static int count; • Initial Value: 0
count++; • Initialized only once in their lifetime
return count; • Memory: data segment
}
#include <stdio.h>
31
Static Storage Class (3/3)
Static Local Variable
#include<stdio.h> • Can be initialized using constant
int initializer(void) literals only
{
return 50;
} • Output:
Compilation error
int main()
{
static int i = initializer();
printf(" value of i = %d", i);
return 0;
} Note:
• Scope of Static Local variable is in
its definition block
• Scope of Static Global variable is in
the file where it has been defined
=======
file1.c • Output:
======= In file1
void fun2(){ In file2
printf(“In file1”);
} • If a variable/function is declared
int main(){ static then it’s scope gets limited to
fun2(); its file only
fun1();
} • So, it is visible to functions in the
same file only
=======
file2.c • Variables/Functions with same name can
======= be used in other files as part of
static void fun2(){ multi-file compilation (very useful for
printf(“In file2”); library writers)
}
void fun1(){
fun2();
}
• Auto • Static
• Register • Extern
15
BITS Pilani, Pilani Campus
extern Storage Class (single sourcefile)
33
• If an extern variable is defined but not initialized:
• Is set to zero (0) once, when storage is allocated
34
Example:
#include <stdio.h>
disp();
return 0;
}
35
Extern Storage Class (1/3)
#include <stdio.h>
• Scope: program
extern int x; • Lifetime: program
• Initial Value: 0
int main() • Memory: data segment
{
printf("x: %d\n", x);
} • Output:
10
int x = 10;
• More than one source file is compiled separately and linked later to form
an executable object code.
This approach is very useful because any change in one file does not affect
• other files thus eliminating the need for recompilation of the entire
program.
• Multiple source files can share a variable declared as an external variable.
• Variables that are shared by two or more files are global variables and we
must declare them accordingly in one file and then explicitly define them
with extern in other file.
USE OF EXTERN IN A MULTIFILE PROGRAM
file1.c file2.c
=======
file1.c • If a global variable/function is defined in a
======= file then it can be forward declared by using
extern int var; extern in other files
int main(void)
{ • var is defined in file2.c
var = 10; • var is declared in file1.c i.e. “extern int
printf(“%d “, var); var;” will declare “var” and not define it
return 0;
}
• Output:
======= 10
file2.c
=======
...
int var;
...
• The extern specifier tells the compiler that the following variables types and
names have already been declared elsewhere and no need to create storage
space for them.
• It is the responsibility of the linker to resolve the reference problem.
• A function defined in one file and accessed in another must include a
function declaration.
• The declaration identifies the function as an external function whose
definition appears elsewhere.
• We place such declarations at the beginning of the file, before all functions.
• Although all functions are external, it would be good practice to explicitly
declare such functions with storage class extern.
Multi-file Compilation
} // factorial
EXAMPLE
#include<stdio.h> int factorial(int x)
#include<conio.h> {
void main() int f;
{ if(x==1)
int a; return(1);
int factorial (int); else
printf("\nEnter the number:"); f=x*factorial (x-1);
scanf("%d",&a); return(f);
printf("The factorial of %d is }
%d",a,factorial(a));
} OUTPUT:
Enter the number:5
The factorial of 5! is120
Fibonacci Example
int fib(int number)
{
if (number == 0) return 0;
if (number == 1) return 1;
return (fib(number-1) + fib(number-2));
}
Fibonacci Numbers
FIBONACCI SERIES USING RECURSION