Advertisement
elvanderb

for / else construction in C

Apr 19th, 2018
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define CREATE_LABEL2(A, B) A ## B
  5. #define CREATE_LABEL(A, B) CREATE_LABEL2(A, B)
  6. #define for_else(A) \
  7.     for A \
  8.     { \
  9.         switch (0) \
  10.         { \
  11.             case 0:
  12.  
  13.  
  14. #define else_for \
  15.             default: \
  16.                 goto CREATE_LABEL(__for_else_no_break_, __LINE__); \
  17.         } \
  18.         goto CREATE_LABEL(__for_else_break_, __LINE__); \
  19.         CREATE_LABEL(__for_else_no_break_, __LINE__): \
  20.         ;\
  21.     } \
  22.     if (0) \
  23.     { \
  24.         CREATE_LABEL(__for_else_break_, __LINE__): \
  25.         ; \
  26.     } \
  27.     else
  28.  
  29. int main(int argc, char **argv)
  30. {
  31.     if (argc < 2)
  32.     {
  33.         fprintf(stderr, "invalid argument\n");
  34.         return 1;
  35.     }
  36.  
  37.     for_else ((int i = 0; argv[1][i] != 0; i++))
  38.     {
  39.         for_else ((volatile int j = 0; j <= i; j++))
  40.             if (j > 5)
  41.             {
  42.                 printf ("\n");
  43.                 break;
  44.             }
  45.             // /!\ this statement will be executed in the for_else loop ! better always use {}...
  46.             printf("%d ", j);
  47.         else_for
  48.             printf("\n%d <= 5!\n", i);
  49.  
  50.         if (argv[1][i] == '!')
  51.         {
  52.             printf("'!' detected!\n");
  53.             break;
  54.         }
  55.     }
  56.     else_for
  57.     {
  58.         fprintf(stderr, "invalid argument\n");
  59.         return 1;
  60.     }
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement