Velocity Reviews > Output/Input Problem

# Output/Input Problem

NO_Code
Guest
Posts: n/a

 10-10-2005
Hi,
I am trying a very simple program on a linux machine. Code given below

int main{
char a,b;

printf("Enter a ");
scanf("%c",&a);
printf("Enter b ");
scanf("%c",&b);
printf("b= %c\n",b);
return 0;
}

when i run this here is what i get
Enter a: c
Enter b: b=
It only allows me to enter just one character and as soon as I press
enter the second prompt is printed along with the b= statement and the
program exits
What am i doing wrong and how can i correct this?

remus.dragos@gmail.com
Guest
Posts: n/a

 10-10-2005
scanf("%s", &a);

%s - this switch allows you to read more characters from console if
char a[..]; is declared like that or as a pointer.
You better try to read more info: try "man scanf".

NO_Code
Guest
Posts: n/a

 10-10-2005
scanf("%s", &a);
If i use %s would this not mean that I am trying to read a null
terminated string? which i am not as i am reading only a character from
stdin

Walter Roberson
Guest
Posts: n/a

 10-10-2005
In article <(E-Mail Removed) .com>,
NO_Code <(E-Mail Removed)> wrote:
>I am trying a very simple program on a linux machine. Code given below

>int main{
> char a,b;
> printf("Enter a ");
> scanf("%c",&a);
> printf("Enter b ");
> scanf("%c",&b);
> printf("b= %c\n",b);
> return 0;
>}

>when i run this here is what i get
>Enter a: c
>Enter b: b=
>It only allows me to enter just one character and as soon as I press
>enter the second prompt is printed along with the b= statement and the
>program exits
>What am i doing wrong and how can i correct this?

scanf() with a %c format element reads *exactly* one character. You
are entering two characters, the input you want and the newline to
terminate the line. Thus after the first scanf(), you still have
a character in the input buffer waiting to be read by the second
scanf().

The other part of what you need to know:
- scanf() always leaves the buffer positioned right after what was
read;
- *except* for %c and %[ format elements, scanf() skips leading
whitespace... including possibly any newline that happened to be
in the buffer;
- the %c and %[ format elements do NOT skip leading whitespace;
- the %s format element does NOT read until the end of line: it
skips leading whitespace and then reads until the first whitespace...
which might happen to be the next newline, but also might happen
to be merely a space or tab.

Notice the implication here, that unless you are using %c or %[
then scanf() will ignore line boundaries between input elements.
For example, scanf("%d%d", &i, &j) is perfectly happy to read any of these:

10 11
10 11
[tab]10[tab]11
[newline][newline][newline]10 11
10[space][space][space][newline][space][space][space][space]11
--
The ordering of results sorted by date is approximate.

Walter Roberson
Guest
Posts: n/a

 10-10-2005
In article <(E-Mail Removed) .com>,
NO_Code <(E-Mail Removed)> wrote:
>scanf("%s", &a);
>If i use %s would this not mean that I am trying to read a null
>terminated string? which i am not as i am reading only a character from
>stdin

More No then Yes.

The scanf() %s format element skips leading whitespace
and then reads a *whitespace* delimited string, which it then
stores in the provided input buffer as a NUL terminated string.

If a is declared as a single character, then scanf("%s", &a)
would certainly be wrong: at best the position after a
would get overwritten with the NUL character, and if the user
happened to enter a longer input, many other memory positions
might get overwritten as well.
--
I am spammed, therefore I am.

Suman
Guest
Posts: n/a

 10-10-2005

NO_Code wrote:
> Hi,
> I am trying a very simple program on a linux machine. Code given below
>
> int main{

How did this even compile? No `unexpected token' problems?

[to say nothing of the rest ...]

Christopher Benson-Manica
Guest
Posts: n/a

 10-10-2005
NO_Code <(E-Mail Removed)> wrote:

> scanf("%s", &a);
> If i use %s would this not mean that I am trying to read a null
> terminated string? which i am not as i am reading only a character from
> stdin

It is proper Usenet etiquette to include the relevant portions of the text
you are replying to. To do this using Google groups, please follow the
instructions below, penned by Keith Thompson:

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

Aloo
Guest
Posts: n/a

 10-10-2005
Try using "fflush(stdin)" just after the first printf. I think u
will solve your problem, because by the looks of it the secomd 'scanf'
seems to read the null character leftover at 'stdin' i.e. the keyboard
buffer , by the first scanf.

Walter Roberson
Guest
Posts: n/a

 10-10-2005
In article <(E-Mail Removed) .com>,
Aloo <(E-Mail Removed)> wrote:
>Try using "fflush(stdin)" just after the first printf.

Try posting some context so we know what you are replying to.

>I think u
>will solve your problem, because by the looks of it the secomd 'scanf'
>seems to read the null character leftover at 'stdin' i.e. the keyboard
>buffer , by the first scanf.

I think that it won't solve anything. According to the C standard,
fflush() is only defined for *output* streams.

fflush() of an input stream could result in any of a number of Bad Things.

Possibly the worst of these is that it might happen to discard the
newline in the buffer, thereby giving the illusion that fflush(stdin)
is well-defined, leading to a chain of events in which someone ends up
posting bad advice about fflush(stdin) and thereby encourages a
proliferation of broken code.

On the other hand, it might lead to someone's beer being spilled,
which for them might be a much worse tragedy.
--
These .signatures are sold by volume, and not by weight.

Martin Ambuhl
Guest
Posts: n/a

 10-10-2005
Aloo wrote:
> Try using "fflush(stdin)"

Pay no attention to the above. fflush is not defined on input streams.

> just after the first printf. I think u
> will solve your problem,

I doubt it. If it should by accident "work" in one context, it will
blow up tomorrow.

> because by the looks of it the secomd 'scanf'
> seems to read the null character leftover at 'stdin' i.e. the keyboard
> buffer , by the first scanf.
>

 Thread Tools

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Mike ASP General 2 05-11-2004 08:36 AM

Advertisments