[問題] Macro 要如何擷字串

看板C_and_CPP (C/C++)作者 (pica)時間10年前 (2015/10/04 09:16), 編輯推噓1(101)
留言2則, 1人參與, 最新討論串1/2 (看更多)
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) gcc 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 要如何透過macro擷取字串出來. 這有點難解釋我用底下的sample code來解釋. 餵入的資料(Input): 預期的正確結果(Expected Output): 底下的code, 會輸出 enum_my_int 得字串, 想請問要如何做, 在macro的操作下, 可以把enum_ 移除. 只留下,想要的my_int 錯誤結果(Wrong Output): 程式碼(Code):(請善用置底文網頁, 記得排版) 4 typedef int my_int; 5 typedef double my_double; 6 7 typedef enum 8 { 9 enum_my_int, 10 enum_my_double, 11 } enum_type; 12 13 #define macro_test_str(p) #p 14 15 void main(void) 16 { 17 18 printf("%s\n", macro_test_str(enum_my_int)); 19 20 } 補充說明(Supplement): -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.231.105.117 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1443921393.A.045.html

10/04 09:55, , 1F
#define macro_test_str(p) #p + 5 // len(enum_) = 5
10/04 09:55, 1F

10/04 09:56, , 2F
這不算是移除,只是前進幾個字元,macro 操作無法移除
10/04 09:56, 2F
文章代碼(AID): #1M47tn15 (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #1M47tn15 (C_and_CPP)