473,549 Members | 2,408 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

scanf doubt

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

Aug 13 '08 #1
51 2511
deepak <de*********@gm ail.comwrote:
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 <ENTERkey,
'\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://toerring.de
Aug 13 '08 #2
On Aug 13, 9:46 am, deepak <deepakpj...@gm ail.comwrote:
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
Aug 13 '08 #3
Daniel Molina Wegener <dm*@coder.clwr ote:
On Aug 13, 9:46 am, deepak <deepakpj...@gm ail.comwrote:
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://toerring.de
Aug 13 '08 #4
On Aug 13, 6:52 pm, j...@toerring.d e (Jens Thoms Toerring) wrote:
Daniel Molina Wegener <d...@coder.clw rote:
<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.
Aug 13 '08 #5
On Aug 13, 11:52 am, j...@toerring.d e (Jens Thoms Toerring) wrote:
Daniel Molina Wegener <d...@coder.clw rote:
On Aug 13, 9:46 am, deepak <deepakpj...@gm ail.comwrote:
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.d e
\______________ ____________ http://toerring.de

Regards,
DMW
Aug 13 '08 #6
deepak <de*********@gm ail.comwrites:
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-faq.com/>.
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_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 13 '08 #7
vi******@gmail. com writes:
On Aug 13, 6:52 pm, j...@toerring.d e (Jens Thoms Toerring) wrote:
>Daniel Molina Wegener <d...@coder.clw rote:
<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_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 13 '08 #8
On Aug 13, 7:58 pm, Keith Thompson <ks...@mib.orgw rote:
vipps...@gmail. com writes:
On Aug 13, 6:52 pm, j...@toerring.d e (Jens Thoms Toerring) wrote:
Daniel Molina Wegener <d...@coder.clw rote:
<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).

Aug 13 '08 #9
vi******@gmail. com writes:
On Aug 13, 7:58 pm, Keith Thompson <ks...@mib.orgw rote:
>vipps...@gmail .com writes:
On Aug 13, 6:52 pm, j...@toerring.d e (Jens Thoms Toerring) wrote:
Daniel Molina Wegener <d...@coder.clw rote:
<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_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 13 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
639
by: clusardi2k | last post by:
/* The below code on SGI will wait for you to enter 2 things, but on Linux it will only wait the first time. I can make the code work by replacing the scanf with: char data ; fgets (data,5,stdin); the_number = atoi (data);
12
9833
by: B Thomas | last post by:
Hi, I was reading O'Reilly's "Practical C programming" book and it warns against the use of scanf, suggesting to avoid using it completely . Instead it recomends to use using fgets and sscanf. However no explanation is offered other than that scanf handels end of lines very badly. I have exeperienced such problems when doing some numerical...
7
7645
by: hugo27 | last post by:
obrhy8 June 18, 2004 Most compilers define EOF as -1. I'm just putting my toes in the water with a student's model named Miracle C. The ..h documentation of this compiler does state that when scanf cannot fill any fields it returns EOF. I have run some tests on scanf and, so far, I've not found an EOF return. For example: If the...
3
2745
by: Vinicius | last post by:
Hello, the following code does not work: " .... void main(void) { char option; printf("Choose an option: ");
51
3833
by: moosdau | last post by:
my code: do { printf("please input the dividend and the divisor.\n"); if(!scanf("%d%d",&dend,&dor)) { temp1=1; fflush(stdin); } else
185
17261
by: Martin Jørgensen | last post by:
Hi, Consider: ------------ char stringinput ..bla. bla. bla. do {
25
9686
by: Podrzut_z_Laweczki | last post by:
Hello, I have question (or 2 :)). Is that true that for a large data using scanf/printf instead of cin/cout makes that the program runs faster? And if it is, is it legal to mix scanf/printf with C++ code? Program should execute below 1 sec and the hint is to use scanf/printf.
15
7011
by: pereges | last post by:
have i written this program correctly ? it is giving me correct output but i am little suspicious in the following two statements - scanf("%d", &ptr->data) and printf("%d\n", ptr->data). /********** LINK LIST **********/ #include <stdio.h>
13
9006
by: AMD | last post by:
Hello, I often need to parse strings which contain a mix of characters, integers and floats, the C-language scanf function is very practical for this purpose. I've been looking for such a feature and I have been quite surprised to find that it has been discussed as far back as 2001 but never implemented. The recommended approach seems to...
0
7467
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7982
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7500
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7827
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6066
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5385
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3514
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1079
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
783
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.