How to do while (0) in Linux kernel using C language

Source: Internet
Author: User

How to do while (0) in Linux kernel using C language

Why do while (0? Because it is indeed wonderful, and the implementation in the Linux kernel is quite good, let's look at the relevant code in the kernel:

#define db_error (fmt, ...) \
    do {\
fprintf (stderr, "(error):"); \
fprintf (stderr, fmt, ## __ VA_ARGS__); \
        } while (0)
This is just a common output of debugging information, some people will think, you are not superfluous? Is it possible to remove the difference of do while (0)? In fact, it is not clear, we look at the example to make it clear, although it is very simple:
int main (void)
{
while (0)
{
printf ("hello world \ n");
}
The
do
{
printf ("hello world1 \ n");
} while (0);
The
return 0;
}
This is a piece of code that is too simple to be simple, but it still needs to be mentioned, please see the results:
Everyone knows that the first while (0) will definitely not run, because the value in the bracket of while () is equal to 0, and the logical judgment is false, that is, the hello world in the code block will not run, but do while (0) It is not the same, even if the condition is not true, do while (0) will work hard to execute once!

That is to say, why the kernel code should be done like this, this is because the kernel code uses do {} while (0); this structure can ensure that it can be executed correctly no matter where it is. This is the best use of it. Place, otherwise sometimes when debugging the program, it is normal to write the debugging statement without printing it. It is normal to know whether you have encountered when writing code. Anyway, I have encountered it, and I used it later. A way to locate the wrong point and correct it smoothly.



The code is simple, but it is not always possible to think about it when you use it with skill and skill. The simpler things are, sometimes the applicable value is still very good!

Share the following debug output program I implemented, which can be used as a template development in the future:

#include
#include
// The kernel code uses do {} while (0); this structure can ensure that it can be executed correctly no matter where it is.
#define db_error (fmt, ...) \
    do {\
fprintf (stderr, "(error):"); \
fprintf (stderr, fmt, ## __ VA_ARGS__); \
} while (0)
    
#define db_msg (fmt, ...) \
    do {\
fprintf (stdout, "(msg):"); \
fprintf (stdout, fmt, ## __ VA_ARGS__); \
} while (0)
    
#define db_warn (fmt, ...) \
    do {fprintf (stdout, "(warn):"); \
fprintf (stdout, fmt, ## __ VA_ARGS__); \
} while (0)
    
#define db_debug (fmt, ...) \
    do {\
fprintf (stdout, "(debug):"); \
fprintf (stdout, fmt, ## __ VA_ARGS__); \
} while (0)
int main (void)
{
db_error ("h \ n");
db_warn ("e \ n");
db_debug ("llo \ n");
return 0;
}
operation result:
The debugging information is in the front, and you can quickly know where to print the statement, which is convenient for DEBUG! Quickly find the location of program bugs!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.