使用宏字符串化预处理器以创建带空格

时间:2015-08-04 17:26:44

标签: string preprocessor

我尝试使用一些宏为我的GUI标题创建一个带空格的字符串,我有这个资源。

#define QUOTE(s) #s
#define xstr(s) QUOTE(s)

#define PACKAGE_NAME                            "SoundTest"
#define PACKAGE_VERSION_MAYOR                   1
#define PACKAGE_VERSION_MINOR                   8
#define PACKAGE_VERSION                         xstr(PACKAGE_VERSION_MAYOR.PACKAGE_VERSION_MINOR)
#define PACKAGE_STRING                          xstr(PACKAGE_NAME PACKAGE_VERSION)

以上在我的窗口标题中产生了这个输出:" SoundTest" " 1.8" (带引号); 我想要这个输出: SoundTest 1.8 (没有引号)。有什么想法吗?

2 个答案:

答案 0 :(得分:1)

document.addEventListener('webkitfullscreenchange', function(){ if (chrome.app.window.current().isFullscreen()){ webview.style.height = screen.height + 'px'; webview.style.width = screen.width + 'px'; } else{ /*get webview to original dimensions*/ }; }); 已经有引号:

PACKAGE_NAME

因此,如果您对#define PACKAGE_NAME "SoundTest" 进行字符串化,那么您也会对引号进行字符串化,从而产生类似PACKAGE_NAME的内容。 (请记住,C预处理器只适用于字符。它不了解C数据类型。)

幸运的是,C允许您通过一个接一个地编写字符串文字来连接字符串文字,因此,例如,"\"SoundTest\""是单个字符串文字,就像它已经写成"SoundTest" " " "1.8"一样。你可以在这里利用它:

"SoundTest 1.8"

答案 1 :(得分:0)

这是否有效,但它不是最好的解决方案?

#define PACKAGE_NAME                            SoundTest
#define PACKAGE_VERSION_MAYOR                   1
#define PACKAGE_VERSION_MINOR                   8
// not using this actually
#define PACKAGE_VERSION                         xstr(PACKAGE_VERSION_MAYOR.PACKAGE_VERSION_MINOR) 
#define PACKAGE_STRING                          xstr(PACKAGE_NAME PACKAGE_VERSION_MAYOR.PACKAGE_VERSION_MINOR)