作者tropical72 (藍影)
看板C_and_CPP
標題[問題] #define 於 {} 內問題
時間Wed Aug 25 13:07:05 2010
開發平台: (例: VC++ or gcc/g++ or Dev-C++, Windows or Linux)
vc (6.0 2008)
有問題的code: (請善用置底文標色功能)
#define N1 10
#define NN 0
#include <stdio.h>
int main()
{
#define N2 10
printf("N1 = %d\n", N1);
printf("N2 = %d\n", N2);
// printf("N3 = %d\n", N3);
printf("NN = %d\n\n", NN);
for(int i=0; i<1; i++) {
#define N1 0
#define N2 0
#define N3 0
#ifndef NN
#define NN 1
#endif
printf("N1 = %d\n", N1);
printf("N2 = %d\n", N2);
printf("N3 = %d\n", N3);
printf("NN = %d\n\n", NN);
}
printf("N1 = %d\n", N1);
printf("N2 = %d\n", N2);
printf("N3 = %d\n", N3);
printf("NN = %d\n\n", NN);
return 0;
}
*[1;36m執行結果*[m
N1 = 10
N2 = 10
NN = 0
N1 = 0
N2 = 0
N3 = 0
NN = 0
N1 = 0
N2 = 0
N3 = 0
NN = 0
補充說明:
上面這段 code 於 VC 可正常執行
但出現以下二個 warnning
warning C4005: 'N1' : macro redefinition
warning C4005: 'N2' : macro redefinition
原始碼比對執行結果,
redefine 的問題,complier 只會發出 warnning
並不會以 error 顯示,而後 define 之變數將以最近 define 之結果為主.
這是目前我能得到的解讀.
若觀念上還有問題
煩請各位先進指教
--
我期待 我等待
肩狹骨上的翅膀早些長出來
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 180.177.76.142
→ bleed1979:我以為N1和N2會有redefined的warning... 08/25 15:26
→ bleed1979:而因為您有define NN,在gcc 4.x對應於ifndef不作用。 08/25 15:32
→ akasan:marco 有 scope ?????? 08/25 22:01
→ tropical72:一開始我也認為是沒有,不過上述code竟過了..意外. 08/25 22:53
推 Fenikso:把for拔掉一樣會過 08/25 23:00
※ 編輯: tropical72 來自: 180.177.76.142 (08/25 23:12)
推 Eric30:原po for裡面的 #ifndef NN 永遠無效耶 你是要寫#if !嗎? 08/28 12:32