简体   繁体   English

将字符串文字与char文字连接

[英]Concatenate string literal with char literal

I want to concat a string literal and char literal. 我想连接字符串文字和char文字。 Being syntactically incorrect, "abc" 'd' "efg" renders a compiler error: 语法上不正确, "abc" 'd' "efg"会导致编译器错误:

xc:4:24: error: expected ',' or ';' xc:4:24:错误:预期为','或';' before 'd' 在“ d”之前

By now I have to use snprift (needlessly), despite the value of string literal and the char literal being know at compile time. 到目前为止,尽管在编译时就知道字符串文字和char文字的值,但我必须(不必要地)使用snprift。

I tried 我试过了

#define CONCAT(S,C) ({ \
    static const char *_r = { (S), (C) }; \
    _r; \
})

but it does not work because the null terminator of S is not stripped. 但是它不起作用,因为没有剥离S的空终止符。 (Besides of giving compiler warnings.) (除了发出编译器警告。)

Is there a way to write a macro to use 有没有办法编写要使用的宏

  • "abc" MACRO('d') "efg" or "abc" MACRO('d') "efg"
  • MACRO1(MACRO2("abc", 'd'), "efg") or MACRO1(MACRO2("abc", 'd'), "efg")
  • MACRO("abc", 'd', "efg") ? MACRO("abc", 'd', "efg")

In case someone asks why I want that: The char literal comes from a library and I need to print the string out as a status message. 如果有人问我为什么要这样做:char文字来自库,我需要将字符串作为状态消息打印出来。

If you can live with the single quotes being included with it, you could use stringification: 如果您可以使用其中包含的单引号,则可以使用字符串化:

#define SOME_DEF 'x'

#define STR1(z) #z
#define STR(z) STR1(z)
#define JOIN(a,b,c) a STR(b) c

int main(void)
{
  const char *msg = JOIN("Something to do with ", SOME_DEF, "...");

  puts(msg);

  return 0;
}

Depending on the context that may or may not be appropriate, but as far as convincing it to actually be a string literal buitl this way, it's the only way that comes to mind without formatting at runtime. 视情况而定,可能合适也可能不合适,但就说服它实际上是字符串字面量而言,这是想到的唯一无需在运行时进行格式化的方法。

Try this. 尝试这个。 It uses the C macro trick of double macros so the macro argument has the chance to expand before it is stringified. 它使用Double宏的C宏技巧,因此macro参数有机会在字符串化之前扩展。

#include <stdio.h>

#define C d
#define S "This is a string that contains the character "
#define STR(s) #s
#define XSTR(s) STR(s)

const char* str = S XSTR(C);

int main()
{
    puts(str);
    return 0;
}

I came up with a GCC-specific solution that I don't like too much, as one cannot use CONCAT nestedly. 我想出了一种我不太喜欢的GCC专用解决方案,因为它不能嵌套使用CONCAT

#include <stdio.h>

#define CONCAT(S1,C,S2) ({                        \
    static const struct __attribute__((packed)) { \
        char s1[sizeof(S1) - 1];                  \
        char c;                                   \
        char s2[sizeof(S2)];                      \
    } _r = { (S1), (C), (S2) };                   \
    (const char *) &_r;                           \
})

int main(void) {
    puts(CONCAT ("abc", 'd', "efg"));
    return 0;
}

http://ideone.com/lzEAn http://ideone.com/lzEAn

C will only let you concatenate string literals. C仅允许您连接字符串文字。 Actually, there's nothing wrong with snprintf() . 实际上, snprintf()没有任何问题。 You could also use strcpy() : 您也可以使用strcpy()

strcpy(dest, str1);
dest[strlen(dest)] = c;
strcpy(dest + strlen(dest) + 1, str2);

You could also use a giant switch statement to overcome this limitation: 您还可以使用巨型switch语句来克服此限制:

switch(c) {
    case 'a':
        puts("part1" "a" "part2");
        break;
    case 'b':
        puts("part1" "b" "part2");
        break;

    /* ... */

    case 'z':
        puts("part1" "z" "part2");
        break;
}

...but I refuse to claim any authorship. ...但是我拒绝要求任何作者身份。

To put it short, just stick with snprintf() . 简而言之,只需坚持使用snprintf()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM