sprintf() without trailing null space in C

后端 未结 8 1316
鱼传尺愫
鱼传尺愫 2021-02-01 13:37

Is there a way to use the C sprintf() function without it adding a \'\\0\' character at the end of its output? I need to write formatted text in the middle of a fixed width stri

8条回答
  •  情话喂你
    2021-02-01 14:17

    You could also use your fixed width string as a format string like this:

    char my_fixed_width_string_format[] = "need 10 chars starting here: %10s";
    char my_fixed_width_string[40];
    char string_to_print[] = "abcdefghijklmnop";
    sprintf(my_fixed_width_string, my_fixed_width_string_format, string_to_print;
    printf(my_fixed_width_string);
    

    should yield

    need 10 chars starting here: abcdefghij

提交回复
热议问题