Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't this swap macro compile?

Tags:

c

The K&R 2nd ed answer book has the following solution for a variable argument swap macro

#define swap(t, x, y) { t _z; \   
                       _z = y; \ 
                       y = x; \  
                       x = _z; } 

Visual Studio Express is telling me that y and x must be constant values and that there is an "expected declaration" to the left of the right brace.

Is this formatting out of date?

Edit:

The way that code is formatted won't let me compile for the errors listed above, but the following code seems to be fine:

#define swap(t, x, y) { t _z; _z = y; y = x; x = _z; }
like image 328
Spellbinder2050 Avatar asked Dec 15 '22 20:12

Spellbinder2050


1 Answers

The problem is you have whitespace after the \ line continuation characters. As such, the \ is no longer treated as a line continuation, but just a non-whitespace character. This means the following lines are not part of the macro definition, and the compiler tries to compile them as normal lines. Getting rid of the whitespace after each \ will fix the error.

like image 84
Drew McGowen Avatar answered Jan 03 '23 13:01

Drew McGowen