C標準中一些預定義的巨集
阿新 • • 發佈:2018-12-02
C標準中指定了一些預定義的巨集,對於程式設計經常會用到。下面這個表中就是一些常常用到的預定義巨集。
巨集
意義
__DATE__
進行預處理的日期(“Mmm dd yyyy”形式的字串文字)
__FILE__
代表當前原始碼檔名的字串文字
__LINE__
代表當前原始碼中的行號的整數常量
__TIME__
原始檔編譯時間,格式微“hh:mm:ss”
__func__
當前所在函式名
對於__FILE__,__LINE__,__func__這樣的巨集,在除錯程式時是很有用的,因為你可以很容易的知道程式執行到了哪個檔案的那一行,是哪個函式。
下面一個例子是列印上面這些預定義的巨集的。
#include #include void why_me(); int main() { printf( "The file is %s.\n", __FILE__ ); printf( "The date is %s.\n", __DATE__ ); printf( "The time is %s.\n", __TIME__ ); printf( "This is line %d.\n", __LINE__ ); printf( "This function is %s.\n", __func__ ); why_me(); return 0; } void why_me() { printf( "This function is %s\n", __func__ ); printf( "The file is %s.\n", __FILE__ ); printf( "This is line %d.\n", __LINE__ ); }
列印資訊:
The file is debug.c.
The date is Jun 6 2012.
The time is 09:36:28.
This is line 15.
This function is main.
This function is why_me
The file is debug.c.
This is line 27.