在test.c中寫如下代碼: 1 #include <stdio.h> 2 3 int main() 4 { 5 printf("line:%d\n", lxx_line); 6 return 0; 7 } 使用gcc編譯 gcc -o test test.c 執行 ./test 結果 line:5 ...
在test.c中寫如下代碼:
1 #include <stdio.h>
2
3 int main()
4 {
5 printf("line:%d\n", __LINE__);
6 return 0;
7 }
使用gcc編譯 gcc -o test test.c
執行 ./test
結果 line:5
__LINE__ 是通過什麼方式知道自己在第5行呢?
使用命令 gcc -E test.c -o test.i 進行預處理
查看test.i的最後幾行代碼如下:
535 # 412 "/usr/include/stdio.h" 2 3 4
536 # 2 "test.c" 2
537
538 int main()
539 {
540 printf("line:%d\n", 5);
541 return 0;
542 }
由此可見:在預處理階段,__LINE__ 會被替換成自己所在行的行號。