473,785 Members | 2,823 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

scanf(), ungetc() behaviour.

I was curious at the start about how ungetc() returns the character to the
stream, so i did the following coding. Things work as expected except if I
change the scanf("%c",&j) to scanf("%d",&j). I don't understand how could
scanf() affect the content of i[0] and i[1]. Can someone tell me why?

#include <stdio.h>
#include <ctype.h>

void main()
{
char i[2], j;
printf("Please input a two digit number:");
i[0]= getchar();
i[1]= getchar();
ungetc(i[1],stdin);
ungetc(i[0],stdin);
scanf("%c", &j); //scanf("%d",&j);
fflush(stdin);
printf("\nYou entered %d in decimal and %c in character mode for
i[0]",i[0],i[0]);
printf("\nYou entered %d in decimal and %c in character mode for
i[1]",i[1],i[1]);
printf("\nYou entered %d in decimal and %c in character mode for j",j,j);

getchar();

}
Mar 3 '06
62 5060

"CBFalconer " <cb********@yah oo.com> wrote in message
news:44******** *******@yahoo.c om...
Argento wrote:
Publish the instructors email address here and we will collectively
set him/her right. It is not right to warp the minds of the
innocent.
Are you serious about this? Before you answer that first question, I
would just want you to know, those course books are from OUHK
(Open University of Hong Kong, that's where I currently study about
this) taken from the course code MT258.


Yes, I am serious. I don't know what we want to set him right
about, since you snipped that portion. However I vaguely remember
writing the above, although you snipped the attributions. Please
don't do that. I guess snipping is an acquired art.


I am sorry about that. Please accept my apology. As i said in the other
post,
The rules in posting is rather different then those of the newsgroup that i
used
to visit. Guess I still have lots to learn about posting in newsgroup and in
deciding
what to snip. The local newsgroup i used to visit do top-posting, quote the
whole thing,etc.
And I have been reading and learning the rules from what you guys had posted
for the last month. Especially those links about google's broken reply, etc.
I
didn't even know what top posting was at the start :p
We all have an interest in not having bad teachers go uncorrected.
They produce bad product, and make life harder for all of us
correcting those ex-students. Some of us will be more diplomatic
than others, and some will be outright rude.

I suspect if you, as a student, go to your instructor and insist
that he cease and desist from misinformation, it will adversely
affect your marks. However if peers tell him the same, usually
with bell, book, candle, and standards references in hand, s/he may
listen and no evil retribution will fall on you.


Actually not, I had once did that in my U123 course. The instructor hated me
but
fortunately, it didn't affect my mark. They were even teaching HTML &
javascript
wrongly :(

Quoted from :-
http://learn.ouhk.edu.hk/~mt258/mt25...MT258Frame.htm
"The principles used in the Project have been refined and applied to the
development of the course units.
The course units are internally OUHK developed by Dr. Reggie Kwan, a
previous course coordinator
of this course, and Dr. Andrew Lui, the present course coordinator."

And all I have is the present CC's e-mail mt***@learn.ouh k.edu.hk
I hope you'll find it useful.

Mar 6 '06 #51

"Vladimir S. Oka" <no****@btopenw orld.com> wrote in message
news:du******** **@nwrdmz01.dmz .ncs.ea.ibs-infra.bt.com...
Argento wrote:
(You mean fflush(stdin), of course.) Before deciding whether to burn
the book, however, please ensure that it is not merely using them as
examples of common errors to be avoided.

Definitely not. It's used in almost every-little-single code, lol.


Can I watch them burn? ;-)

Only if you are willing to pay part of the cost for buying the book.
It cost me heaps you know. It's course material after all, buying is
compulsory (actually, its accounted into the course fee itself).
Mar 6 '06 #52

ro******@ibd.nr c-cnrc.gc.ca (Walter Roberson) writes:
In article <ln************ @nuthaus.mib.or g>,
Keith Thompson <ks***@mib.or g> wrote:
Arndt Jonasson <do********@inv alid.net> writes:

I prefer using 'return' from main, for about the only reason that I
may otherwise get compilation warnings telling me that the code
between the call to 'exit' and the end of the function is not reached.

Why would you have code between a call to exit and the end of the
function?


I have observed compilers sometimes complain that the implicit return
of a function was not reachable when the function ended with exit()...
even in some very straight forward cases.


Yes, that was the situation I had in mind. Perhaps this reason for
my habit is entirely historical by now.
Mar 8 '06 #53
On 2006-03-02 23:32:53 -0500, "Argento" <ar********@hot mail.comsaid:
Well, i just keep on seeing examples that uses fflush( stdin ) and i
was told to do so after a scanf() and before a getchar()
because the '\n' is not taken by scanf("%d") or scanf("%c"). That was
my practice of clearing the stdin's buffer.
Any suggestion/alternative of doing so would be appreciated.
Loop getchar() until you see the '\n'.

Though, scanf() itself can be quite capricious and you would do well to
find an alternative to _that_ instead of working around one of its many
apparent and real flaws.

Oct 2 '06 #54
Jordan Abel wrote:
"Argento" <ar********@hot mail.comsaid:
>Well, i just keep on seeing examples that uses fflush( stdin )
and i was told to do so after a scanf() and before a getchar()
because the '\n' is not taken by scanf("%d") or scanf("%c").
That was my practice of clearing the stdin's buffer.
Any suggestion/alternative of doing so would be appreciated.

Loop getchar() until you see the '\n'.
Not good enough. Have the following routine handy:

#include <stdio.h>

int flushln(FILE *f) {
int ch;

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

--
Some informative links:
<news:news.anno unce.newusers
<http://www.geocities.c om/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html >
<http://www.netmeister. org/news/learn2quote.htm l>
<http://cfaj.freeshell. org/google/>
Oct 3 '06 #55
CBFalconer said:
Jordan Abel wrote:
>"Argento" <ar********@hot mail.comsaid:
>>Well, i just keep on seeing examples that uses fflush( stdin )
and i was told to do so after a scanf() and before a getchar()
because the '\n' is not taken by scanf("%d") or scanf("%c").
That was my practice of clearing the stdin's buffer.
Any suggestion/alternative of doing so would be appreciated.

Loop getchar() until you see the '\n'.

Not good enough. Have the following routine handy:

#include <stdio.h>

int flushln(FILE *f) {
int ch;

while (('\n' != (ch = getc(f))) && (EOF != ch)) continue;
return ch;
}
Not good enough. Have the following changes ready:

s/flushln/discardln/

(or something along those lines), and either:

s/n ch/n ch == EOF/

or:

s/n ch/n ch != EOF/

The routine is the more useful if its return value can be used in a simple
on/off test in a loop - which you pick is a matter of taste, really. Hence
the second change. The first change is because we really, really, really
mustn't enshrine this ridiculous use of the word "flush" to describe
getting rid of *input*!

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 3 '06 #56
Richard Heathfield wrote:
The routine is the more useful if its return value can be used in a simple
on/off test in a loop - which you pick is a matter of taste, really. Hence
the second change. The first change is because we really, really, really
mustn't enshrine this ridiculous use of the word "flush" to describe
getting rid of *input*!
Richard 'discards' his toilet.
Oct 3 '06 #57
Christopher Layne said:
Richard Heathfield wrote:
>The routine is the more useful if its return value can be used in a
simple on/off test in a loop - which you pick is a matter of taste,
really. Hence the second change. The first change is because we really,
really, really mustn't enshrine this ridiculous use of the word "flush"
to describe getting rid of *input*!

Richard 'discards' his toilet.
No, Richard regards the contents of the toilet bowl as output, so the word
"flush" is appropriate.

But you don't flush a tap (or, if you are Usanian, a faucet).

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 3 '06 #58
Richard Heathfield wrote:
Christopher Layne said:
>Richard Heathfield wrote:
>>The routine is the more useful if its return value can be used
in a simple on/off test in a loop - which you pick is a matter
of taste, really. Hence the second change. The first change is
because we really, really, really mustn't enshrine this
ridiculous use of the word "flush" to describe getting rid of
*input*!

Richard 'discards' his toilet.

No, Richard regards the contents of the toilet bowl as output,
so the word "flush" is appropriate.

But you don't flush a tap (or, if you are Usanian, a faucet).
But you do flush an IV. Having had them on and off for the past 3
months I am now an expert.

--
Some informative links:
<news:news.anno unce.newusers
<http://www.geocities.c om/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html >
<http://www.netmeister. org/news/learn2quote.htm l>
<http://cfaj.freeshell. org/google/>
Oct 4 '06 #59
CBFalconer said:
Richard Heathfield wrote:
>Christopher Layne said:
>>Richard Heathfield wrote:

The routine is the more useful if its return value can be used
in a simple on/off test in a loop - which you pick is a matter
of taste, really. Hence the second change. The first change is
because we really, really, really mustn't enshrine this
ridiculous use of the word "flush" to describe getting rid of
*input*!

Richard 'discards' his toilet.

No, Richard regards the contents of the toilet bowl as output,
so the word "flush" is appropriate.

But you don't flush a tap (or, if you are Usanian, a faucet).

But you do flush an IV.
Yes, but what does an IV do? It carries a fluid from a reserve store,
through the tube, through the needle, and into the destination storage unit
(known as a "patient"). Thus, flushing an IV - which is done to ensure that
all the fluid intended to be administered has indeed been administered,
just as fflush is done to ensure that all the data we intend to write to
the disk is indeed written - is an *output* operation.
Having had them on and off for the past 3
months I am now an expert.
I am sorry to hear that.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 4 '06 #60

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);
20
2922
by: Sivarn | last post by:
I'm writing a program for that takes dates as input using scanf. I want to verify that the user is inputting a full 4 digits for the year. How do I do this? I know that the return value on printf is the number of printed characters; so if I could somehow get my year variable to store the leading zeros, I could just run a check: int dummy = 0; dummy = printf("%d", year);
11
3493
by: TTroy | last post by:
Hello C programmers, Can someone tell me why ungetc can't sent back EOF, but it's sister function getc has no trouble sending it to us? For a file, this might not make a difference, but for an interactive terminal, it is probably nice to push EOF back (because to user doesn't want to provide an EOF twice). How is it getc can send EOF down it's pipe, but we can't send EOF down ungetc's pipe (especially when this pipe is the same)? ...
6
3489
by: Rob Thorpe | last post by:
Given the code:- r = sscanf (s, "%lf", x); What is the correct output if the string s is simply "-" ? If "-" is considered the beginning of a number, that has been cut-short then the correct output is that r = EOF. If it is taken to be a letter in the stream, then the output should be r = 0, as far as I can see. My compiler gives EOF.
33
3169
by: Lalatendu Das | last post by:
Dear friends, I am getting a problem in the code while interacting with a nested Do-while loop It is skipping a scanf () function which it should not. I have written the whole code below. Please help me in finding why such thing is happening and what the remedy to it is. Kindly bear with my English. int main ()
68
4668
by: stasgold | last post by:
Hello. I maybe reinvent the weel ... I'm trying to read positive integer number with the help of scanf, if the input value is not positive number but negaive one zero or char , i have to reread the input until I get the needed pos. number I wrote the code , but it doesn't work the way it should : if i put some char into input, the program goes infinite loop instead of promting me to enter a new value.
27
2835
by: Simon Biber | last post by:
The following Example 3 is given in the 1999 C standard for the function fscanf: I have tested several implementations and none of them get the last case right. In no case does fscanf return 0 indicating failure to match "100ergs of energy" with "%f". The actual behaviour varies. Some will match '100', leaving the 'e' unread:
20
11024
by: Xavoux | last post by:
Hello all... I can't remind which function to use for safe inputs... gets, fgets, scanf leads to buffer overflow... i compiled that code with gcc version 2.95.2, on windows 2000 char tmp0 = "ABCDEFGHI\0"; char buff; /* Input buffer. */ char tmp1 = "ABCDEFGHI\0";
26
9534
by: vid512 | last post by:
hi. i wanted to know why doesn't the scanf functions check for overflow when reading number. For example scanf("%d" on 32bit machine considers "1" and "4294967297" to be the same. I tracked to code to where the conversion itself happens. Code in scanfs just ignores return value from conversion procedures. More info in case of glibc posted here:
0
9480
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10330
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10093
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9952
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7500
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3654
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.