Improve company productivity with a Business Account.Sign Up

x
?
Solved

do{ }while (0) loop usage

Posted on 2001-06-22
5
Medium Priority
?
328 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 150 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

Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
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.

595 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