'How to write "#pragma twice" preprocessor in C?
We know there exists #pragma once which prevents source file to be included more than once, but how to make preprocessor (with conditional compilation) which prevents the inclusion of the file in another file more than twice (once and twice is possible, but more than doesn't include).
Solution 1:[1]
something like this?
// so72049366.h file
#ifndef SO72049366_TWICE
#ifndef SO72049366_ONCE
#define SO72049366_ONCE
#else
#define SO72049366_TWICE
#endif
i++;
#endif
// so72049366.c file
#include <stdio.h>
int main(void) {
int i = 0;
#include "so72049366.h"
printf("%d\n", i);
#include "so72049366.h"
printf("%d\n", i);
#include "so72049366.h"
printf("%d\n", i);
}
Output
1
2
2
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
