COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### problem with scanf( )

• Email
• Follow

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

 0
Reply sreelalpp (11) 11/28/2004 1:58:48 AM

See related articles to this posting


sreelal wrote:
> Dear all,
>
>  plz see the following prograam.

There are a few problems.
>

#include <stdio.h>

>   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

--
Al Bowers
Tampa, Fl USA
mailto: xabowers@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/


 0
Reply xabowers (231) 11/28/2004 2:38:14 AM

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 ?

 0
Reply sreelalpp (11) 11/28/2004 5:12:26 PM


sreelal wrote:
> 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;
}

--
Al Bowers
Tampa, Fl USA
mailto: xabowers@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/


 0
Reply xabowers (231) 11/28/2004 6:09:04 PM

sreelal wrote:

< snip >

> 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

 0
Reply greggory (13) 11/28/2004 9:17:13 PM

sreelal <sreelalpp@gmail.com> wrote:

>         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
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.

--
Stan Tobias
mailx echo siXtY@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g
`
 0
Reply siXtY (303) 11/28/2004 10:24:03 PM

5 Replies
87 Views

Similar Articles

12/10/2013 2:01:32 PM
[PageSpeed]

Similar Artilces:

problem with edk
\. LibGen Done. make: *** [microblaze_0/lib/libxil.a] Error 2 Done. Have you an idea to resolve this problem? thanks Regards R!SC Last time I checked with EDK6.2i, it did not like embedded spaces in the path name. IIRC, one could either put the files in a path without embedded spaces, or use the subst command. - Newman "R!SC" <opb@xilinx.com> wrote in message news:_XdCd.622615$35.25734702@news4.tin.it... > Hi all, > > i'm first time approch with fpga, i have xilinx ise and edk 6.3i version. > With XPS I have create a new project with project builder... > Done. > > > Have you an idea to resolve this problem? > > thanks > Regards > R!SC > Problem with Widcards that have Longs as keys > and Lists of *the specific type List<?>* as keys. That?s wrong. The problem is, that Map<Long, List<?>> can map Longs on any type of list. I.e., 3 could map to a List<String>, whereas 5 maps to List<Number> and so on. Thus, the moment you give Java a Map<Long, List<Double>>, it says: cannot assign, because imagine that this would work, then the following code would be possible, and of course we don?t want that: Map<Long, List<?>> badMap = new HashMap<Long, List<Double>>; //will not compile map.put Problem formdesign Dear All, Every time I open a pop-up-datsheet-form the form-width increases. And I can't change the Form-width-property. auto resize = no auto center = no pop-up = yes Modal = yes What am I missing ? Filip On Mar 2, 4:24 pm, "Filips Benoit" <benoit.fil...@telenet.be> wrote: > Dear All, > > Every time I open a pop-up-datsheet-form the form-width increases. > And I can't change the Form-width-property. > > auto resize = no > auto center = no > pop-up = yes > Modal = yes > > What am I missing ? > > Filip What is the BorderSty StarCraft problem Battlenet to update you to the latest version. > > Will I have to start over? Even if you do, you can use cheat codes to skip levels with no ill-effects. The only things you have to worry about appear in BroodWar, and there is still a way to work around that problem. Select into problem Hi Guys, I have a question about PL/SQL code I have the following select into statement : SELECT STUD_AGE INTO v_cHP_MaxAge FROM STUDENT WHERE GDU = '015346001002'; I know It has no value returned but it give a no_data_found error. How do I escape that error without using exception? Is there any way to force the query return some value instead of an error? Thanks in advance Jun wrote: > Hi Guys, > > I have a question about PL/SQL code > > I have the following select into statement : > > SELECT STUD_AGE INTO v_cHP_MaxAge FROM STUDENT W mhchem problem Using mhchem, I wanted to put reaction rate constants above and below <=> with this: \documentclass{article} \usepackage[version=3]{mhchem} \begin{document} \ce{CO2(aq) + H2O <=>[\ce{$k_+$}][\ce{$k_-$}] H+ + HCO3-} \ce{CO2(aq) + H2O <=>[\text{$k_+$}][\text{$k_-$}] H+ + HCO3-} \end{document Alas,$k_+$above <=> appeared in the .dvi file at the correct size in both equations but$k_-\$ was too small in both. What should I have done? -- John Harper, School of Mathematics, Statistics and Computer Science, Victoria University, PO Box 600, Wellington 6140, New Zealand e-

problem in kde
I'm running KDE3.1 from Slackware 9.1 and I notice that sometimes I have problems when I hit the "enter" key. It sometimes opens up Konqueror. Is this a known issue and is there a way to fix it (other than not using the "enter" key). Miguel De Anda wrote: > I'm running KDE3.1 from Slackware 9.1 and I notice that sometimes I have > problems when I hit the "enter" key. It sometimes opens up Konqueror. Is > this a known issue and is there a way to fix it (other than not using the > "enter" key). is konqueror web borwser ( or home di

verbatim problem
Hi, I would like to write three lines like the following: 8 adders/subtractors + 4 Data Registers = 120 + 28 = 148 (CLBs) (conventional implementation) When I use verbatim I found the fonts for these three lines are different from the fonts I used in the paper. Is there a better way to write these three lines? Thank you very much! sincerely ------------- Kuan Zhou ECSE department Kuan Zhou wrote: > I would like to write three lines like the following: > > > 8 adders/subtractors + 4 Data Registers > = 120

Problem with tkinter
Hello Friends, i installed the new Python 2.3 tarball but have a little (or big) problem. When testing turtle.py i get the following message. I am using Linux SUSE 8.2, Tk ist installed as version 8.4 ../configure, make and make install work fine .... Python 2.3 (#2, Nov 11 2003, 17:40:46) [GCC 3.3 20030226 (prerelease) (SuSE Linux)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> but: ropebu@z3pc47:/usr/local/lib/python2.3/lib-tk> python turtle.py Traceback (most recent call last): File "turtle.py", line 4, in ? import Tkinter File "/usr/local/lib/python2.3/lib-tk/Tkinter.py", line 38, in ? import _tkinter # If this fails your Python may not be configured for Tk ImportError: No module named _tkinter So the questions is: how to configure python to use Tk. Greeting from Karlsruhe Hi! ropebu@web.de (rolf peter) wrote: >So the questions is: how to configure python to use Tk. IIRC the package "tcl-devel" isn't installed by default, but nessesary before compiling python. hth Jan Thank You, that was it after installing the package

Simulation problem
Hi, I am stuck with this problem, hope you can help me: I have a variable X_0 that contains the weekly market returns for one year (52 observations), a variable Y that represents the company returns for the same period and a variable called NOISE_0 that is the difference between the two: X_0 = Y + NOISE_0 Now I need to create the returns for n companies: X_t = Y + NOISE_t where t = 1 to n Y is always constant. NOISE_t (t=1 to n) has to be randomly created but has to have the same variance as the original NOISE_0 variable. Var(NOISE_t) = Var(NOISE_0) Any solution? Thanks in advance. PA If I understand correct, you want to generated, y(i,t)=a + b*x(i,t) + error(i) where i is company id, t is weed id. error(i) has is iid within a company. ******here is a sample pag, you may modify as you wish*********; data test; do company=1 to 50; std=ranuni(123)*10; do week=1 to 52; x=rannor(123); y=3+3*x+std*rannor(123); output; end; end; run; proc print; run; alves wrote: > Hi, > > I am stuck with this problem, hope you can help me: > > I have a variable X_0 that contains the weekly market returns for one > year (52