INS
INS

Reputation: 10820

In C and C++, can I use macros definitions containing space?

I am wondering if macro definitions can contain space. Let's take for example this code:

#define MACRO_PARAM int param2

int function(int param1, MACRO_PARAM)
{
    return param1+param2;
}

This works ok with Visual Studio 8 and gcc 3.4.5 (mingw). For me this is good enough for the moment but is this standard? or can I rely on this behavior across different compilers?

Thanks,

Iulian

PS: To answer to the question why would you wanna do that?: I'm using bison flex for a project and I'm trying to make something reentrant (I need to declare some macros for function parameters).

Upvotes: 3

Views: 5514

Answers (4)

Norman Gray
Norman Gray

Reputation: 12514

Yes, you can certainly have multi word (indeed, multi-line) expansions to preprocessor macros, in any remotely-conformant compiler. C macros are pretty nasty, but if they couldn't even do that, they'd be largely useless.

The preprocessor syntax can do quite a lot (enough that it's easily abused). See section 6.10.3 of the standard, ISO-9899 (PDF), if you want or need legalistic chapter and verse.

Upvotes: 3

Karl von Moor
Karl von Moor

Reputation: 8614

Yes, you can rely on this behaviour.

Upvotes: 1

Daniel Daranas
Daniel Daranas

Reputation: 22624

Yes, you can do that.

Upvotes: 1

Brian Hooper
Brian Hooper

Reputation: 22044

You'll need to end each line of the definition with a \ (except the last).

Upvotes: 6

Related Questions