0
#include<stdio.h>
#define MAX 0
int main()
{
#ifdef MAX
printf("MAX defined");
#endif
#if defined (MAX)
printf("MAX is defined");
#endif
return 0;
}

Both the #ifdef and #if defined give the same effect then what is the difference between them? I have not seen the disassembly code of these directives if you have seen then kindly try to explain that as well.

Pratik P
  • 47
  • 3
  • 10
  • 1
    functionally, they're equivalent, however, `#if defined()` allows logical operators like `#if defined(A) || defined(B)`, while `#ifdef A || B` is invalid syntax. – Zinki Aug 30 '18 at 06:15
  • https://stackoverflow.com/a/1714255/1632887 – seleciii44 Aug 30 '18 at 06:16
  • Since these are preprocessor directives, there will be no code generated for them. They don't exist at all when the compiler starts compiling. – molbdnilo Aug 30 '18 at 06:21

1 Answers1

3

The difference is historical. Originally there was only #ifdef. The newer syntax is more flexible and allows combining tests with logical conditions, but in the simple form you can use them interchangeably.

Jim Janney
  • 361
  • 1
  • 6