In C and C++, why is each .h file usually surrounded with #ifndef #define #endif directives?

c, c++, c-preprocessor, include-guards

Solution

It's a so-called "include guard". The purpose is to prevent the file from having to be parsed multiple times if it is included multiple times.

Problem

Why does each .h file starts with #ifndef #define #endif? We can certainly compile the program without those directives.

Original source