2017-03-17 93 views
2
#include <stdio.h> 
#define VAR cc 

int main(void) { 
    int ccc = 9; 
    printf("hell loo %d", VARc); 
    return 0; 
} 

我這個代碼的理解是指任何地方預處理程序發現了VAR,它將與cc代替它,因此printf將有一個適當的定義的變量ccc,但代碼中的錯誤了。是否有人可以幫助我我得到 C預處理器更換工作不


編輯1

錯誤是

test.c: In function ‘main’: 
test.c:16: error: ‘VARc’ undeclared (first use in this function) 
test.c:16: error: (Each undeclared identifier is reported only once 
test.c:16: error: for each function it appears in.) 
+1

什麼是錯誤信息? –

+1

請發佈您的錯誤信息 –

+0

你正在努力實現什麼?這看起來像一個[XY問題](http://xyproblem.info/) –

回答

10

這是行不通的。預處理器在整個令牌上工作,而不是字符串。

如果你想串聯,你可以這樣做:

#include <stdio.h> 
#define VAR(End) cc##End // ## does token concatenation inside a pp macro 

int main(void) { 
    int ccc = 9; 
    printf("hell loo %d", VAR(c)); 
    return 0; 
} 
0

爲什麼不工作的原因是:換句話說,從預處理文件識別標記

符號化普瑞森預處理來自宏擴展之前。

作爲CPP是貪婪,將考慮VARc作爲與標識符類別的單個令牌,並且是在宏定義從VAR不同。這就是爲什麼它不能被取代。

所以解決方案之一是使用連接或創建另一個宏爲VARc