0
#ifndef api_H_
#define api_H_
...
#endif

In above code it's checking that if file is already included or not to avoid multiple inclusions.

Question:

I want to know that if there is any better way to do so and what are the alternatives of it and if this is the better way of doing it then why it is so?

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Nilesh Pal
  • 23
  • 7
  • See also [Should I use `#include` in headers?](https://stackoverflow.com/questions/1804486/should-i-use-include-in-headers/) and (much more recent) [Understanding different styles of `#define` in C](https://stackoverflow.com/questions/57729743) and [Why doesn't the compiler automatically add or generate an include guard?](https://stackoverflow.com/questions/31911681) and [What is a good reference documenting patterns of use of `.h` files in C?](https://stackoverflow.com/questions/25627) (which now might not be an acceptable question), and a myriad other questions. Search `[c] include guard`. – Jonathan Leffler Sep 04 '19 at 16:19
  • For those curious, C++ is attempting to solve this problem (and more) with [modules](https://www.modernescpp.com/index.php/c-20-modules). If something like that will ever come to C is an open question. – Some programmer dude Sep 04 '19 at 16:43

1 Answers1

0

With most (if not all) modern compilers you can use #pragma once which does the same job, but which is possibly faster.

You also eliminate the risk of name collisions, which can happen if two different header files contain the same preprocessor symbol (api_H_).

Basically instead of:

#ifndef api_H_
#define api_H_

... header content

#endif

you write:

#pragma once

... header content

Also read #pragma once vs include guards? and Is #pragma once a safe include guard?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • I still don't see a reason why `#pragma once` would be faster. – melpomene Sep 04 '19 at 15:19
  • @melpomene I wrote "possibly" – Jabberwocky Sep 04 '19 at 15:19
  • Yeah, but why? Why not write "possibly slower"? You have no justification either way. – melpomene Sep 04 '19 at 15:19
  • 1
    @melpomene just found [this interesting answer](https://stackoverflow.com/a/39583019/898348) – Jabberwocky Sep 04 '19 at 15:23
  • One could quibble that your interesting answer is for C++, @Jabberwocky, but that's not entirely fair. They're near enough the same in C. – Jonathan Leffler Sep 04 '19 at 16:10
  • 1
    The reason why `#pragma once` might be faster is that one encountering a second `#include "header.h"` (that contained `#pragma once`), the preprocessor doesn't have to reopen the file and read to the `#ifndef HEADER_H_INCLUDED` line and then skip to the matching `#endif` and check whether there's anything else in the file, etc. However, GCC practically optimizes the classic include guards so that there's very little difference in cost between `#pragma once`. – Jonathan Leffler Sep 04 '19 at 16:22
  • Pragma once may fail, and all modern compilers essentially do it anyway, without the need for the directive. Include guards cannot fail. (Though ODR violations may still exist.) Always use include guards. – Dúthomhas Sep 04 '19 at 17:28
  • can you tell me in which case "#pragma once" can fail – Nilesh Pal Sep 05 '19 at 12:01
  • @NileshPal read [this answer](https://stackoverflow.com/a/34884735/898348) (from the links at the end of my answer) – Jabberwocky Sep 05 '19 at 12:08
  • can you please tell me that how name collisions can happen? – Nilesh Pal Sep 05 '19 at 12:32
  • @NileshPal they can't happen with `#pragma once`. But they can happen with header guards if you have two different header files using buth the same preprocessor symbol, for example: file1.h contains `#ifndef api_H_`, file2.h contains also `#ifndef api_H_` and you include both file1.h and file2.h into the same .c file. Then file2.h will basically be skipped. – Jabberwocky Sep 05 '19 at 12:34
  • But why would I check the inclusion of my header file in other header file cant i check it in file itself like cant i do like this in api.h i have first check **`#ifndef api_H_`** – Nilesh Pal Sep 05 '19 at 14:55
  • @NileshPal Well you _should_ not, but you _could_ do it my mistake, especially when using careless copy/pasting. If it's still not clear, then post another specific question. – Jabberwocky Sep 05 '19 at 15:06
  • It is both possible for a file system to have distinct paths to the same file, as is the (more likely and sadly common in large projects) possibility that a file is exists in multiple locations in the project tree. #pragma once will fail bothering of these. – Dúthomhas Sep 06 '19 at 18:29
  • 1
    Anyone naming include guards highly-collisionable names (like _api_H_) deserve to get chewed out in a code review, especially for completely misunderstanding the purpose of an include guard. – Dúthomhas Sep 06 '19 at 18:31