Solved

do{ }while (0) loop usage

Posted on 2001-06-22
5
261 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
  • 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now