search
Login

Categories


C Books Guide and List
C++ Books Guide and List
Best Java Books

3.8k questions

4k answers

117 comments

10.7k users

0 votes
3.1k views
asked Guru 24.6k points 29 51 75

1 Answer

0 votes
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;

}
answered Guru 39.1k points 10 21 31

Related questions

0 votes
2 answers 4.1K views
0 votes
1 answer 1.4K views
1.4K views
asked keem Expert 13.2k points 26 40 57
0 votes
1 answer 1.4K views
1.4K views
asked keem Expert 13.2k points 26 40 57
0 votes
1 answer 42 views
0 votes
1 answer 4.4K views
...