Developer community
0 votes
3.2k views
asked by Guru (24.6k points) 30 54 83 | 3.2k views

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 by Guru (39.1k points) 10 22 36

Related questions

0 votes
2 answers
0 votes
1 answer
asked by keem Expert (13.2k points) 27 43 61 | 1.4k views
0 votes
1 answer
asked by keem Expert (13.2k points) 27 43 61 | 1.4k views
0 votes
1 answer
0 votes
1 answer
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

Categories


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

3.8k questions

4k answers

117 comments

10.7k users

3,751 questions
4,046 answers
117 comments
10,709 users