簡體   English   中英

定義主體是另一個定義

[英]Define body is another define

我無法明確找到答案,似乎在實踐中起作用:

// example #1
#define test 5
#define test2 test

(test2 == 5) == true

甚至這樣:

// example #2
#define test2 test
#define test 5

是否有明確的C規范規則允許這樣做。 我認為預處理器非常簡單,它只是查找/替換。 但是我猜想在#define test2 test的情況下它知道test不是字符串,所以它可能是一個define 因此,預處理器是否僅針對這種情況進行多次掃描?

我的主要問題是,為什么我的示例2起作用?

預處理結果:

#define test2 test
#define test 5

test2
test

得到:

5
5

即使在test2之后定義了test 預處理器按照以下步驟重新掃描源:

(C99,6.10.3.4重新掃描並進一步替換p1)“替換了替換列表中的所有參數並執行#和##處理后,將刪除所有地標預處理令牌。然后,重新掃描生成的預處理令牌序列,以及源文件的所有后續預處理標記,以替換更多的宏名稱。”

如何處理第一個:

#define test 5
#define test2 test
something with test
something with test2

#define test2 5
something with 5
something with test2

something with 5
something with 5

如何處理第二個:

#define test2 test
#define test 5
something with test
something with test2

#define test 5
something with test
something with test

something with 5
something with 5

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM