example of header file in c

Example of a Header File in C

Here is an example of a header file in C:

#ifndef MYHEADER_H
#define MYHEADER_H

// Function declaration
int add(int a, int b);

#endif

Explanation

  • The #ifndef directive checks if the identifier MYHEADER_H has not been defined before. If it has not been defined, the code between #ifndef and #endif will be included in the compilation process. This is a common technique to prevent multiple inclusions of the same header file, which can cause compilation errors.
  • The #define directive defines the identifier MYHEADER_H. This is typically done to ensure that the header file is only included once in a source file.
  • The int add(int a, int b); is a function declaration. It declares a function named add that takes two int parameters (a and b) and returns an int. Function declarations are used to inform the compiler about the existence and signature of a function before it is used in a source file.

This example demonstrates the basic structure of a header file in C, which typically contains function declarations, type definitions, and macro definitions. It is important to note that the actual implementation of the functions should be placed in a separate source file and linked with the source file that includes the header file during the compilation process.

I hope this explanation helps! Let me know if you have any further questions.