Functions and macros are declared in header files. Header files would be included in source files by the compiler at the time of compilation.
Header files are included in source code using #include directive.#include<some.h> includes all the declarations present in the header file 'some.h'.
A header file may contain declarations of sub-routines, functions, macros and also variables which we may want to use in our program. Header files help in reduction of repetitive code.
Syntax of include directive:
#include<stdio.h> //includes the header file stdio.h, standard input output header into the source code Functions can be declared as well as defined in header files. But it is recommended only to declare functions and not to define in the header files. When we include a header file in our program we actually are including all the functions, macros and variables declared in it.
In case of pre-defined C standard library header files ex(stdio.h), the functions calls are replaced by equivalent binary code present in the pre-compiled libraries. Code for C standard functions are linked and then the program is executed. Header files with custom names can also be created.
Program: Custom header files example
/****************
Index: restaurant.h
****************/
int billAll(int food_cost, int tax, int tip);
/****************
Index: restaurant.c
****************/
#include<stdio.h>
int billAll(int food_cost, int tax, int tip)
{
int result;
result = food_cost + tax + tip;
printf("Total bill is %d\n",result);
return result;
}
/****************
Index: main.c
****************/
#include<stdio.h>
#include"restaurant.h"
int main()
{
int food_cost, tax, tip;
food_cost = 50;
tax = 10;
tip = 5;
billAll(food_cost,tax,tip);
return 0;
}
Header files are included in source code using #include directive.#include<some.h> includes all the declarations present in the header file 'some.h'.
A header file may contain declarations of sub-routines, functions, macros and also variables which we may want to use in our program. Header files help in reduction of repetitive code.
Syntax of include directive:
#include<stdio.h> //includes the header file stdio.h, standard input output header into the source code Functions can be declared as well as defined in header files. But it is recommended only to declare functions and not to define in the header files. When we include a header file in our program we actually are including all the functions, macros and variables declared in it.
In case of pre-defined C standard library header files ex(stdio.h), the functions calls are replaced by equivalent binary code present in the pre-compiled libraries. Code for C standard functions are linked and then the program is executed. Header files with custom names can also be created.
Program: Custom header files example
/****************
Index: restaurant.h
****************/
int billAll(int food_cost, int tax, int tip);
/****************
Index: restaurant.c
****************/
#include<stdio.h>
int billAll(int food_cost, int tax, int tip)
{
int result;
result = food_cost + tax + tip;
printf("Total bill is %d\n",result);
return result;
}
/****************
Index: main.c
****************/
#include<stdio.h>
#include"restaurant.h"
int main()
{
int food_cost, tax, tip;
food_cost = 50;
tax = 10;
tip = 5;
billAll(food_cost,tax,tip);
return 0;
}