what happens when you input things like 12ab to scanf(%d,&argu)?
It's better to read a full line, using fgets()
, and then inspecting it, rather than trying to parse "on the fly" from the input stream.
It's easier to ignore non-valid input, that way.
Use fgets()
and then just strtol()
to convert to a number, it will make it easy to see if there is trailing data after the number.
For instance:
char line[128];
while(fgets(line, sizeof line, stdin) != NULL)
{
char *eptr = NULL;
long v = strtol(line, &eptr, 10);
if(eptr == NULL || !isspace(*eptr))
{
printf("Invalid input: %s", line);
continue;
}
/* Put desired processing code here. */
}
scanf with %n giving wrong output
%n
captures all the characters processed by scanf including leading whitespace. Using %n twice can correct that. The format string skips leading whitespace and then gets the beginning count of characters. Then the integer is scanned and finally the total count of characters is captured. The difference in the count is the characters in the integer.
Always check the return of scanf as the input stream may need cleaning.
int begin = 0;
int end = 0;
int val = 0;
int clean = 0;
int result = 0;
do {
if ( ( result = scanf(" %n%d%n",&begin,&val,&endn)) != 1) {// scan one int
while ( ( clean = getchar ( )) != '\n') {//clean bad input
if ( clean == EOF) {
fprintf ( stderr, "problem reading input\n");
exit ( 1);
}
}
}
else {//scanf success
printf("byte count is: %d\n",end-begin);
}
} while ( result != 1);
Scanf does not work as expected
scanf("%d\n",&i);
is equivalent to std::cin >> i >> std::ws;
.
If you want the same behaviour for scanf
, remove \n
: scanf("%d",&i);
This is caused by the fact that any whitespace character in scanf
means "skip input until non-whitespace is found"
basic problems in scanf format and using function pointer
scanf
is a nasty beast. The difference between using scanf("%d %d ", &i, &j);
and scanf("%d %d", &i, &j);
is the extra whitespace at the end of the former statement. This makes scanf
want to skip more whitespace; but because there isn't any, it asks the console to get more (prompting you for input). Once you press ENTER, it takes that as a newline and gobbles that up - then scanf
lets you continue.
Also I wonder why input those 2 two numbers in this way
scanf("%d %d ", &i, &j);
instead ofscanf("%d %d ", i, j);
?
This is because scanf
needs to modify the integers you are passing to it. In C, if you want to modify your argument then you need to get passed the pointer to the value. If the value itself is parsed and taken as a pointer, then you'd most likely get a segmentation fault.
Related Topics
Why Do Sin(45) and Cos(45) Give Different Results
Line by Line C - C++ Code Debugging in Linux Ubuntu
How to Pass a Vector to Execvp
Is Lambdification of a Concept an Improvement or Bad Practice
Do I Get a Finished Slot If I Start Qprocess Using Startdetached
Use Templates to Get an Array's Size and End Address
How Are Exceptions Implemented Under the Hood
Error Lnk2019 Unresolved External Symbol
Reversing Order of Words in a Sentence
How to Tokenize (Words) Classifying Punctuation as Space
Dereferencing One Past the End Pointer to Array Type
Why Can't I Assign an Array Variable Directly to Another Array Variable with the '=' Operator
Size of the Classes in Case of Virtual Inheritance
How to Force the Use of Cmov in Gcc and VS
Why Isn't Memcpy Guaranteed to Be Safe for Non-Pod Types
Explicitly Initialize Dword to 1, But Debugger Shows Wildly Out of Range Value