All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Huber, George K RDECOM CERDEC STCD SRI" <George.K.Huber@us.army.mil>
To: 'Ankit Jain' <ankitjain1580@yahoo.com>,
	linux prg <linux-c-programming@vger.kernel.org>
Subject: RE: parse error
Date: Mon, 27 Sep 2004 11:58:38 -0400	[thread overview]
Message-ID: <E3E30069B061524E90BCEE4417E30661138538@monm207.monmouth.army.mil> (raw)

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

             reply	other threads:[~2004-09-27 15:58 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-09-27 15:58 Huber, George K RDECOM CERDEC STCD SRI [this message]
  -- strict thread matches above, loose matches on Subject: below --
2015-04-07  6:47 Parse Error Nejc Urbajs
2015-04-07  6:52 ` Nejc Urbajs
2015-04-07  6:24 Nejc Urbajs
2015-04-07  6:42 ` Nejc Urbajs
2015-04-05 14:43 Rickard Bremer
2015-04-05 19:38 ` Burton, Ross
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=E3E30069B061524E90BCEE4417E30661138538@monm207.monmouth.army.mil \
    --to=george.k.huber@us.army.mil \
    --cc=ankitjain1580@yahoo.com \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.