简体   繁体   中英

Making Format String with sprintf

I want to create a format string that will be used in scanf by specifying a MAX_LENGTH for a string input. What I have is the following but the compiler says it's wrong:

const int MAX_LENGTH = 50;
char format[9];

sprintf(format, "\%%d[^\n]s", MAX_LENGTH);

The compiler says that the data argument is not used by the format string. The first % should be escaped, and the second one is part of the %d which should be read in from MAX_LENGTH . How do I accomplish this? By the way, the format string should read at most MAX_LENGTH characters until a newline is reached.

应使用%%而非 \\%来转义%符号。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM