1.What is a Multifile program?
- If a program contains many functions then it is difficult to read and maintain because when the number of functions increases the size of the program/file will also increase. To avoid this some functions can be written in separate file
- The individual files will be compiled separetely and then linked together to form one executable object program.
- This will help to editing and debugging because each file can be maintained at a manageable size.
2.How functions are defined in Multifile Program?
- Within a multi file program a function definition may be either external (or) static
- An external function will be recognized throughout the entire program(ie all the files)
- Static function will be recognized only with in the file in which it is defined.
- The default storage class for the function is external
- Syntax for Function definition
Storage class datatype name(type1 arg1......typen,argn) { }
- Example:
extern int calculate(int x) { }
3.How functions are declared in Multifile Program?
- When a function is defined in one file and accessed in another one file, the 2nd file must include a function declaration.
- This declaration identifies the function as an external function whose definition appears else where.
- The function declarations are usually placed at the beginning of the file before any function definition.
- Syntax for external function declaration is same as function definition with additional one semicolon at the end
Storage class datatype name(type1 arg1...typen,argn);
- Example:
extern int calculate(int x);
- To execute a multifile program, each individual file must be compiled separetely then the main program file is compiled and executed.
4.A simple Multifile program in c
/****************File1*********/
#include<stdio.h>
//Include file test.c
#include "test.c"
//function declaration
extern void result(void);
int main()
{
printf("In file1 main function\n");
result();
return;
}
/*****************File2*********/ extern void result(void) { printf("Welcome to file2\n"); return; }output:
./a.out
In file1 main function
Welcome to file2
5.Accessing external variables in Multifile function:
- Within a multifile program, external variables defined in one file and accessed in another file.
- An external variable definition can appear in only one file.
- Its location within the file must be external to any function definition. Usually it will appear at the beginning of the file.
- External variable may include initial values.
- If no initial value is assigned then it will automatically initialized to zero.
- To access an external variable in another file,the variable must first be declared within that file .This declaration may appear anywhere within the file usually at the beginning of the file.
- The declaration must begin with the storage class specifier extern. Initial values cannot be included in external variable declaration
- Example:
/*********file1**********/ #include<stdio.h> //Include file test.c #include "test.c" //External variable definition int x=1,y=2,z=3; //function declaration extern void result(void); int main() { printf("In file1 main function\n"); x=y+z; printf("x=%d y=%d z=%d\n",x,y,z); result(); return; }
/*****************File2*********/ extern int x,y,z;/*external var declaration*/ extern void result(void) { printf("Welcome to file2\n"); printf("x=%d y=%d z=%d\n",x,y,z); return; }
Output:./a.out
In file1 main function
x=5 y=2 z=3
Welcome to file2
x=5 y=2 z=3 - The value assigned to an external variable can be altered within any file in which the variable is recognized
- The changes will be recognized in all other files. Thus external variables provide a convenient means of transferring information between files.
- Example:
/*********file1**********/ #include<stdio.h> //Include file test.c #include "test.c" //External variable definition int x=10,y=20,z=30; //function declaration extern void result(void); int main() { printf("In file1 main function\n"); x=y+z; printf("x=%d y=%d z=%d\n",x,y,z); result(); return; }
/*****************File2*********/ extern int x,y,z;/*external var declaration*/ extern void result(void) { printf("Welcome to file2\n"); y=y+10; z=z+10; printf("x=%d y=%d z=%d\n",x,y,z); return; }
Output:./a.out
In file1 main function
x=50 y=20 z=30
Welcome to file2
x=50 y=30 z=40