Is there a difference between #ifndef FOO_INCLUDED_H and #ifndef FOO_H

I've noticed a couple of tutorials using include guards like:

 
  #ifndef FOO_INCLUDED_H 


and others like:


 
  #ifndef FOO_H 


...is there a difference between those two?
It doesn't matter which name you use as long as it's unique.
There's no difference. It's only about making a longer name to avoid potential conflicts.

To me I always use filename (most of time class name) as the include guard name.

For example I have a file called MyClass.h, I would use include guard like this:

1
2
3
4
5
6
#ifndef __MY_CLASS_H__
#define __MY_CLASS_H__

// all the staffs

#endif 

Got it :) Thanks guys.
closed account (E0p9LyTq)
There is another commonly used, non-standard though, #include guard: #pragma once .

http://stackoverflow.com/questions/787533/is-pragma-once-a-safe-include-guard
@liuyang
Double-underscores are reserved for the language implementations to use. I would strongly advise you not to use them.

More info: http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier
Last edited on
pragma once vs include guards
All modern compilers understand the include guards and treat them with the same optimization as pragma once.

That said, if I expect my code to be most compatible I will use both #pragma and #include.

1
2
3
#pragma once
#ifndef DUTHOMHAS_MYFILENAME_HPP
#define DUTHOMHAS_MYFILENAME_HPP 

From 2012: http://stackoverflow.com/questions/13339392/should-i-still-use-include-guards-and-pragma-once

naming pattern
My personal pattern is: my personal tag (DĂșthomhas) + the simple filename + file type.
This proactively prevents conflicts with similarly-named libraries I may use from other people's projects.

If you wish, you may also salt the name with the date-time. I'll do this if I am developing a library: DUTHOMHAS_201608011244_MYFILENAME_HPP, for example. This prevents mix-ups between versions of the same library... But that is totally unnecessary most of the time.

underscores 
Any leading underscore in global/file-level view is reserved for the compiler implementation to use.

So, unless you are writing a compiler's library, avoid leading and trailing underscores.

This is by design in order to to prevent collisions: the compiler's library naming conventions are different than user library naming conventions. My "list" library won't collide with the <list> library.

And while I'm at it, I should also wrap all my stuff in a namespace. Again, my personal namespace is "duthomhas"; chances are if you find anything using that namespace it was written by me [or some loser poser stealing my stuff].

Hope this helps.
Thanks @booradley60 for the info. Though I haven't seems any issue so far, it is a very useful suggestion!
Topic archived. No new replies allowed.