Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

How can I specify a variable width in a scanf format string?

format scanf specify String
0
Posted

How can I specify a variable width in a scanf format string?

0

You can’t; an asterisk in a scanf format string means to suppress assignment. You may be able to use ANSI stringizing and string concatenation to construct a constant format specifier based on a preprocessor macro containing the desired width: #define WIDTH 3 #define Str(x) #x #define Xstr(x) Str(x) /* see question 11.17 */ scanf(“%” Xstr(WIDTH) “d”, &n); If the width is a run-time variable, though, you’ll have to build the format specifier at run time, too: char fmt[10]; sprintf(fmt, “%%%dd”, width); scanf(fmt, &n); (scanf formats like these are unlikely when reading from standard input, but might find some usefulness with fscanf or sscanf.) See also questions 11.17 and 12.10. comp.lang.c FAQ list ยท Question 12.16 Q: How can I read data from data files with particular formats? How can I read ten floats without having to use a jawbreaker scanf format like “%f %f %f %f %f %f %f %f %f %f”? How can I read an arbitrary number of fields from a line into an array? A: In general, there are th

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.