Define 語句中的do while 0

2021-08-26 19:42:29 字數 637 閱讀 9693

經常在define語句中看到用do{} while(0)將乙個程式片段包起來,一直感覺很奇怪,經查詢,才發現這是在define語句使用中的乙個tricky的情況。

define表達使用do{} while(0)的兩個例子:

#define list_insert_after(listelm, elm, field) do while (0)

#define init_list_head(ptr) do while (0)

下面以第二個define語句為例子說明不使用do{}while(0)會用什麼情況發生。

這是測試**

#define init_list_head(ptr)

struct node ;

void main()

if(1 == 0)

;else

head.val = 0;

相信看到這裡後各位就會明白使用do{}while(0)的原因了吧。

define 語句中的 和 的作用與區別

閱讀c c 原始碼時經常看到define語句中 和 這裡記錄下其區別和用法。先看如下 include 測試不帶 號的效果 define no number sign x x 測試乙個 號的效果 define single number sign x x 測試兩個 號的效果 define double...

for語句中的 和

for int i 0 i 100 i 和for int i 0 i 100 i 是程式設計中常用的兩個語句,這兩個語句看似作用類似但實際上在使用中卻容易產生很多意想不到的問題 入下例 這段 是用歸併排序演算法對陣列進行排序,在merge函式中有三個for語句,如果使用 和使用 會嘗試完全不同的結果...

Sql語句中的DDL語句

資料庫模式定義語言ddl data definition language 是用於描述資料庫中要儲存的現實世界實體的語言。主要由create 新增 alter 修改 drop 刪除 和 truncate 刪除 四個關鍵字完成。create database 資料庫名 建立乙個資料庫 create d...