Angus
Angus

Reputation: 12631

Nested macros in C

#include<stdio.h>
#define A(int x) printf("%d\n",x)
#define AS(A) A(20)
typedef struct{
 int *m;
 int n;
 int k;
}st;
//static st sb[10] = {AS(A)}
int main()
{
    AS(A);
    return 0;
}

I'm getting an error as below .

Line 14: error: macro parameters must be comma-separated

Please help.

Upvotes: 1

Views: 4443

Answers (3)

COD3BOY
COD3BOY

Reputation: 12112

You dont actually need this : #define A(int x) printf("%d\n",x)

but you need : #define A(x) printf("%d\n",x) , you don't actually need to declare a variable in preprocessor! ,

Note that : The preprocessor does not know anything about keywords.

Upvotes: 2

Sandeep Pathak
Sandeep Pathak

Reputation: 10767

In C, macro parameters aren't typed. It's all symbol substitution . Try this :

#include<stdio.h>
#define A(x) printf("%d\n",x) /*Remove the type */
#define AS(A) A(20)
int main()
{
    AS(A);
    return 0;
}

see codepad

Upvotes: 3

Kiril Kirov
Kiril Kirov

Reputation: 38183

This has nothing to do with nesting macros. The problem is

#define A(int x) printf("%d\n",x)

you must remove the int part. Like this:

#define A(x) printf("%d\n",x)

If you leave the int, the preprocessor interprets it as another parameter, that's why it tells you

Line 14: error: macro parameters must be comma-separated

because expects:

#define A(int,x) printf("%d\n",x)

Upvotes: 7

Related Questions