Home > Back-end >  How to write "#pragma twice" preprocessor in C?
How to write "#pragma twice" preprocessor in C?

Time:04-29

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).

CodePudding user response:

something like this?

// so72049366.h file
#ifndef SO72049366_TWICE

#ifdef SO72049366_ONCE
#define SO72049366_TWICE
#endif

#ifndef SO72049366_ONCE
#define SO72049366_ONCE
#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
  •  Tags:  
  • c
  • Related