How do I concatenate const/literal strings in C?

前端 未结 17 1498
醉梦人生
醉梦人生 2020-11-21 23:45

I\'m working in C, and I have to concatenate a few things.

Right now I have this:

message = strcat(\"TEXT \", var);

message2 = strcat(strcat(\"TEXT          


        
17条回答
  •  无人共我
    2020-11-22 00:24

    Do not forget to initialize the output buffer. The first argument to strcat must be a null terminated string with enough extra space allocated for the resulting string:

    char out[1024] = ""; // must be initialized
    strcat( out, null_terminated_string ); 
    // null_terminated_string has less than 1023 chars
    

提交回复
热议问题