scanf takes extra parameter why?

1.5k views Asked by At

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
6

There are 6 answers

4
Dale Wilson On BEST ANSWER

You have a space in your format after the second %i. scanf will read extra data to match the space. Remove the space and it should work as you expected.

0
R Sahu On

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.

0
Jerry Coffin On

After discussion in comments with Benjamin Lindley, I think I may have originally misunderstood the intent of the question.

Perhaps the question is not about why you're allowed to enter a third input, but about why you need to enter a third input before the scanf is satisfied.

In that case, I think @Dale Wilson's answer is basically correct (though somewhat short on explanation).

A space in a scanf format string means that scanf will skip across all consecutive white space. Pressing the enter key on the keyboard is normally interpreted as entering a new-line character into the input stream, and that's considered white space.

With the trailing space in the format string, he needs to enter some character that's not whitespace to get scanf to return, so even though he doesn't need or want the program to read a third parameter, he needs to enter something that scanf can/will recognize as the end of the whitespace in the input that's being matched by the trailing space in the format string. It doesn't really matter what that "something" is, other than the fact that it can't be white-space (space, new-line, tab, vertical tab, possibly others depending on locale).


The text below is how I answered originally, but after discussion and rereading, I think it missed the point of what the OP probably intended to ask, so while what it says is true, I doubt it's of much real use.

C and C++ both view standard input as basically a file. They can't (and don't try to) control what data you enter into standard input. All they can do is decide what (if anything) to do with that data after they read it.

In this case, you haven't asked to read that data at all, so it's just ignored.

Standard input to a program is a little like the program is a person standing on a busy street with hundreds of other people around. The program can listen to what they say or not--but can't really do anything to stop all of them from talking. Likewise here, the program can read some input or not, but can't do anything to stop you from entering extra input it doesn't want or care about.

0
AndersK On

In C the caller and callee have to agree on how many parameters are pushed on the stack before calling.

scanf is no different, it takes a variable number of arguments basically determined by the format string that is supplied. scanf scans the format specifier and expects for each specified format a corresponding address to a variable.

in your test program you wrote

scanf("%i %i ",&a,&b);

which means that from the input buffer stdin, retrieve two integers and place them at the address of a and b. However there is nothing preventing you from writing many more characters in stdin, these can be read with subsequent calls to scanf.

scanf in general should be avoided for keyboard input, instead use fgets() which allows you to specify a max size for the input then use sscanf() to read from the buffer.

char buffer[128];
if (fgets(buffer,sizeof(buffer),stdin) != NULL)
{
  if (sscanf( buffer, "%i %i", &a, &b) == 2)
  {
    printf( "the numbers are %i and %i\n", a,b );
  }
}

note that you are including a C++ header but not using it at all, you may want to remove that include or change the code completely and use cin/cout instead.

0
Arial On

While scanf is convenient and easy to use, do note that it

  • does not check input arguments for you (and hence, it is your responsibility to check user's input argument)

Hence, when you're doing

scanf("%i %i ",&a,&b);

scanf would just 'scan' for input, until it THINKS that it's done.

Hence, you should instead check the input like this

if (scanf("%i %i", &a, &b) != 2) {
    /* throw error and exit */
}
0
Kunal Saini On

The way you have written scanf is completely wrong and should be avoided.

Instead of your scanf:

scanf("%i %i ",&a,&b);

You should use this:

scanf("%i%i", &a, &b);

Avoid using spaces or any other thing except the %datatype in scanf.

The working code you wanted:

#include <iostream>
using namespace std;
#include <stdio.h>

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;
}