Lab 7
Lab 7
LAB REPORT
Page 1 of 7
Objectives: The main objectives of this experiment are
1. To learn about multi-file program structure
2. To learn how to write a multi-file program
3. To write a program for Fibonacci series
4. To write a program for Fibonacci series using multi-file program
5. To write a program for Fibonacci series using static variable
Theory:
Multiple file program is a very efficient and significant structure in C programming language.
When different functions are defined in different files and at the end all of them are linked
together and executed as a single program, is called a multi-file program. In multi-file program a
large C program can be divided into multiple files, which makes each file short enough to
conveniently edit and compile. External storage class is very useful to call a global variable in
multi-file program. So, the facility of editing and debugging of a large C program can be easily
maintained by writing multi-file program.
Extern storage class is used when we have global functions or variables which are shared
between two or more files. Keyword extern is used to declare a global variable in another file to
provide the reference of variable or function which have been already defined in the original file.
Fibonacci Series:
Fibonacci series is the set of numbers in which each number is the sum of the two preceding
ones, starting from 0 and 1. If Fn is denoted as nth Fibonacci number then,
Fn = Fn-1 + Fn-2 ,where n > 1
Page 2 of 7
Sample Code:
Experimental Study:
Problem 1: Write a multi-file program which shows values of two variables and a message.
main.c
#include <stdio.h>
extern int a,b;
extern void func (void);
int main()
{
printf("Value of a=%d & b=%d\n",a,b);
printf("Sum of a and b = %d\n",a+b);
func();
return 0;
}
Multifile1.c
int a = 10;
Multifile2.c
int b = 15;
Multifile3.c
Page 3 of 7
Output for problem 1:
Page 4 of 7
Problem 3: Write a program of Fibonacci series using multi-file.
Sample Code:
main.c
#include<stdio.h>
extern int func();
int main(){
int n;
printf("Enter the value of n: ");
scanf("%d",&n);
printf("\nFibonacci series: ");
func(n);
printf("\n"); }
Fibo_1.c
int func (int n)
{
int t1=0, t2=1, t3=0 ;
printf("%d ",t3);
while(t2<n)
{
printf("%d ",t2);
t3 = t1+t2;
t2 = t1;
t1 = t3;
}
}
Page 5 of 7
Problem 4: Writing a program of Fibonacci series using static variable.
Sample Code:
#include<stdio.h>
int func (int);
int main(){
int n;
printf("Enter the value of n: ");
scanf("%d",&n);
printf("\nFibonacci series: ");
func(n);
printf("\n"); }
Page 6 of 7
Discussion: In the experiment we have learnt to find Fibonacci series using multi-file program.
We have declared global variables at the beginning of the main file using extern storage class.
Function prototypes of user-defined functions referenced in each file are declared at the start of
the main file. We have carefully written the code and used semicolon after ending every
statement.
Conclusion: Multiple file program is useful when the program is very large and hard to
maintain.
We can easily edit any part of the program while the program is split into multiple files. Time
complexity can be reduced using multiple files. Multiple file program is easy to understand. So,
we should properly learn the structure of multiple file program to write an elegant and efficient
program.
Page 7 of 7