Originally Posted by
Rahul11
sorry I didn't understand what you mean.
Do you mean I need three files main.c file1.c and file1.h
What should I keep inside header file ?
First of all, you should avoid global variables if at all possible. Define them in main() and pass them to functions called by main().
A simple program may only consist of a few functions in one .c file. When you create larger programs, you will need to put functions in several files. Then you will need a header file to place the extern function declaration/prototypes, struct declarations, etc... along with minimal global variables.
You would place the extern variables in the header file. A very simplistic example of a multiple module program, programmed on Linux:
foo.h
Code:
#ifndef FOO_H
#define FOO_H // To prevent multiple inclusion of the header file
extern int result; // Declaration only, will be defined later
int square(const int val); // Function prototype (Declaration)
#endif // FOO_H
foo.c
Code:
#include <stdio.h>
#include "foo.h" // Your variable & function declarations
int main(void)
{
int num = 10; // Always initialize local varaibles
result = square(num);
printf("num == %d, result == %d\n", num, result);
return 0;
}
bar.c
Code:
#include "foo.h"
// For illustration purposes only, normally would be defined in foo.c
int result = 0; // Definition of the global variable
int square(const int val) // Function prototype (Declaration)
{
return val * val;
}
Output:
Code:
$./foo
num == 10, result == 100