Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
17
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

@ymko

do~while(0)とは何か?

do~whileなんて珍しいものをと思いきや、
0なのでループしないという謎ソースがあった。

よくよく聞いてみるとエラー処理などで
一気に抜けたい場合などに使えるテクニックらしい。

こんな感じにしてやると

do_while_0.c
#include <stdio.h>
int main()
{
  int i = 0;
  do {
    if (i == 0) {
      printf("hello world! i=%d\n", i);
      break;
    }
    if (i == 1) {
      printf("hello world! i=%d\n", i);
      break;
    }
    printf("I don't want to run here!\n");
  } while (0);
  return 0;
}

gotoなどを使わなくても囲われたブロックからbreakで脱出できるという訳。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
17
Help us understand the problem. What are the problem?