What’s the need of using encapsulation { do..while(0) block } in define a macro in C programming?
While reading many codes in c programming based open source project i came to know one interesting things. Let me explain you by some sample code.
#define Assert(v) if(!(v)) printf("Error occurred at: %s, in %s at %i", #v, __FILE__, __LINE__); |
At most of the experienced people used to write such macro in following style
#define Assert(v) do { if(!(v)) printf("Error occurred at: %s, in %s at %i", #v, __FILE__, __LINE__); } while(0) |
This method is called as making encapsulating our macro.
Now let me explain you whats the need of this….
see if you havent encapsulated your macro then
case 1: if you will call that macro with ;
int main() { Assert(0); return 0; } |
here it will expand as
int main() { if(!(v)) printf("Error occurred at: %s, in %s at %i", #v, __FILE__, __LINE__); ; return 0; } |
as you can see here that extra ; in macro will create one empty statement. but it will not create problem in such condition but
Case 2:if you are using that macro with any if statement
int main() { if(1) Assert(0); else printf("ShareProgrammingTips"); return 0; } |
this will expand as
int main() { if(1) if(!(v)) printf("Error occurred at: %s, in %s at %i", #v, __FILE__, __LINE__); ; //this empty statement will close if block and create error for below else block else printf("ShareProgrammingTips"); return 0; } |
here that extra statement will close if block and create error for below else block. and this code will not even compile.
if you have encapsulated your macro then that will be expand as
int main() { if(1) if(!(v)) do { printf("Error occurred at: %s, in %s at %i", #v, __FILE__, __LINE__); } while(0); else printf("ShareProgrammingTips"); return 0; } |
Here it will be not create any problem for complication.
If your macro have multi statement then in that case also it will help you to over come from such condition.
with such encapsulataion method you can use macro as just calling any function must be followed by ;
You may like to read this also....
Post comment
Search in this website
our sponsors
latest comments
- sagar on List of all standard version of c language
- Mohit Dhukia on How to access/unblock songs.pk in india?
- shinto peter on How to configure mail from localhost ( wamp ) using PHP?
- tammylleanne on Implementation limitation of c programming language
- Deepak on How to access/unblock songs.pk in india?
Find us on Facebook
Top Authors
Find us on stackoverflow
Polls
My Bookmarks
- Audio/video Recorder & player application based on MATLAB
- check dependency of your binary
- defination of all standard c programming language function
- Great Question-Answer on c programming
- know what your c code means
- Limition of c programming language
- List of all version of c programming language
- Online c compiler
- php freelancing work
- some more stuff on C programming language
- Volatile Keyword in Embedded System
- Write Android application in c language