[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

scanf doubt

deepak

8/13/2008 1:46:00 PM

Hi,

For the program pasted below, scanf is not waiting for the second user
input.
Can someone suggest reason for this?

void
main()
{
char c;

scanf("%c", &c);
printf("%c\n", c);

scanf("%c", &c);
printf("%c\n", c);

return(TRUE);
}

output:
a
a

70 Answers

jt

8/13/2008 1:54:00 PM

0

deepak <deepakpjose@gmail.com> wrote:
> For the program pasted below, scanf is not waiting for the second user
> input.
> Can someone suggest reason for this?

You're missing

#include <stdio.h>

> void
> main()

make that

int main( void )

especially since your main() function returns a value.

> {
> char c;

> scanf("%c", &c);
> printf("%c\n", c);

Here you read a single char which then gets printed out.

> scanf("%c", &c);
> printf("%c\n", c);

And here you read a second char that also gets printed out.
It's just that this char is the char for the <ENTER> key,
'\n', which you don't see directly. So your program actually
reads in two chars, just that the second isn't one you were
expecting.

> return(TRUE);

What's 'TRUE'? Better return either 0 (which always indicates
success) or either EXIT_SUCCESS or EXIT_FAILURE as defined in
<stdlib.h>.

> }
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://t...

Daniel Molina Wegener

8/13/2008 3:46:00 PM

0

On Aug 13, 9:46 am, deepak <deepakpj...@gmail.com> wrote:
> Hi,
>
> For the program pasted below, scanf is not waiting for the second user
> input.
> Can someone suggest reason for this?

The buffer isn't flushed?, apply fflush(3) over stdin.
Also, scanf to get chars from the user input isn't safe,
try getc(3) over stdin.

>
> void
> main()
> {
> char c;
>
> scanf("%c", &c);
> printf("%c\n", c);
>
> scanf("%c", &c);
> printf("%c\n", c);
>
> return(TRUE);
>
> }
>
> output:
> a
> a

Regards,
DMW

jt

8/13/2008 3:53:00 PM

0

Daniel Molina Wegener <dmw@coder.cl> wrote:
> On Aug 13, 9:46 am, deepak <deepakpj...@gmail.com> wrote:
> > Hi,
> >
> > For the program pasted below, scanf is not waiting for the second user
> > input.
> > Can someone suggest reason for this?

> The buffer isn't flushed?, apply fflush(3) over stdin.

fflush() can only be used for output streams, using it
for input is a Windows extension which doesn't work on
other systems.

> Also, scanf to get chars from the user input isn't safe,
> try getc(3) over stdin.

What's unsafe about scanf() when reading in chars the
user did input? A different question would be if it
would be more effective to use getc(), fgetc() or
getchar() when reading just single chars...

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://t...

Daniel Molina Wegener

8/13/2008 4:07:00 PM

0

On Aug 13, 11:52 am, j...@toerring.de (Jens Thoms Toerring) wrote:
> Daniel Molina Wegener <d...@coder.cl> wrote:
>
> > On Aug 13, 9:46 am, deepak <deepakpj...@gmail.com> wrote:
> > > Hi,
>
> > > For the program pasted below, scanf is not waiting for the second user
> > > input.
> > > Can someone suggest reason for this?
> > The buffer isn't flushed?, apply fflush(3) over stdin.
>
> fflush() can only be used for output streams, using it
> for input is a Windows extension which doesn't work on
> other systems.

Thanks, I will take a better read on manual pages next
time.

>
> > Also, scanf to get chars from the user input isn't safe,
> > try getc(3) over stdin.
>
> What's unsafe about scanf() when reading in chars the
> user did input? A different question would be if it
> would be more effective to use getc(), fgetc() or
> getchar() when reading just single chars...

Thanks again, "effective" is the right word...

>
> Regards, Jens
> --
> \ Jens Thoms Toerring ___ j...@toerring.de
> \__________________________ http://t...


Regards,
DMW

vippstar

8/13/2008 4:08:00 PM

0

On Aug 13, 6:52 pm, j...@toerring.de (Jens Thoms Toerring) wrote:
> Daniel Molina Wegener <d...@coder.cl> wrote:
<snip>

> > The buffer isn't flushed?, apply fflush(3) over stdin.
>
> fflush() can only be used for output streams, using it
> for input is a Windows extension which doesn't work on
> other systems.

It's not an extension, it's undefined behavior.

> > Also, scanf to get chars from the user input isn't safe,
> > try getc(3) over stdin.

It's certainly safe.

Keith Thompson

8/13/2008 4:31:00 PM

0

deepak <deepakpjose@gmail.com> writes:
> For the program pasted below, scanf is not waiting for the second user
> input.
> Can someone suggest reason for this?
>
> void
> main()
> {
> char c;
>
> scanf("%c", &c);
> printf("%c\n", c);
>
> scanf("%c", &c);
> printf("%c\n", c);
>
> return(TRUE);
> }
>
> output:
> a
> a

