How can I use a variable to indicate the maximum number of characters scanf() should read?
For example, using printf() , you can use * so that
#define MAXVAL 5 printf("Print at maximum MAXVAL chars: %.*s\n", MAXVAL, "myStringHere");
This will print only 5 characters, how can I do scanf read-only in MAXVAL? MAXVAL should be used as a length specifier. I can't just
scanf("%5s", string);
For now, I can only think of reading in a large array using scanf , then using ssprintf to save the string in my limited length length. Using a length specifier would be much simpler.
c scanf
CS Student
source share