[Solved] issue with understanding macros in C programming


Macros (#define MACRO ...) are processed by the C preprocessor before the actuel compile process takes place.

So the compiler only “sees” this once the file has been preprocessed:

int main()
{
  // your code goes here
  int k = 0;

  scanf("%d", &k);
  switch (k)
  {
  case 1:
    break;
  case 2:
    break;
  case 3:
    break;
  case 4:
    break;
  }

  printf("typed first_case\n");
  printf("typed second_case\n");
  printf("typed third_case\n");
  printf("typed fourth_case\n");
}

You could write your program like this, and the outcome would be exactly the same:

#define first_case
#define second_case
#define third_case
#define fourth_case

/* Find the longest line among the giving inputs and print it */

#include <stdio.h>
int main()
{
  // your code goes here
  int k = 0;

  scanf("%d", &k);
  switch (k)
  {
  case 1:
    printf("case 1\n");
    break;
  case 2:
    break;
  case 3:
    break;
  case 4:
    break;
  }


#ifdef first_case
  printf("typed first_case\n");
#endif
#ifdef second_case
  printf("typed second_case\n");
#endif

#ifdef third_case
  printf("typed third_case\n");
#endif
#ifdef fourth_case
  printf("typed fourth_case\n");
#endif

}

solved issue with understanding macros in C programming