Jens already explained the problem; the second scanf is reading
the new-line. You're seeing 'a' twice in your output because the
first is echoed when you typed it, and the second is the result
of the printf call. You're probably seeing an extra blank line on
output as well.

Try typing "ab" without hitting the enter key after the 'a'.
Then read section 12 of the comp.lang.c FAQ, <http://www.c-fa....
Then read the rest of it.

I don't believe that the code you posted is your real code.
It should have been rejected by your compiler. You left out the
"#include <stdio.h>" that's required for scanf and printf, and you
incorrectly declared main to return void rather than int, but you
could probably get away with both of those errors. The more serious
problems are attempting to return a value from a void function,
which should have produced at least a warning, and referring to
TRUE without declaring it, which should have caused your program
to be rejected.

If you're going to post code here, please post the *exact* code that
you actually compiled. Copy-and-paste it; don't re-type it. If you
got any diagnostic messages from the compiler, either warnings or
error messages, show us those as well (again, copy-and-paste the
messages; don't re-type them or try to summarize them.)

The exact details are important. If you knew which information you
could safely omit, you'd know enough that you wouldn't need to ask
your question in the first place.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson

8/13/2008 4:58:00 PM

0

vippstar@gmail.com writes:
> On Aug 13, 6:52 pm, j...@toerring.de (Jens Thoms Toerring) wrote:
>> Daniel Molina Wegener <d...@coder.cl> wrote:
> <snip>
>
>> > The buffer isn't flushed?, apply fflush(3) over stdin.
>>
>> fflush() can only be used for output streams, using it
>> for input is a Windows extension which doesn't work on
>> other systems.
>
> It's not an extension, it's undefined behavior.

An implementation may define the behavior of a construct whose
behavior is not defined by the C standard. That's a valid way to
provide an extension.

But whether you call it an extension or UB, it's certainly
non-portable (and, in this particular case, unnecessary).

[...]

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

vippstar

8/13/2008 8:22:00 PM

0

On Aug 13, 7:58 pm, Keith Thompson <ks...@mib.org> wrote:
> vipps...@gmail.com writes:
> > On Aug 13, 6:52 pm, j...@toerring.de (Jens Thoms Toerring) wrote:
> >> Daniel Molina Wegener <d...@coder.cl> wrote:
> > <snip>
>
> >> > The buffer isn't flushed?, apply fflush(3) over stdin.
>
> >> fflush() can only be used for output streams, using it
> >> for input is a Windows extension which doesn't work on
> >> other systems.
>
> > It's not an extension, it's undefined behavior.
>
> An implementation may define the behavior of a construct whose
> behavior is not defined by the C standard. That's a valid way to
> provide an extension.

Yes, I know. But C says it's UB, the impl says it's an extension.
This is comp.lang.c so I thought I should mention what C thinks of
that, because the poster did not mention UB.

> But whether you call it an extension or UB, it's certainly
> non-portable (and, in this particular case, unnecessary).


CBFalconer

8/13/2008 9:26:00 PM

0

Daniel Molina Wegener wrote:
> deepak <deepakpj...@gmail.com> wrote:
>
>> For the program pasted below, scanf is not waiting for the
>> second user input. Can someone suggest reason for this?
>
> The buffer isn't flushed?, apply fflush(3) over stdin.

No no. fflush is only valid on output files. Behavious is
undefined when applied to input files. To flush a line you can
apply flushln, or similar, as below:

int flushln(FILE *f) {
int ch; /* yes, an int */

while ((EOF != (ch = getc(f))) && (ch != '\n')) continue;
return ch;
}

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.a...
Try the download section.


Keith Thompson

8/13/2008 9:58:00 PM

0

vippstar@gmail.com writes:
> On Aug 13, 7:58 pm, Keith Thompson <ks...@mib.org> wrote:
>> vipps...@gmail.com writes:
>> > On Aug 13, 6:52 pm, j...@toerring.de (Jens Thoms Toerring) wrote:
>> >> Daniel Molina Wegener <d...@coder.cl> wrote:
>> > <snip>
>>
>> >> > The buffer isn't flushed?, apply fflush(3) over stdin.
>>
>> >> fflush() can only be used for output streams, using it
>> >> for input is a Windows extension which doesn't work on
>> >> other systems.
>>
>> > It's not an extension, it's undefined behavior.
>>
>> An implementation may define the behavior of a construct whose
>> behavior is not defined by the C standard. That's a valid way to
>> provide an extension.
>
> Yes, I know. But C says it's UB, the impl says it's an extension.

And they're both right. But when the C standard says that something
is UB, it merely means that the C standard doesn't define the
behavior; other things (secondary standards like POSIX, or
implementations) are free to define it.

You wrote:

It's not an extension, it's undefined behavior.

It *is* an extension, and it's undefined behavior only in that the
behavior isn't defined *by the C standard*.

> This is comp.lang.c so I thought I should mention what C thinks of
> that, because the poster did not mention UB.
>
>> But whether you call it an extension or UB, it's certainly
>> non-portable (and, in this particular case, unnecessary).

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"