Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scanf takes extra parameter why?

Tags:

c++

c

scanf

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
like image 413
waruni guna Avatar asked Sep 16 '26 03:09

waruni guna


1 Answers

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.

like image 80
R Sahu Avatar answered Sep 17 '26 22:09

R Sahu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!