#include<stdio.h>
#include <string.h>
#define STR1 "F:\\c\\projects\\Banking Management System\\data\\"
#define STR2 "pwd.txt"
#define STR3 STR1 STR2
#define PPCAT_NX(A, B) A##B
#define PPCAT(A, B) PPCAT_NX(A, B)
void main() {
printf("\n%s", STR3);
printf("\n%s ",PPCAT(STR1, STR2));
}
問題陳述
第一個 ("printf("\n%s", STR3);") 作業正常,給我所需的輸出,如下所示:
F:\c\projects\銀行管理系統\data\pwd.txt
但是,第二個 (printf("\n%s ",PPCAT(STR1, STR2));" 給了我以下錯誤:
|=== 構建檔案:“無專案”中的“無目標”(編譯器:未知)===| F:\c\projects\銀行管理系統\src\stringConcatMacro.c||在函式'main'中:| F:\c\projects\Banking Management System\src\stringConcatMacro.c|4|error: 粘貼 ""F:\c\projects\Banking Management System\data\"" 和 ""pwd.txt"" 沒有給出有效的預處理令牌| F:\c\projects\Banking Management System\src\stringConcatMacro.c|9|注:在宏'PPCAT_NX'的定義中| F:\c\projects\Banking Management System\src\stringConcatMacro.c|15|注:宏'PPCAT'的展開| F:\c\projects\Banking Management System\src\stringConcatMacro.c|15|注:宏'STR1'的展開| ||=== 構建失敗:1 個錯誤,0 個警告(0 分鐘,0 秒)===|
我想使用第二種方法,其中我可以通過我的路徑(STR1)傳遞不同的檔案名(而不是使用固定的 STR2)。
第二個選項哪里出了問題? enter code here
非常感激任何的幫助。
uj5u.com熱心網友回復:
第二個選項哪里出了問題?
規則是結果##必須是“有效令牌”。STR1是記號,數字1234是記號,字串文字"string"也是記號的一個例子。##連接兩個字串文字的結果"string""string"不是單個有效標記。所以操作無效。
字串文字無論如何都連接在一起,只需將它們并排撰寫即可。
#define STRLITERALCAT(a, b) a b
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/356335.html
