problem with scanf( )

S

sreelal

Dear all,

plz see the following prograam.

main()

{
int a,b;

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

printf("%d %d",a,b);
}


when i run the above program on windows using Turbo C,
the program reads three numbers, ( it is expected to read two numbers only)

if i remove the white space
after the second %d in scanf statement, it reads only two.
but if there is any space after the second %d in the scanf statement
the program waits for one more number to be inputted even after I enter two
numbers.

Can anyone say why it happends?


Regards,

Lal
 
A

Al Bowers

sreelal said:
Dear all,

plz see the following prograam.

There are a few problems.
#include said:
main() int main(void)

{
int a,b;

scanf("%d %d ",&a,&b);
scanf("%d %d", &a,&b);
will do.
printf("%d %d",a,b);
printf("%d %d\n",a,b);

return 0;
}


when i run the above program on windows using Turbo C,
the program reads three numbers, ( it is expected to read two numbers only)

Enter the numbers, seperated by a space, ie.
34 23
 
S

sreelal

the problem is,

The program is supposed to read two numbers. But when i run this it
waits for a third number to enter. I enter like

33 56 77

Why it is so ?
 
A

Al Bowers

sreelal said:
the problem is,

The program is supposed to read two numbers. But when i run this it
waits for a third number to enter. I enter like

33 56 77

Why it is so ?

Don't know.
Try this routine which uses fucntion fgets to get the stdin input
and function sscanf to convert to type integers.

#include <stdio.h>

int main(void)
{
int a,b;
char s[128];

a = b = 0;
printf("Enter two integers: ");
fflush(stdout);
fgets(s,sizeof s,stdin);
if(2 == sscanf(s,"%d %d",&a,&b))
printf("a=%d and b=%d\n",a,b);
else puts("You must enter two numbers on the line");
return 0;
}
 
G

gregg

sreelal wrote:

but if there is any space after the second %d in the scanf statement
the program waits for one more number to be inputted even after I enter two
numbers.

Can anyone say why it happends?

I wonder if it might be the actual behaviour of scanf: this function
parses the string you give first, and look for any format specifiers.
It finds the first two all right, and then proceeds on to the next since
there are spaces (blanks) before the actual '\0'.
Problem is, there is no format specifier left. So the functions waits
for something which it does not actually cares for ( I mean, it doesn't
store it).


I guess one of the clc guru could explain it.
(Or it might be ub, and depend on compiler implementation)
I have the same behaviour here, on a gcc 3.4

my €.02
 
S

S.Tobias

sreelal said:
scanf("%d %d ",&a,&b);
if i remove the white space
after the second %d in scanf statement, it reads only two.
but if there is any space after the second %d in the scanf statement
the program waits for one more number to be inputted even after I enter two
numbers.
Can anyone say why it happends?

Read the description for scanf(). Whitespace in the format string matches
any number of whitespace in the input stream. It means that scanf will
read until the first non-whitespace character is read (which it will
then conceptually ungetc(), I gather). So what you can actually feed
it with is:
1 2 x
The first %d will read initial whitespace and do the conversion, the
space will read all whitespace until character `2', the second %d will
do another conversion, space will try to read any whitespace until a
non-whitespace character (that's why it waits when you enter only two
numbers), when it finds to one (`x' character) we go over to the next
space, and this and subsequent ones will not do anything, because there
already is an `x' waiting in the input queue.

Actually, you can exit from scanf earlier, even without feeding it with
all data it expects. On *nix systems you do it by pressing ^D at the
console, on Win/DOS it's probably ^Z (this is of course highly system
dependent and off-topic as such). You must check return value from
scanf to make sure it did all the conversions you expected.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top