# [SOLVED]if-else loop statement error

Status
Not open for further replies.

#### shaswat

I works on Keil uVision 4 compiler and I found an error during writing my code. I stucked there more than an hour but I didn't resolve the problem. After several Hit and trial I resolve my problem but still I don't know what was the problem. I debug the code in same format in my code-block and found same error as in Keil compiler.
Here is the piece of code....

Code:
#define not()   printf("if\n");
#define yes()   printf("else\n");

int main()
{
uint16_t  no ;
if(no)
//{
not();
// }

else
// {
yes();
//}

return 4;
}

error: 'else' without a previous 'if'| ( in my code block)

I don't understand why its not working as after if statement ,there is only one instruction so I don't add braces. In that case it shows error. But if I add braces in my statements it works good. Can anybody tell the reason that why adding braces runs the code?

Any new idea would be really appreciated...

#### vhn

##### Member level 4
Hi, The answer is in the code itself. When the macro expands, the line will read printf statement followed by two semicolons. So the compiler sees effectively more than one "line" of code. You can avoid this by removing the semicolon either at the end of macro definition or at the end of macro invocation. Although I don't know what is driving you to write this piece of code, it is always a good practice to use braces along with control statements.

shaswat

### shaswat

Points: 2

#### shaswat

Hi, The answer is in the code itself. When the macro expands, the line will read printf statement followed by two semicolons. So the compiler sees effectively more than one "line" of code. You can avoid this by removing the semicolon either at the end of macro definition or at the end of macro invocation. Although I don't know what is driving you to write this piece of code, it is always a good practice to use braces along with control statements.

Actually I called only one macro statement inside the if statement, so I don't required to put the braces in between my statement . However if I used more than one then I will definitely used braces. Well, I think this one helps me, as today I learn a important point ..
One thumps up for the fast replying.....

Status
Not open for further replies.