C language in the Linux kernel do while (0) the method of refinement

Source: Internet
Author: User

Why does while (0) wonderful? Since it's really good, and it's pretty good to implement in the Linux kernel, 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 the output of a normal debug message. Someone would think. Aren't you superfluous? Is it possible to remove the does while (0)? In fact otherwise, let's look at the sample to be clear, although very easy:

int main (void) {while (0) {  printf ("Hello world\n");} do{printf ("Hello world1\n");} while (0); return 0; }
This is a simple code that can no longer be simpler. But I'd like to mention it. Please see the results of the execution:

Everyone knows that the first while (0) must not be executed, since the value in the while () bracket is equal to 0. Logical decision is false. That is, the Hello World in the code block does not execute, but the Do while (0) is not the same, and does while (0) even if the condition is not true. Will fight to do it once!

Other words. Why does the kernel code do this, this is because the kernel code uses do{}while (0), this structure can ensure that no matter where it can be run correctly, this is the best place to use it, or sometimes debug the program, the only debug statements written in the fact that it is not printed is actually very normal things , I do not know when you write code, have encountered. I've met it anyway. Later, this method is used to locate the wrong point and correct it smoothly.

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center ">

The code is simple. But with good use of fine proficiency is not always able to want to get, the more simple things, sometimes, the applicable value is very nice!

Share my implementation of the following debugging output program, later can be taken when the template developed:

#include <stdio.h> #include <stdarg.h>//kernel code used do{}while (0), which ensures that the #define can be executed correctly wherever it is 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");d B_warn ("e\n");d b_debug ("llo\n"); return 0;}
Execution Result:

Debug information before, very quickly can know where to print the statement, convenient debug!

Quickly locate the bug in the program!

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center ">

C language in the Linux kernel do while (0) the method of refinement

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.