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));
| ^~~~~~~~