You are on page 1of 45

C

PROGRAMMING AND
(DATASTRUCTURES
Functions -question 2
NESO ACADEMY
The output of the following C program is:

void f1(int a, int b) int main()


{ {
int c; int a=4, b=5, c=6;
C= a; a = b; b= c; f1(a, b);
} f2(&b, &c);
void f2(int *a, int *b) printf("%d", C-a-b);
{ return ;
int c; }
C= *a; *a = *b; *b = C;
} [GATE 2015 - Set 1]

NESO ACADEMY
The output of the following C program is

void f1(int a, int b) b C

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

void f2(int *a, int *b) b

int cj 1000 2000

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

void f2(int *a, int *b) b


{ 5
1000 2000
int c

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:

void f1(int a, int b) int main()


{ {
int c; int a=4, b=5, c=6;
C = a; a = b; b = c; f1(a, b);
} f2(&b, &c);
void f2(int *a, int *b) printf("%d", c-a-b) ;
{ return 0;
int c;
C = *a: *a = *b: *b = C; a b C

4 5

NESO ACADEMY
The output of the following C program is:

void f1(int a, int b) int main()


{ {
int c; int a=4, b=5, C=6;

}
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

void f1(int a, int b) int main()


{ {
int cj int a=4, b=5, C=6;
C = a; a = b; b = c; f1(a, b);
} f2(&b, &c) ;
void f2(int *a, int *b) printf("%d", -5 );
{ return ;
int c;
C = *a: *a = *b: *b = c
b C

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()

for(fun(0; fun(); fun())


printf("%d ", fun());
return 0;

NESO ACADEMY
Consider the following C program:

Output window
int fun()
num

static int num = 16; 15


return num-r
}
int main()

for( 16 ;
fun(); fun() )
printf("%d ", fun()) ;
return 0;
}

NESO ACADEMY
Consider the following C program:

int fun() Output window


num
{
static int num = 16; 14
return num--;
}
int main()
f
for( 16 ; 15 ; fun())
printf("%d
return 0;
14 3

NESO ACADEMY
Consider the following C program:

int fun() Output window


num
{ 14
static int num = 16; 12
return num-- ;
}
int main()
f
for( 16 ; 12 ; 13 )
printf("%d 14 );
return 0;
}

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

InC, functions are global by default.

This means if we want to access the function outside from the


file where it is declared, we can access it easily.

Now if we want to restrict this access,then we make our


function static by simply putting a keyword static in front of the
function.

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

File Edit View Search Project Build Debug Fortran

•Debua
wSmith Tools Tools+

M2
| Plugins

G:S:|R|Oa|GD|
DoxyBlocks Settings

<global>
Help
v
a|sC
fun

Management filel.c x main.c x


Projects Symbols int fun (int a, int þ)
OWorkspace 2
static_functions int ci

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

•Debug <alobal> vmait


•Aa||aGOo|aalsC
Management
filel,c x main.c x
Projects Symbols
static int fun (int a, int b)
OWorkspace
G static functions 3 int c;
Ge Sources
4 c= a+b:
return ci
file1.c

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

•Debug <global> vmait


•Aa||aGOo|aalsC
Management file1.c main.c x
Projects Symbols 1 #include <stdio. h>
OWorkspace 2 #include <stdlib. h>
G static functions 3 int fun(int, int) :
Ge Sources
4

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

CAUsers\jaspr\Documents\static_functions\main.c Windows(CR+LF) WINDOV


IMPORTANT TAKE AWAYS

Static functions are restricted to the files where they are declared.

Reuse of the same function in another file is possible

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.

Last In First Out (LIFO)fashion.

* Data is retrieved in

Two operations: push and pop.

NESO ACADEMY
K Stack is a container (or memory segment) which holds some data.

RData is retrieved in Last In First Out (LIFO) fashion.

k Two operations: push and pop.

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.

Data isretrieved in Last In First Out (LIFO)fashion.

Two operations: push and pop.

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.

Datais retrieved in Last In FirstOut (LIFO)fashion.

Two operations: push and pop.

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.

Dataisretrieved in Last In First Out (LIFO) fashion.

K Two operations: push and pop.

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)

int b = 5; STACK (or


b= b+a;
return b;
CALL STACK)
NESO ÅCADEMY
Activation

1.
Record ()
Locals of the callee
-is a portion of astack which is generally composed of:

2. Returnaddress to the caller


3. Parameters of the callee

Example:
int main()

int a = 10;
a = fun1 (a) ;
printf("%d", ;
a)
} a = 15

int fun1 (int a)


{
int b = 5; STACK (or
b = b+a;
return b;
CALL STACK)
NESO CADEMY
Activation Record () -is a portion of astack which is generally composed of:

1. Locals of the callee


2. Returnaddress to the caller
3. Parameters of the callee

Example:
int main()
printf Output: 15
int a = 10;
a = fun1 (a) ;
printf(%d", a);
} a = 15

int fun1(int a)

int b = 5; STACK (or


b = b+a;
return b;
CALL STACK)
NESO CADEMY
Activation Record ()-is a portion of a stack which is generally composed of:
1. Locals of the callee

2. Returnaddress to the caller


3. Parameters of the callee

Example:
int main()
Output: 15
int a = 10;
a = fun1(a) ;
printf(%d",ya);

int fun1 (int a)


{
int b = 5; STACK (or
b=bta; CALL STACK)
return b;
NESO ACADEMY
C

PROGRAMMING AND
DATA STRUCTURES
Static and Dynamic
Scoping in C -part 2
NESO ACADEMY
WHY ScOPING?

Scoping is necessary if you want to reuse


Variable names
Example:

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.

int a = 10, b = 20;


int fun()
{
int a = 5; Output: 4
{
int c:

C =
b/a;
printf ("%d", ;
c)
}
}
NES ACADEMY
Stack

Call Stack

Heap

uninitialized data
(bss)

Initialized data

Text/Code segment Initialized Data


Segment

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); Call Stack


}
int fun1(int b)
5
{
b = b+10; a
b = fun2(b);
Initialized Data
return b;
Segment
NESO ACADEMY
STATIC ScOPING EXAMPLE
int fun1(int );
int fun2(int);
int a = 5;
int main() int fun2(int b)
fun1 b = 25
{
int a = 10; int c; main
a = fun1(a) : C= a + b;
a = 10

printf(“"%4", a); return c; Call Stack


} }
int fun1(int b)
5
{
b= b+10; a
b = fun2(b);
InitializedData
return b;
Segment
NESO ACADEMY
STATIC ScOPING EXAMPLE
int fun1(int);
int fun2(int);
int a = 5;
int main() int fun2(int b)
{
int a = 10; int C; main
a = fun1(a); C= a + b; a = 25

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

You might also like