title: C Preprocessor conditional with extended macros and typecast
body: <p>For a microcontroller, I use macros for the HAL. Now, to generalise the use of the HAL, I want to do something like</p>
  
  <pre><code>#define UART  UART1
  #if UART==UART1
  # define PIN_TX 9
  #elif UART==UART2
  # define PIN_TX 2
  #else
  # warning "UART not correctly defined"
  #endif
  </code></pre>
  
  <p>However, UART1 is a memory address with a type cast (e.x. <code>(uint8_t*)0x004000000</code>). So the compiler prints some errors.</p>
  
  <p>I made a simple example:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  #define v1 (double)1
  #define v2 (double)2
  
  int main(int argc, char *argv[])
  {
    printf("We have: ");
  
  #define VAL (v1)
  
  #if VAL==v1
    printf("VAL is 1\n");
  #elif VAL==v2
    printf("VAL is 2\n");
  #else
  # warning "VAL not 1 or 2"
    printf("Not defined\n");
  #endif
  }
  </code></pre>
  
  <p>Which also fails to compile with gcc with the following comments:</p>
  
  <pre><code>cc     preproc.c   -o preproc
  preproc.c: In function ‘main’:
  preproc.c:3:20: error: missing binary operator before token "1"
   #define v1 (double)1
                      ^
  preproc.c:10:14: note: in expansion of macro ‘v1’
   #define VAL (v1)
                ^~
  preproc.c:12:5: note: in expansion of macro ‘VAL’
   #if VAL==v1
       ^~~
  preproc.c:3:20: error: missing binary operator before token "1"
   #define v1 (double)1
                      ^
  preproc.c:10:14: note: in expansion of macro ‘v1’
   #define VAL (v1)
                ^~
  preproc.c:14:7: note: in expansion of macro ‘VAL’
   #elif VAL==v2
         ^~~
  preproc.c:17:2: warning: #warning "VAL not 1 or 2" [-Wcpp]
   #warning "VAL not 1 or 2"
    ^~~~~~~
  &lt;builtin&gt;: recipe for target 'preproc' failed
  make: *** [preproc] Error 1
  </code></pre>
  
  <p>However, if I remove the (double) in the definition of the v1 and v2, it compiles and runs as expected.</p>
  
  <hr>
  
  <p>Note that as an alternative solution I made</p>
  
  <pre><code>#define USE_UART1
  //#define USE_UART2
  
  #if defined(USE_UART1)
  # define UART UART1
  # define PIN_TX 9
  #elif defined(USE_UART2)
  # define UART UART2
  # define PIN_TX 2
  #else
  # warning "UART not correctly defined"
  #endif
  </code></pre>
  
  <p>But that involves another variable [edit: technically a macro, but practially another bunch of characters that I have to keep track of].</p>
  
  <p>I was curious to know the rationale behind that compilation error and/or how to solve it, if possible.</p>