Solved

do{ }while (0) loop usage

Posted on 2001-06-22
5
301 Views
Last Modified: 2010-04-15
Can somebody please explain the usage of do while(0) loop in C.An example would be useful.

Thanks,
arut
0
Comment
Question by:arut
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 4

Expert Comment

by:newmang
ID: 6220525
Are you sure you don't mean do while(1)?

if you have a do while loop like the following

do
   statement
   statement
while(value)

The loop will execute while value is true (ie not=zero), when value hits zero the loop will end.

Consequently if you have the following as you ask

do
   statement
   statement
while(0)

Then the loop will execute only once, therefore you might as well not bother with the do or while() statements.

What is more common is to use a do..while(1) loop to form an endless loop as follows

do
   statement
   statement
while(1)

This will go on ad infinitum. It is often used for a server type daemon where it runs forever until a signal is caught and some other signal handler code kills the application.

You can also use a break command internally to break the loop as follows

do
    get character from keyboard
    process character
    if character = Q or q
        break
    do some more stuff
    and more stuff
while(1)
stuff that gets done when q has been entered

Cheers - Gavin
0
 
LVL 5

Accepted Solution

by:
djbusychild earned 50 total points
ID: 6221554
Coming from a UNIX background and hacking around kernels you see do {} while(0) a lot.

These are usually used when you're defining MACROs. In order to ensure that people can use the MACRO within a control block without any worries. For example the Berkeley queue uses do {} while() for all their MACROs

look at the following example

#include <stdio.h>
#define MYMACRO { \
                  printf ("hello world"); \
                }

int main(int argc, char* argv[])
{
        if (1)
            MYMACRO ;
        return 0;
}

many people put ";" at the end of a macro call, and this will not work well. so you do the following


#include <stdio.h>
#define MYMACRO do { \
                  printf ("hello world"); \
                } while(0)

int main(int argc, char* argv[])
{
        if (1)
            MYMACRO ;
        return 0;
}

It's just an old trick. =)
0
 
LVL 5

Expert Comment

by:djbusychild
ID: 6221558
actually, I should have really put an "else" block there to show you exactly why, but you get the idea.
0
 

Author Comment

by:arut
ID: 6221938
Thanks for the simple example :)
0
 
LVL 5

Expert Comment

by:djbusychild
ID: 6222206
yo, wassupt wit da 'B', man? --;
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question