Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

Possible Duplicate:
Why are there sometimes meaningless do/while and if/else statements in C/C++ macros?Why are there sometimes meaningless do/while and if/else statements in C/C++ macros?

I met code like below:

#define ev_io_init(ev,cb,fd,events) \ do { \ ev_init ((ev), (cb)); \ ev_io_set ((ev),(fd),(events)); \ } while (0) 

I want to know why the author use do { } while (0) here. Is there any difference with this?

#define ev_io_init(ev,cb,fd,events) { \ ev_init ((ev), (cb)); \ ev_io_set ((ev),(fd),(events)); \ } 

BTW: the code is from libev, ev_local.h

Possible Duplicate:
Why are there sometimes meaningless do/while and if/else statements in C/C++ macros?

I met code like below:

#define ev_io_init(ev,cb,fd,events) \ do { \ ev_init ((ev), (cb)); \ ev_io_set ((ev),(fd),(events)); \ } while (0) 

I want to know why the author use do { } while (0) here. Is there any difference with this?

#define ev_io_init(ev,cb,fd,events) { \ ev_init ((ev), (cb)); \ ev_io_set ((ev),(fd),(events)); \ } 

BTW: the code is from libev, ev_local.h

Possible Duplicate:
Why are there sometimes meaningless do/while and if/else statements in C/C++ macros?

I met code like below:

#define ev_io_init(ev,cb,fd,events) \ do { \ ev_init ((ev), (cb)); \ ev_io_set ((ev),(fd),(events)); \ } while (0) 

I want to know why the author use do { } while (0) here. Is there any difference with this?

#define ev_io_init(ev,cb,fd,events) { \ ev_init ((ev), (cb)); \ ev_io_set ((ev),(fd),(events)); \ } 

BTW: the code is from libev, ev_local.h

CPP macros have their own tag. macros wiki states: "questions relating to other macro programming languages should not use this tag"
Link
reinserted duplicate post notice that was removed in last edit
Source Link
Linus Kleen
  • 34.8k
  • 10
  • 97
  • 100

Possible Duplicate:
Why are there sometimes meaningless do/while and if/else statements in C/C++ macros?

I met code like below:

#define ev_io_init(ev,cb,fd,events) \ do { \ ev_init ((ev), (cb)); \ ev_io_set ((ev),(fd),(events)); \ } while (0) 

I want to know why the author use do { } while (0) here. Is there any difference with this?

#define ev_io_init(ev,cb,fd,events) { \ ev_init ((ev), (cb)); \ ev_io_set ((ev),(fd),(events)); \ } 

BTW: the code is from libev, ev_local.h

I met code like below:

#define ev_io_init(ev,cb,fd,events) \ do { \ ev_init ((ev), (cb)); \ ev_io_set ((ev),(fd),(events)); \ } while (0) 

I want to know why the author use do { } while (0) here. Is there any difference with this?

#define ev_io_init(ev,cb,fd,events) { \ ev_init ((ev), (cb)); \ ev_io_set ((ev),(fd),(events)); \ } 

BTW: the code is from libev, ev_local.h

Possible Duplicate:
Why are there sometimes meaningless do/while and if/else statements in C/C++ macros?

I met code like below:

#define ev_io_init(ev,cb,fd,events) \ do { \ ev_init ((ev), (cb)); \ ev_io_set ((ev),(fd),(events)); \ } while (0) 

I want to know why the author use do { } while (0) here. Is there any difference with this?

#define ev_io_init(ev,cb,fd,events) { \ ev_init ((ev), (cb)); \ ev_io_set ((ev),(fd),(events)); \ } 

BTW: the code is from libev, ev_local.h

this is not interpreted by the preprocessor as a macro now that it's spread over multiple lines without escaping
Source Link
Loading
insert duplicate link
Source Link
Loading
Post Closed as "exact duplicate" by Greg Hewgill, Alok Save, Nim, CommunityBot, Gunther Piez
fixed code formatting
Source Link
Quasdunk
  • 15.2k
  • 3
  • 39
  • 47
Loading
Source Link
ciphor
  • 8.3k
  • 11
  • 57
  • 71
Loading