Indent preprocessor instructions in C

I am writing a program in C that must act differently based on a definition or not a symbolic constant. For a simple example, my first thought was to write this as follows:

#include <stdio.h>
#define TEST

int main(void) {
   int num;
   #ifdef TEST
      num=1;
      printf("YES\n");
   #else
      num=0;
      printf("NO\n");
   #endif
   printf("%d\n", num);
   return 0;
}

However, when trying to auto-render my code (in particular, using gg=Gin vim) my indentation was lost:

#include <stdio.h>
#define TEST

int main(void) {
   int num;
   #ifdef TEST
   num=1;
   printf("YES\n");
   #else
   num=0;
   printf("NO\n");
   #endif
   printf("%d\n", num);
   return 0;
}

Of course, if I try to auto-delay something like the following (with the actual commands in between), the following occurs:

#ifdef TEST1
   commands
   #ifdef TEST2
      commands
   #else
      #ifdef TEST3
         commands
      #endif
      commands
   #endif
#endif

So, is there anything I can do to have the indentation indicated above by default?

+4
source share
1 answer

: , , , " " , , ( , ).

: C , . , . , , - - , , , (.. ()) :

    if (conditional1)
        action1();
#if compileoption1
        else if (conditional2)
            action2();
#else
        action3();
#endif

#if compileoption1
    if (conditional1)
#endif
    action1();

. , C C (, , , ....), - . , - . . : https://www.kernel.org/doc/Documentation/CodingStyle

20 .

+1

Source: https://habr.com/ru/post/1622942/


All Articles