Gaara
Gaara

Reputation: 697

Error using "#" in macro

I am trying to clean up an existing code and I did format the below Macro from

#define v(x) { if (!is_visited(n->line) && ANNOTATE_SOURCE) { visit(n->line); sprintf(buffer, "#\n# LINE %d: %s#\n", n->line, get_line(n->line)); program = emit(program, buffer); }}

to

#define v(x) {\
               if (!is_visited(n->line) && ANNOTATE_SOURCE)\ 
               {\ 
                  visit(n->line);\
                  sprintf(buffer, "#\n# LINE %d: %s#\n", n->line,get_line(n->line));\ 
                  program = emit(program, buffer);\
                }\
             }\

and I got error: '#' is not followed by a macro parameter

I searched through the forums and I could not understand why this error occurs though I am not trying to use any Macro within any other Macro.

Edit:

I tried to do sprintf(buffer, "\#\n\# LINE %d: %s\#\n", n->line,get_line(n->line)); (Adding escape character to #. still the same error persists)

Upvotes: 2

Views: 83

Answers (2)

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53026

Your macro has spaces after the continuation character \, that's not allowed

#define v(x) do {                                                             \
        if (!is_visited(n->line) && ANNOTATE_SOURCE)                          \
        {                                                                     \
          visit(n->line);                                                     \
          sprintf(buffer, "#\n# LINE %d: %s#\n", n->line, get_line(n->line)); \
          program = emit(program, buffer);                                    \
        }                                                                     \
    } while (0)

As you can see above, I also used a do {} while (0) loop that doesn't loop to allow using a ; after the macro invocation,

v(a);

is now valid.

Upvotes: 2

ouah
ouah

Reputation: 145899

You have to ensure \ is the last character of your line before the new-line character. This is not the case in your program: there are whitespaces after \.

Upvotes: 3

Related Questions