I'm new to c++ & its developing I used to scan to take input parameters. But I gave two input parameters. But program allows me to enter extra parameter. Please explain me why this happened. Please find below the code I I used.
#include <iostream>
int main(int argc, const char * argv[]) {
int a,b;
scanf("%i %i ",&a,&b);
printf("a-> %i",a);
printf("b-> %i",b);
return 0;
}
Output (40 is allowed as an extra parameter)
20
30
40
a-> 20b-> 30Program ended with exit code: 0
scanf takes extra parameter why?
That's not an accurate conclusion. The extra input is left in the input stream. It is not consumed by scanf. It is discarded at the end of the program.
If you had another scanf line,
scanf("%d", &c);
the value 40 would be read into c.
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