pragma-once
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| pragma-once [February 19, 2026 at 23:01] – yanevskiv | pragma-once [May 14, 2026 at 11:38] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | # #pragma once | ||
| + | In C and C++ programming languages `#pragma once` is a non-standard preprocessor directive that is equivalent to [[header-guard|header guards]]. | ||
| + | You just put `#pragma once` at the top, rather than wrapping the entire header within header guards. | ||
| + | |||
| + | Header guards: | ||
| + | ```cpp | ||
| + | // image.h | ||
| + | #ifndef __IMAGE_H__ | ||
| + | #define __IMAGE_H__ | ||
| + | |||
| + | struct Image { | ||
| + | ... | ||
| + | }; | ||
| + | |||
| + | #endif /* __IMAGE_H__ */ | ||
| + | ``` | ||
| + | |||
| + | Pragma once: | ||
| + | |||
| + | ```cpp | ||
| + | // image.h | ||
| + | #pragma once | ||
| + | |||
| + | struct Image { | ||
| + | ... | ||
| + | }; | ||
| + | ``` | ||
| + | |||
| + | ## Should I use #pragma once? | ||
| + | It's up to you! It's widely supported by almost all C and C++ compilers -- even though it's not technically defined by the standard in either programming language. | ||
| + | |||
| + | An advantage is that there is less chance you will make a typo like this: | ||
| + | ```c | ||
| + | #ifndef __HEADER_H_ | ||
| + | #define __HEADER_H__ | ||
| + | |||
| + | /* ... */ | ||
| + | |||
| + | #endif | ||
| + | ``` | ||
| + | |||
| + | There is no good reason not to use `#pragma once` although my preferences still rather lean towards header guards simply because I feel most C and C++ programmers will find them more familiar. | ||
| + | |||
| + | ## What is #pragma? | ||
| + | `#pragma` is a preprocessor directive that instructs the compiler to employ some non-standard behavior -- beyond the language itself. `#pragma` directive itself is part of the C standard, but what exactly `#pragma` *does* is up to the compilers. | ||
| + | |||
| + | ### GCC example | ||
| + | As an example -- if you use the [[gcc|GCC]] compiler -- you can use `#pragma GCC poison` in order to ban (or " | ||
| + | |||
| + | Well, maybe gets() is a bad example because GCC already bans it and tells you to use fgets(). ;) | ||
| + | |||
| + | You can read more about GCC `#pragma` directive [here](https:// | ||
| + | ```c | ||
| + | /* | ||
| + | Compile: gcc main.c | ||
| + | Error: | ||
| + | |||
| + | main.c: In function ‘main’: | ||
| + | main.c: | ||
| + | 24 | | ||
| + | | ^ | ||
| + | main.c: | ||
| + | 19 | #pragma GCC poison gets | ||
| + | | ^~~~ | ||
| + | main.c: | ||
| + | 24 | | ||
| + | | ^~~~ | ||
| + | | fgets | ||
| + | */ | ||
| + | |||
| + | #include < | ||
| + | #pragma GCC poison gets | ||
| + | |||
| + | int main() | ||
| + | { | ||
| + | char str[BUFSIZ]; | ||
| + | gets(str); | ||
| + | puts(str); | ||
| + | return 0; | ||
| + | } | ||
| + | ``` | ||
| + | |||
| + | ### OpenMP example | ||
| + | A multithreading library [[openmp|OpenMP]] uses `#pragma` extensively directive extensively! | ||
| + | |||
| + | All OpenMP directives start with `#pragma omp`. | ||
| + | |||
| + | The following example creates two competing threads -- one that prints a red letter ' | ||
| + | |||
| + | You have to remember to compile the source code with `-fopenmp` option. | ||
| + | ```c | ||
| + | /* | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | */ | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #define RED " | ||
| + | #define GREEN " | ||
| + | #define NONE " | ||
| + | |||
| + | #define A_STR RED " | ||
| + | #define B_STR GREEN " | ||
| + | |||
| + | int main() | ||
| + | { | ||
| + | #pragma omp parallel num_threads(2) | ||
| + | { | ||
| + | int tid = omp_get_thread_num(); | ||
| + | for ( ; ; ) { | ||
| + | #pragma omp crticial | ||
| + | { | ||
| + | printf(" | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | return 0; | ||
| + | } | ||
| + | ``` | ||
| + | |||
| + | |||
| + | ## Links | ||
| + | |||
| + | - https:// | ||
| + | - https:// | ||
