Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is macro declared this way? [duplicate]

What is the reason to define macro this way:

#define test_errno(msg) do{if (errno) {perror(msg); exit(EXIT_FAILURE);}} while(0)

I mean what is the reason behind do{}while(0)? Of course it will be done once only, zero is constant, cannot change to nonzero somehow, so why to use such construction?

like image 851
4pie0 Avatar asked Jun 06 '26 23:06

4pie0


1 Answers

It allows things like

if (condition) 
   test_errno(...);

to work properly with or without braces.

like image 79
DrC Avatar answered Jun 10 '26 08:06

DrC