scanf() variable length specifier

前端 未结 4 840
心在旅途
心在旅途 2020-11-29 08:21

How can I use a variable to specify the max number of chars scanf() should read in?

For example using printf() you can use the * like so

相关标签:
4条回答
  • 2020-11-29 09:17

    You can use the C preprocessor to help you with that.

    #define STR2(x) #x
    #define STR(X) STR2(X)
    scanf("%" STR(MAXVAL) "s", string);
    

    The processor combines "%" STR(MAXVAL) "s" to "%5s"

    0 讨论(0)
  • 2020-11-29 09:19

    Kernighan and Pike recommend using snprintf() to create the format string. I developed this idea into a method that safely reads strings:

    void scan_string(char * buffer, unsigned length) {
        char format[12]; // Support max int value for the format %<num>s
        snprintf(format, sizeof(format), "%%%ds", length - 1); // Generate format
        scanf(format, buffer);
    }
    
    int main() {
      char string[5];
      scan_string(string, sizeof(string));
      printf("%s\n", string);
    }
    
    0 讨论(0)
  • 2020-11-29 09:24

    You can't. You need to use something other than scanf(). A good and popular choice is fgets(), although its semantics are slightly different: fgets() will read a line of input, whereas scanf() with %s will read whitespace separated sequences of characters.

    To use fgets(), you'd want something like:

    fgets(string, MAXVAL, stdin);
    

    If for some reason you really want to use scanf(), have a look at this question: How to prevent scanf causing a buffer overflow in C?

    0 讨论(0)
  • 2020-11-29 09:28
    #include <stdio.h>
    
    #define MAXLEN 5
    #define S_(x) #x
    #define S(x) S_(x)
    
    int main(void){
        char string[MAXLEN+1];
    
        scanf("%" S(MAXLEN) "s", string);
        printf("<%.*s>\n", MAXLEN, string);
        return 0;
    }
    
    0 讨论(0)
提交回复
热议问题