← Back to C Course | Chapter 14: Additional Topics | Lesson 2 of 9

C Header Files

A header file is a separate file that lists what code offers, like a table of contents. Other files include it to use that code, and guards stop it being included twice.
Syntax
c
#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

c
// 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;
}

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

c
#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

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting include guards, so the header is processed twice and causes redefinition errors.
  2. Defining variables or functions in a header that several files include, causing duplicate definitions.
  3. Using angle brackets instead of quotes for your own headers.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.