Why allow concatenation of string literals?

前端 未结 10 637
借酒劲吻你
借酒劲吻你 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:43

    Sure, it's the easy way to make your code look good:

    char *someGlobalString = "very long "
                             "so broken "
                             "onto multiple "
                             "lines";
    

    The best reason, though, is for weird printf formats, like type forcing:

    uint64_t num = 5;
    printf("Here is a number:  %"PRIX64", what do you think of that?", num);
    

    There are a bunch of those defined, and they can come in handy if you have type size requirements. Check them all out at this link. A few examples:

    PRIo8 PRIoLEAST16 PRIoFAST32 PRIoMAX PRIoPTR
    

提交回复
热议问题