How to pass variable length width specifier in sscanf?

后端 未结 3 837
不思量自难忘°
不思量自难忘° 2021-01-04 06:13
sscanf(input_str, \"%5s\", buf); //reads at max 5 characters from input_str to buf

But I need to use something like %MACRO_SIZEs instead of %5s

3条回答
  •  礼貌的吻别
    2021-01-04 07:06

    if your MACRO_SIZE is const at compile time, you can try this:

    #define MACRO_SIZE "5"
    snprintf(fmt_str, 100, "%" MACRO_SIZE "s", buf);
    

提交回复
热议问题