Reputation: 55
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).
Upvotes: 1
Views: 933
Reputation: 108978
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
Upvotes: 5