Why allow concatenation of string literals?

前端 未结 10 660
借酒劲吻你
借酒劲吻你 2020-11-30 14:47

I was recently bitten by a subtle bug.

char ** int2str = {
   \"zero\", // 0
   \"one\",  // 1
   \"two\"   // 2
   \"three\",// 3
   nullptr };

assert( int         


        
10条回答
  •  囚心锁ツ
    2020-11-30 15:33

    While people have taken the words out of my mouth about the practical uses of the feature, nobody has so far tried to defend the choice of syntax.

    For all I know, the typo that can slip through as a result was probably just overlooked. After all, it seems robustness against typos wasn't at the front of Dennis's mind, as shown further by:

    if (a = b);
    {
        printf("%d", a);
    }
    

    Furthermore, there's the possible view that it wasn't worth using up an extra symbol for concatenation of string literals - after all, there isn't much else that can be done with two of them, and having a symbol there might create temptation to try to use it for runtime string concatenation, which is above the level of C's built-in features.

    Some modern, higher-level languages based on C syntax have discarded this notation presumably because it is typo-prone. But these languages have an operator for string concatenation, such as + (JS, C#), . (Perl, PHP), ~ (D, though this has also kept C's juxtaposition syntax), and constant folding (in compiled languages, anyway) means that there is no runtime performance overhead.

提交回复
热议问题