Max string length using scanf -> ANSI C

后端 未结 5 1301
情歌与酒
情歌与酒 2020-11-30 13:14

I have:

#define MAX_STR_LEN 100

and I want to put into scanf pattern so I can control the string length:

scanf         


        
5条回答
  •  盖世英雄少女心
    2020-11-30 13:22

    I wasn't happy with any of these solutions, so I researched further, and discovered GNU GCC macro stringification

    which can be used as:

    #define XSTR(A) STR(A)
    #define STR(A) #A
    #define MAX_STR_LEN 100
    scanf("%"XSTR(MAX_STR_LEN)"[^\n]s", sometext)
    

    Maybe VS2010 offers something similar?

提交回复
热议问题