Re: Needed explaination:#define xxx do {} while (0)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,
> hi ,
> I couldn't even get the statement
> "it says such macros are used to have a
> block  of statements without errors on subsitution."

The statement was not properly phrased. I am copying the example  from
kernelnewbie FAQ to clarify what I tried to mean. And thanks a lot for
all the responses.
-Shourya  

================================================
(from Per Persson) As both Miller and Collins point out, you want a
block statement so you can have several lines of code and declare
local variables. But then the natural thing would be to just use for
example:

  #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();
================================================

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux