Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why put a random number on the first line in header file?

Tags:

c++

c

I have seen this countless of times. Instead of

#ifndef _common_h_
#define _common_h_

#endif /* _common_h_ */

people sometimes define their header files in following format:

#ifndef _common_h__479124197491641974591
#define _common_h__479124197491641974591

#endif /* _common_h__479124197491641974591 */

What is that random number good for? I just couldn't successfully google any answer to this.

like image 294
Saraph Avatar asked Jun 11 '14 17:06

Saraph


1 Answers

The idea is to make it harder to trip your include guard unintentionally, and/or to avoid triggering someone elses' include guards.

For example, if you are writing a library, and you have a Common.h header, then using _common_h_ for the guard could prevent users of your library from having _common_h_ guards in their own libraries, which is not ideal. Adding a random number makes such collisions nearly impossible.

like image 184
Sergey Kalinichenko Avatar answered Sep 28 '22 15:09

Sergey Kalinichenko