Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] if-else loop statement error

Status
Not open for further replies.

shaswat

Advanced Member level 4
Joined
Jul 2, 2013
Messages
115
Helped
1
Reputation
2
Reaction score
1
Trophy points
18
Activity points
963
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...
 

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.
 
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.

Thanks for the answer
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.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top