Consider the C code below.
#include <stdio.h>
#define foo(x) if (x>0) printf("Ouch\n");
int main()
{
int a = 4;
int b = -3;
if(a>b)
foo(b) ;
else
printf("Arrg!\n");
printf("thanks\n");
return 0;
}
When the program is run, I get an error saying error: ‘else’ without a previous ‘if’
. When we replace foo(b)
with if (b>0) printf("Ouch\n")
according to the macro definition, shouldn't the program transfer into the following code when written with braces?
if(a>b){
if(x>0){
printf("Ouch\n");
}
}
else{
printf("Arrh!\n");
}
printf("thanks\n");
I don't see why the compiler complains. What does the program actually get transferred to?
Thanks