0
825views
C Program Elements
1 Answer
0
1views

(a) Header file

  • A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source files.
  • 2 types - files that programmer writes

$\hspace{2.5cm}$- files that comes with compiler

For example:

$\underbrace{\#include}_{\text{preprocessor directive to call header file}}$ $\underbrace{\lt stdio.h\gt}_{\text{header file}}$

  • It is a simple practice in C or C++ programs to keep all the constants, macros, system wide global variables and function prototype in the header file and include that header file whenever it is required.

(b) Macros

  • A macro is a collection of code that is defined in a C program by a name. It differs from a function in the sense that once a macro is defined by a name, the compiler put the corresponding codes for it at every place where that macro name appears.

Advantages of using macro:

  • Speed of execution of program increases.
  • Reduces length of program.
  • Reduces the errors caused by repetitive coding.

For eg:

#include$\lt stdio.h\gt$

#define MAX-SIZE 10 // Macro

int main(void)

{ int size = 0;

size = size + MAX-SIZE;

printf("%d", size);

return 0;

}

After preprocessing,

#include$\lt stdio.h\gt$

#...

..

int main(void)

{ int size = 0;

size = size + 10;

...

...

}

The macro MAX-SIZE is replaced by its value(10).

Please log in to add an answer.