I have searched and read that the ^ modifier states to ignore whatever you put inside of the [ ] in scanf. For example:
int val;
scanf("%[^abc] %d, &val);
printf("val is %d", val);
Now, if I input abc42, I thought the abc would be ignored and 42 would get stored into val. But, this doesn't happen.
I also tried to suppress the scanf by making it:
scanf("%*[^abc] %d, &val);
but this did not work either. So I am confused on what ^ actually does.
When you say you want scanf to ignore certain characters, I think you mean you want scanf to read those characters and then ignore them. In order to do that, you use the %*[] format specifier:
scanf("%*[abc] %d", &val);
The [abc] tells scanf to read any number of the characters between the brackets (i.e. a, b, or c). The * tells scanf to ignore those characters, then the %d reads the value into val.
The ^ specifier tells scanf to read any number of characters other than those in between the brackets, which is the opposite of what you want to do, I think.
You need to carefully read the documentation of scanf(3) and to enable all warnings when compiling (e.g. gcc -Wall -Wextra -g if using GCC...); you'll probably have been warned with such a compilation. You should also use the result of scanf and run your program in a debugger (gdb)
scanf("%[^abc] %d", &val);
is reading two items, the first being a string of characters outside of the set {a,b,c} (and the second being an integer going "nowhere").
So your program has undefined behavior (UB) because you call scanf with one argument less than it should, and because the first argument should be the location of a wide enough char buffer. UB can be really bad, so you need to always avoid it.
You might try:
int val= 0;
char buf[32]; // 30+1 could be enough....
memset (buf, 0, sizeof(buf));
if (scanf("%30[^abc] %d", buf, &val) == 2) {
printf("got string %s and int %d\n", buf, val);
}
else { // handle scanf failure
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With