Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions 3_quick_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,12 @@

### 3.3.2 Include Files

* Every header file must have a '`#ifndef FILE_NAME_H_`' and '`#endif`' guard
surrounding all code. See "Include Files".
* Every header file must have a `#pragma once` before any other directives,
declarations or definitions. See "Include Files".

+ The `#ifndef` must be the first line of code following the file header
comment.

+ The `#endif` must appear alone on the last line in the file.
+ An exception is granted to files that are specially generated by tools or
copied directly from an external project which utilize an include guard
instead.

* All C include files shall use the same extension and it shall be `.h`. See
"Include Files".
Expand Down
24 changes: 1 addition & 23 deletions 4_naming_conventions/44_identifiers.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,29 +178,7 @@ The underscore effectively separates the words, making names more readable.
typedef UINT32 THIS_IS_AN_EXAMPLE_OF_WHAT_TO_DO_FOR_PCI;
```

#### 4.4.5.4 The names of guard macros shall end with an underscore character.

The guard macro, used in the `#ifndef` at the start of an include file, uses a
postfix underscore character '`_`', in its name in order to prevent collision
with other names that follow the naming convention. This may not be sufficient
for header files that don't have a unique name. In that case, additional text
may have to be added to the macro name in order to make it unique. This may not
be required if the header files are mutually exclusive.

```c
#ifndef FILE_NAME_H_
#define FILE_NAME_H_
...
#if (A_NUMBER > 72)
...
#else // NOT (A_NUMBER > 72)
...
#endif // (A_NUMBER > 72)
...
#endif /* FILE_NAME_H_ */
```

#### 4.4.5.5 The #else and #endif clauses of conditional compilation blocks shall be commented to identify their context.
#### 4.4.5.4 The #else and #endif clauses of conditional compilation blocks shall be commented to identify their context.

If a conditional compilation construct spans more than seven lines, a comment
shall be added to the construct's `#else` and `#endif` clauses identifying the
Expand Down
34 changes: 14 additions & 20 deletions 5_source_files/53_include_files.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,38 +92,32 @@ file did not use the changed header file.
This maintains proper dependency relationships between the files and allows the
header file to be used without prior knowledge of its dependencies.

### 5.3.5 All include file contents must be protected by a #include guard.
### 5.3.5 All include file contents must be protected by #pragma once.

Also known as a "macro guard", use #include guards to avoid multiple inclusions
when dealing with the include directive. Adding `#include` guards to a header
file makes that file idempotent.
`#pragma once` is a widely supported preprocessor directive that serves the same purpose
as include guards: preventing multiple inclusions of the same header file.
Unlike include guards, it is more concise, less error‑prone, and does not require defining
a unique macro name.

```c
#ifndef FILE_NAME_H_
#define FILE_NAME_H_
...
#endif // FILE_NAME_H_
#pragma once
```

Avoid duplicating the guard macro name in different header files. Including a
duplicate guard macro name prevents the symbols in the other from being
defined. Names starting with one or two underscores, such as
`_MACRO_GUARD_FILE_NAME_H_`, must not be used. They are reserved for compiler
implementation.

With modern programming practices, particularly include files including other
include files, it is almost impossible to avoid including the same file more
than once. This can only slow down the processing time and may cause difficult
to diagnose issues. To avoid this, the use of _macro guards_ is required in all
to diagnose issues. To avoid this, the use of _pragma once_ is required in all
include files to protect against inadvertent multiple inclusion.

* The `#ifndef` shall be on the first line following the file header comment.
* `#pragma once` shall be on the first line following the file header comment.
This location ensures that all code is contained.

* The `#endif` shall appear as the last line in the file. The `#endif` is
followed by a comment consisting solely of the guard token. The line shall end
with a carriage return (new line) as the last thing in the file, thus ensuring
that all code is contained.
> Note that unlike macro-based include guards, `#pragma once` does not require
> any changes at the end of the file, such as an `#endif` directive.

Some files in the codebase specially generated by tools or copied directly from
an external project may not use `#pragma once`. These files are exempted from
this rule.

### 5.3.6 Include files shall contain only public or only private data.

Expand Down