Output/Input Problem

N

NO_Code

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?
 
R

remus.dragos

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

NO_Code

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
 
W

Walter Roberson

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
 
W

Walter Roberson

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

Suman

NO_Code said:
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 ...]
 
C

Christopher Benson-Manica

NO_Code said:
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.
 
A

Aloo

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

Walter Roberson

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

Martin Ambuhl

Aloo said:
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.
 
V

vivek.sriwastava

just use fflush(stdin) to flush the input buffer so that it can show
the prompt for the next character.
eg.
printf("Enter a ");
scanf("%c",&a);
fflush(stdin);
printf("Enter b ");
scanf("%c",&b);
fflush(stdin);
printf("b= %c\n",b);
return 0;
 
M

Martin Ambuhl

just use fflush(stdin) to flush the input buffer so that it can show
the prompt for the next character.

I can't believe we have had two suggestions in 24 hours that people use
undefined behavior. Please learn: "fflush() is not defined on input
streams."

We are told in the Lunyu (13.3) that Zilu put a silly claim to Kongzi.
Master Kong replied "How can you be so dense? Where an exemplary man is
ignorant, one should expect him to offer no opinion." This is a maxim
you should inscribe on your forehead. Or you could look up one of Dan
Pop's more colorful expressions of the same idea.
 
E

Emmanuel Delahaye

(e-mail address removed) a écrit :
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".
Do it yourself...
 
E

Emmanuel Delahaye

Walter Roberson a écrit :
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().

<all good things-for-level-4-gurus snipped>

Now you understand better why scanf() is for gurus only. Mean
programmers use fgets().
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top