Thread: Concatenate with preprocessing function

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    93

    Concatenate with preprocessing function

    Hi guys,

    I am learning how to write macro style fuctions.
    I wrote this simple function in a header file

    Code:
    #ifndef CONCATENATE_H
    #define CONCATENATE_H
    
    
    #define CONCAT_2 (p1,p2)  p1##p2
    
    
    #endif
    and I call the function from main file

    Code:
    #include <stdio.h>
    #include <stdint.h>
    #include "Concatenate.h"
    
    
    int main()
    {
      
        printf("%d",CONCAT_2(10,20));
    
    
        return 0;
    }
    I use Visual Studio Code is a code editor

    When I run the code it returns the following errors and I cannot figure out what I am doing wrong
    Code:
    [Running] cd "c:\Users\n.antoniou\Dropbox\IoT\C_Projects\Macros\" && gcc Concatenate.c -o Concatenate && "c:\Users\n.antoniou\Dropbox\IoT\C_Projects\Macros\"Concatenate
    In file included from Concatenate.c:6:
    Concatenate.c: In function 'main':
    Concatenate.h:4:19: error: 'p1' undeclared (first use in this function)
        4 | #define CONCAT_2 (p1,p2)  p1##p2
          |                   ^~
    Concatenate.c:11:17: note: in expansion of macro 'CONCAT_2'
       11 |     printf("%d",CONCAT_2(10,20));
          |                 ^~~~~~~~
    Concatenate.h:4:19: note: each undeclared identifier is reported only once for each function it appears in
        4 | #define CONCAT_2 (p1,p2)  p1##p2
          |                   ^~
    Concatenate.c:11:17: note: in expansion of macro 'CONCAT_2'
       11 |     printf("%d",CONCAT_2(10,20));
          |                 ^~~~~~~~
    Concatenate.h:4:22: error: 'p2' undeclared (first use in this function)
        4 | #define CONCAT_2 (p1,p2)  p1##p2
          |                      ^~
    Concatenate.c:11:17: note: in expansion of macro 'CONCAT_2'
       11 |     printf("%d",CONCAT_2(10,20));
          |                 ^~~~~~~~
    Concatenate.h:4:27: error: expected ')' before 'p1p2'
        4 | #define CONCAT_2 (p1,p2)  p1##p2
          |                           ^~
    Concatenate.c:11:17: note: in expansion of macro 'CONCAT_2'
       11 |     printf("%d",CONCAT_2(10,20));
          |                 ^~~~~~~~

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    93
    I found the error, there was a space in CONCAT_2 (p1,p2)

  3. #3
    Registered User
    Join Date
    Apr 2021
    Posts
    92
    The preprocessor gets used for some truly weird things. In particular, it is considered "good form" when defining a macro that will be used as part of an (arithmetic) expression to wrap the contents of the macro in parentheses. Thus:

    #define sum (a + b)

    Because of this, the rule for "object-like macros" (like "sum") versus "function-like macros" (like your "CONCAT_2") is very simple: a function-like macro is defined with parens immediately after the name:


    #define sum (a + b) // defines an object-like macro you use like "y = sum * 4;"

    #define sum(a, b) (a + b) // defines a function-like macro you use like "y = sum(m * x, b);"

    Note that this only applies to the definition of the macro. Once the macros are defined, you can use spaces or not: sum (a, 4).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Preprocessing
    By acho.arnold in forum C Programming
    Replies: 6
    Last Post: 06-17-2013, 10:25 AM
  2. Preprocessing #if
    By AnishaKaul in forum C Programming
    Replies: 2
    Last Post: 09-09-2010, 06:24 AM
  3. Preprocessing out function calls
    By PoorLuzer in forum C Programming
    Replies: 3
    Last Post: 05-14-2009, 03:24 AM
  4. preprocessing
    By ganesh bala in forum C Programming
    Replies: 3
    Last Post: 02-18-2009, 03:24 AM
  5. Preprocessing C
    By trem in forum C Programming
    Replies: 3
    Last Post: 04-08-2002, 01:42 PM

Tags for this Thread