* parse error
@ 2004-09-26 14:32 Ankit Jain
2004-09-25 6:29 ` Elias Athanasopoulos
0 siblings, 1 reply; 5+ messages in thread
From: Ankit Jain @ 2004-09-26 14:32 UTC (permalink / raw)
To: linux prg
hi
i could not understand why compiler has put semi colon
before else due t owhich this parse error comes....
thanks
#define exch(x,y) { int tmp; tmp=x; x=y; y=tmp; }
However that wouldn't work in some cases. The
following code is meant to be an if-statement with two
branches:
if(x>y)
exch(x,y); // Branch 1
else
do_something(); // Branch 2
But it would be interpreted as an if-statement with
only one branch:
if(x>y) { // Single-branch
if-statement!!!
int tmp; // The one and only branch
consists
tmp = x; // of the block.
x = y;
y = tmp;
}
; // empty statement
else // ERROR!!! "parse error
before else"
do_something();
________________________________________________________________________
Yahoo! Messenger - Communicate instantly..."Ping"
your friends today! Download Messenger Now
http://uk.messenger.yahoo.com/download/index.html
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: parse error
2004-09-26 14:32 parse error Ankit Jain
@ 2004-09-25 6:29 ` Elias Athanasopoulos
2004-09-28 6:14 ` Jan-Benedict Glaw
0 siblings, 1 reply; 5+ messages in thread
From: Elias Athanasopoulos @ 2004-09-25 6:29 UTC (permalink / raw)
To: Ankit Jain; +Cc: linux prg
On Sun, Sep 26, 2004 at 03:32:52PM +0100, Ankit Jain wrote:
> hi
>
> i could not understand why compiler has put semi colon
> before else due t owhich this parse error comes....
>
> thanks
>
>
> #define exch(x,y) { int tmp; tmp=x; x=y; y=tmp; }
#define exch(x,y) do { int tmp; tmp=x; x=y; y=tmp; } while(0);
Regards,
--
University of Athens I bet the human brain
Physics Department is a kludge --Marvin Minsky
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: parse error
2004-09-25 6:29 ` Elias Athanasopoulos
@ 2004-09-28 6:14 ` Jan-Benedict Glaw
2004-09-26 22:51 ` Elias Athanasopoulos
0 siblings, 1 reply; 5+ messages in thread
From: Jan-Benedict Glaw @ 2004-09-28 6:14 UTC (permalink / raw)
To: linux prg
[-- Attachment #1: Type: text/plain, Size: 889 bytes --]
On Sat, 2004-09-25 09:29:01 +0300, Elias Athanasopoulos <elathan@phys.uoa.gr>
wrote in message <20040925062900.GC1997@velka.phys.uoa.gr>:
> On Sun, Sep 26, 2004 at 03:32:52PM +0100, Ankit Jain wrote:
> > hi
> >
> > i could not understand why compiler has put semi colon
> > before else due t owhich this parse error comes....
> >
> > thanks
> >
> >
> > #define exch(x,y) { int tmp; tmp=x; x=y; y=tmp; }
>
> #define exch(x,y) do { int tmp; tmp=x; x=y; y=tmp; } while(0);
Nearly:-) Omit the last semicolon to make it work...
MfG, JBG
--
Jan-Benedict Glaw jbglaw@lug-owl.de . +49-172-7608481 _ O _
"Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | Gegen Krieg _ _ O
fuer einen Freien Staat voll Freier Bürger" | im Internet! | im Irak! O O O
ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: parse error
2004-09-28 6:14 ` Jan-Benedict Glaw
@ 2004-09-26 22:51 ` Elias Athanasopoulos
0 siblings, 0 replies; 5+ messages in thread
From: Elias Athanasopoulos @ 2004-09-26 22:51 UTC (permalink / raw)
To: linux prg
On Tue, Sep 28, 2004 at 08:14:51AM +0200, Jan-Benedict Glaw wrote:
> On Sat, 2004-09-25 09:29:01 +0300, Elias Athanasopoulos <elathan@phys.uoa.gr>
> wrote in message <20040925062900.GC1997@velka.phys.uoa.gr>:
> > On Sun, Sep 26, 2004 at 03:32:52PM +0100, Ankit Jain wrote:
> > > hi
> > >
> > > i could not understand why compiler has put semi colon
> > > before else due t owhich this parse error comes....
> > >
> > > thanks
> > >
> > > #define exch(x,y) { int tmp; tmp=x; x=y; y=tmp; }
> >
> > #define exch(x,y) do { int tmp; tmp=x; x=y; y=tmp; } while(0);
>
> Nearly:-) Omit the last semicolon to make it work...
Right. :-) Actually after a lot of C Coding it's too difficult to
develop the habit to omit a last semicolon in one line of code.
Regards,
--
University of Athens I bet the human brain
Physics Department is a kludge --Marvin Minsky
^ permalink raw reply [flat|nested] 5+ messages in thread
* RE: parse error
@ 2004-09-27 15:58 Huber, George K RDECOM CERDEC STCD SRI
0 siblings, 0 replies; 5+ messages in thread
From: Huber, George K RDECOM CERDEC STCD SRI @ 2004-09-27 15:58 UTC (permalink / raw)
To: 'Ankit Jain', linux prg
Ankit wrote:
>i could not understand why compiler has put semi colon
>before else due t owhich this parse error comes....
>#define exch(x,y) { int tmp; tmp=x; x=y; y=tmp; }
Remember that macro expansion occures early in the compilation process
(it occures in step 4 if I remember correctly). In this case a simple
text replacement is performed.
>However that wouldn't work in some cases. The following code is
>meant to be an if-statement with two branches:
if(x>y)
exch(x,y); // Branch 1
else
do_something(); // Branch 2
As written, assuming exch() is a function you would have a two branch
if statement. However, because exch is a macro, this is what is passed
from the c preprocessor to the compiler (because exch(..) is replaced with
the body of the defination:
if(x>y)
{ int tmp; tmp=x; x=y; y=tmp;};
else
do_something();
and you have a one-brance if statement along with a parse error of an
unassociated elst statement. There are two fixes to this problem. The
first is to remove the semi-colon after the if statement:
if(x>y)
exch(x,y)
else
do_something();
This will expand to what you expect. This is probably the least
satisfactory fix. The better fix would be to wrap the entire macro in a
do-while loop, leaving the semicolon in place (i.e. this construct allows
a macro call to be treated like a function).
#define exch(x,y) do{int tmp; tmp=x; x=y; y=tmp;}while(0)
if(x>y)
do{int tmp; tmp=x; x=y; y=tmp;}while(0);
else
...
You should be aware that while macros are powerful, they can be tricky to
use. As an example, consifer what would happen with this statement
exch(a, b+4)
George
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2004-09-28 6:14 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-09-26 14:32 parse error Ankit Jain
2004-09-25 6:29 ` Elias Athanasopoulos
2004-09-28 6:14 ` Jan-Benedict Glaw
2004-09-26 22:51 ` Elias Athanasopoulos
-- strict thread matches above, loose matches on Subject: below --
2004-09-27 15:58 Huber, George K RDECOM CERDEC STCD SRI
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).