C Header Files
In this page:
#ifndef HEADER_NAME_H
#define HEADER_NAME_H
return_type function_name(parameter_type parameter); // declaration only
#endif
#include "header_name.h" // custom header in the .c file
Standard vs. Custom Header Files
Standard headers, included with angle brackets like #include <stdio.h>, are provided by the compiler's own library and searched for in its system include paths, whereas custom headers, included with double quotes like #include "utils.h", are your own files searched for relative to the current project first.
Example: Standard vs. Custom Header Files
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("stdio.h is a standard header");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Header Guards
A header guard wraps a header file's contents in #ifndef HEADER_NAME, #define HEADER_NAME, and a closing #endif, ensuring the preprocessor skips the file's contents entirely if it has already been included once in the current translation unit, preventing duplicate-definition compile errors.
Example: Header Guards
#ifndef MYHEADER_H
// Define the macro MYHEADER_H (replaced by the preprocessor)
#define MYHEADER_H
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("Guarded against duplicate inclusion");
// Return 0 to signal that the program finished successfully
return 0;
}
#endif
Login to try C/C++/Java/PHP code in the editor
Declaring Functions in Headers
A header file should contain declarations — function prototypes, type definitions, macro constants — describing what exists, not the actual executable code that implements those functions; the implementations belong in a corresponding .c source file instead.
Example: Declaring Functions in Headers
// Include standard input/output (printf, scanf)
#include <stdio.h>
int add(int a, int b);
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d", add(2, 3));
// Return 0 to signal that the program finished successfully
return 0;
}
int add(int a, int b) {
// Send a + b back to the caller
return a + b;
}
Login to try C/C++/Java/PHP code in the editor
Variables and Macros in Headers
Declaring shared constants and macros in a header lets every source file that includes it use the exact same values consistently, so changing a shared setting in one place (the header) automatically updates every file that depends on it, instead of hunting down scattered duplicate definitions.
Example: Variables and Macros in Headers
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define the macro MAX_USERS (replaced by the preprocessor)
#define MAX_USERS 100
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d", MAX_USERS);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Preventing Duplicate Definitions
If a header contains an actual function body rather than just a prototype, and that header gets included into more than one .c file, the linker sees the same function defined twice and reports a duplicate-symbol error — which is exactly the failure header guards and declaration-only headers are designed to avoid.
Example: Preventing Duplicate Definitions
// Include standard input/output (printf, scanf)
#include <stdio.h>
int square(int n) {
// Send n * n back to the caller
return n * n;
}
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d", square(4));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Forgetting include guards, so the header is processed twice and causes redefinition errors.
- Defining variables or functions in a header that several files include, causing duplicate definitions.
- Using angle brackets instead of quotes for your own headers.
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: