Professional Documents
Culture Documents
C Lec 66-70
C Lec 66-70
PROGRAMMING AND
(DATASTRUCTURES
Functions -question 2
NESO ACADEMY
The output of the following C program is:
NESO ACADEMY
The output of the following C program is
4 5
int c;
C= a; a = b; b= c;
int main()
{
int a=4, b=5;
f1(a, b);
}
NESO ACADEMY
The output of the following C program
C= *a;
*a; *a = *b; *b = c;
}
int main()
{ b
int b=5, C=6;
5 6
f2(&b, &c);
1000 2000
NESO ACADEMY
The output of the following C program
C = *a; *a = *b; *b = c;
}
int main()
{ b
int b=5, c=6; 6 5
f2(&b, &c};
1000 2000
NESO ACADEMY
The output of the follOwing C program is:
4 5
NESO ACADEMY
The output of the following C program is:
}
C = a; a =
void f2(int *a, int *b)
{
int c;
C=
b;
*a; *a = *b; *b
b= c;
= C;
}
f1(a, b);
f2(&b, &c);
;
printf("%", 5-4-6);
return
a b C
4 5
NESO ACADEMY
The output of the following C program
4 5
NESO ACADEMY
C
PROGRAMMING AND
DATA STRUCTURES
Functions -question 3
NESO ACADEMY
Consider the following C program: What is the output of the C
program available in the LHS?
int fun()
{ a) Infinite loop
static int num = 16; b) 13 10 7 4 1
return num--; c) 14 11 8 5 2
d) 15 12 85 2
int main()
NESO ACADEMY
Consider the following C program:
Output window
int fun()
num
for( 16 ;
fun(); fun() )
printf("%d ", fun()) ;
return 0;
}
NESO ACADEMY
Consider the following C program:
NESO ACADEMY
Consider the following C program:
NESO ACADEMY
Consider the following C program:
Output window
int fun()
num
{ 14
static int num = 16; 11
return num--;
int main()
for( 16 ;12 ; 13
", 11 );
)
printf("%d
return 0;
}
NESO ACADEMY
Consider the following C program:
Output window
int fun()
num
14 11 8
static int num = 16;
return num--;
}
int main()
for( 16 ; 9; 10 )
printf("%d 8 );
return 0;
}
NESO ACADEMY
Consider the following C program:
Output window
int fun()
num
{ 14 11 8 5 2
static int num = 16;
return num--;
}
int main()
for( 16 ;9 8
10
);
)
printf("%d
return 0;
}
NESO ACADEMY
C
PROGRAMMING AND
(DATASTRUCTURES
Static function in C
NESO ACADEMY
BASICS
NESO ACADEMY
Lets understand the usefulness of static
function with the help of an example
NESO ACADEMY
file1.c (static functions] - Code:Blocks 16.01
•Debua
wSmith Tools Tools+
M2
| Plugins
G:S:|R|Oa|GD|
DoxyBlocks Settings
<global>
Help
v
a|sC
fun
e Sources
4 C= a+b;
return ci
-O file1.c 6
-Omain.c 7
main.c [static_functions] - Code:Blocks 16.01
File Edit View Search Project Build Debug Fortran wxSmith Tools Tools+ Plugins DoxyBlocks Settings Help
•0 Debua G:6:|| <global> v mait
ML|Oa|OOolaalsC
Management file1.c main.c x
Projects Symbols 1
2
#include <stdio h> .
O Workspace #include <stdlib.h>
G static functions 3 int fun(int, int):
Ge Sources
4
5
int main ()
CAUsers\jasp\Documents\static_functi
-Dfile1.c
-main.c
int sum = fun(3, 4)
7 printf ("%d", sum) ; Phpcess returned e (exe) execu
return 0; Press any key to continue.
9
10
file1.c (static functions] - Code:Blocks 16.01
File Edit View Search Project Build Debug Fortran wSmith Tools Tools+ Plugins DoxyBlocks Settings Help
Dmain.c 7
main.c (static_functions] - Code:Blocks 16.01
File Edit View Search Project Build Debug Fortran wSmith Tools Tools+ Plugins DoxyBlocks Settings Help
5
int main ()
{
file1.c
-Dmain.c
int sum = fun (3, 4)
7 printf ("%dm sum) :
return 0;
10
Logs & others
A Code::Blocks x9Search results x Cccc x Build log >x Build messages xA ppCheck x opCheck messages xA Cscope
File L Message
C:\Users\. .. 1 warning: 'fun defined but not used [-Wunused-function)
obj\Debug... In function 'main':
C:\Users\. .. 6 undefined reference to fun
error: ld returned 1 exit status
Static functions are restricted to the files where they are declared.
NESO ACADEMY
C
PROGRAMMING AND
DATA STRUCTURES
Static and Dynamic
Scoping in C -part 1
NESO ACADEMY
PRELIMINARIES
Stack
Memory layout
of C program
Heap
uninitialized data
- Introduction >
(bss)
Initialized data
Text/Code segment
NESO ACADEMY
K Stack is acontainer (or memory segment) which holds some data.
* Data is retrieved in
NESO ACADEMY
K Stack is a container (or memory segment) which holds some data.
main()
{
fun1();
}
fun1() { fun2(); }
fun2() { fun3();}
fun3() { return; } STACK (or
CALL STACK)
NESO ACADEMY
K Stack is a container (or memory segment) which holds some data.
main()
{
fun1() ;
fun1() { fun2() }
fun2() { fun3();}
fun3() { return; } STACK (or
CALL STACK)
NESO ACADEMY
R Stack is a container (or memory segment) which holds some data.
main()
{
fun1();
fun1() { fun2(); }
fun2() { fun3();}
fun3() { return; } STACK (or
CALL STACK)
NESO ACADEMY
K Stack is a container (or memory segment) which holds some data.
main()
{
fun1();
fun1() { fun2(); }
fun2() { fun3();}
fun3() { return; } STACK (or
CALL STACK)
NESO ACADEMY
ACTIVATION RECORD
STACK (or
CALL STACK
NESO ACADEMY
Activation Record ()- is aportion of a stack which is generally composedof:
1. Locals of the callee
2. Return address to the caller
3. Parameters of the callee
Example:
int main()
b=5 a=10
int a = 10; Some addr
a = fun1 (a) ;
printf("%d" a);
} a = 10
int fun1 (int a)
1.
Record ()
Locals of the callee
-is a portion of astack which is generally composed of:
Example:
int main()
int a = 10;
a = fun1 (a) ;
printf("%d", ;
a)
} a = 15
Example:
int main()
printf Output: 15
int a = 10;
a = fun1 (a) ;
printf(%d", a);
} a = 15
int fun1(int a)
Example:
int main()
Output: 15
int a = 10;
a = fun1(a) ;
printf(%d",ya);
PROGRAMMING AND
DATA STRUCTURES
Static and Dynamic
Scoping in C -part 2
NESO ACADEMY
WHY ScOPING?
int fun1()
int a = 10;
}
int fun2()
{
int a = 40;
}
NESO ACADEMY
WHAT IS STATIC SCOPING?
In Static scoping (or lexical scoping). definition of a variable is resolved by
searching its containing block or function. If that fails,then searchingthe outer
containing block and so on.
C =
b/a;
printf ("%d", ;
c)
}
}
NES ACADEMY
Stack
Call Stack
Heap
uninitialized data
(bss)
Initialized data
NESO ACADEMY
STATIC ScoPING EXAMPLE
int fun1(int) ;
int fun2(int);
int a = 5; fun2 b= 20 c = 25
int main() int fun2(int b)
fun1 b = 20
{
int a = 10; int c;
a = fun1(a) ; C =
return
a + b;
main a = 1O
printf("%d", ;
a) return c Call Stack
}
int fun1(int b) Output: 25 5
{
b = b+10;
b = fun2(b);
Initialized Data
return b;
Segment
NES ACADEMY
STATIC ScOPING EXAMPLE
int fun1(int);
int fun2(int);
int a = 5;
int main() int fun2(int b)
{
int a = 10; int c;
a = fun1(a) : C= a
return c;
+ b;
printf("%d", a); Call Stack
} }
int fun1(int b) Output: 25
5
{{
b = b+10;
b = fun2(b);
Initialized Data
return b;
Segment
NESO ACADEMY