473,785 Members | 2,811 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
Well, I have taken to cutting things back to the first few glaring
errors, such as #include <conio.h> or void main(), and ignoring the
rest (assuming I decide to answer at all). After all, one needs to
fix the earliest errors first, all else may well be spurious. This
also reduces the bandwidth wastage.


After so a few replies to me, i have understand about the problem with
void main() but i do not have much idea about <conio.h> other then its
not in the standard. Is there any other problem related to it other then its
not in the stardard/less portable?
Mar 4 '06 #31
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.
Mar 4 '06 #32
(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.
Mar 4 '06 #33
The reason for doing it yourself is that you get to test for
closing errors, and then you can decide what, if any, remedial
steps to take. This may be crucial in avoiding data loss.

Yes, yes, error checking *-)
Because I am just at Unit 5 of the course, there ain't much error
checking idea in my mind yet. Thanks, very informative.
Mar 4 '06 #34
Argento wrote:
Well, I have taken to cutting things back to the first few glaring
errors, such as #include <conio.h> or void main(), and ignoring the
rest (assuming I decide to answer at all). After all, one needs to
fix the earliest errors first, all else may well be spurious. This
also reduces the bandwidth wastage.


After so a few replies to me, i have understand about the problem with
void main() but i do not have much idea about <conio.h> other then its
not in the standard. Is there any other problem related to it other
then its not in the stardard/less portable?


Please do not remove attribution lines when quoting. It's imporatnat to
know who said what, as well.

As for conio.h, there's nothing wrong with it as such (AFAIK). It's just
that it's implementation and system specific and thus off topic here.
If you have questions about it, they're better answered in one of the
Borland or MS-DOS groups (if my memory serves, but look it up
yourself).

The only possible thing "wrong" with conio.h is that as soon as it's in
your program, it (your program) becomes non-portable, which in these
parts is considered a Bad Thing.

--
BR, Vladimir

We are experiencing system trouble -- do not adjust your terminal.

Mar 4 '06 #35
Argento wrote:
The reason for doing it yourself is that you get to test for
closing errors, and then you can decide what, if any, remedial
steps to take. This may be crucial in avoiding data loss.

Yes, yes, error checking *-)
Because I am just at Unit 5 of the course, there ain't much error
checking idea in my mind yet. Thanks, very informative.


You're around 4 units late in starting error checking... ;-)

(don't snip attributions)

--
BR, Vladimir

Loose bits sink chips.

Mar 4 '06 #36
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.


In my experience, collective mind of c.l.c regulars trumps your (or any)
University or course books.

(attributions please)

--
BR, Vladimir

At a contest for farting in Butte
One lady's exertion was cute :
It won the diploma
For fetid aroma,
And three judges were felled by the brute.

Mar 4 '06 #37
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? ;-)

--
BR, Vladimir

Very few profundities can be expressed in less than 80 characters.

Mar 4 '06 #38

"Vladimir S. Oka" <no****@btopenw orld.com> wrote in message
news:du******** **@nwrdmz01.dmz .ncs.ea.ibs-infra.bt.com...
Please do not remove attribution lines when quoting. It's imporatnat to
know who said what, as well.

As for conio.h, there's nothing wrong with it as such (AFAIK). It's just
that it's implementation and system specific and thus off topic here.
If you have questions about it, they're better answered in one of the
Borland or MS-DOS groups (if my memory serves, but look it up
yourself).

The only possible thing "wrong" with conio.h is that as soon as it's in
your program, it (your program) becomes non-portable, which in these
parts is considered a Bad Thing.


Thanks. I had been reading this newsgroup like a month and only starting
from
yesterday, do i post. Sorry for removing the attribution lines and snip
thing, I use
to roam about in the local newsgroups (Hong Kong, that's where I live), they
have
different manners. They do top-posting and quote the whole thing. I guess I
got
lots to learn from all of you other then C questions.
Mar 4 '06 #39
Argento wrote:

"Vladimir S. Oka" <no****@btopenw orld.com> wrote in message
news:du******** **@nwrdmz01.dmz .ncs.ea.ibs-infra.bt.com...
Please do not remove attribution lines when quoting. It's imporatnat
to know who said what, as well.

As for conio.h, there's nothing wrong with it as such (AFAIK). It's
just that it's implementation and system specific and thus off topic
here. If you have questions about it, they're better answered in one
of the Borland or MS-DOS groups (if my memory serves, but look it up
yourself).

The only possible thing "wrong" with conio.h is that as soon as it's
in your program, it (your program) becomes non-portable, which in
these parts is considered a Bad Thing.


Thanks. I had been reading this newsgroup like a month and only
starting from
yesterday, do i post. Sorry for removing the attribution lines and
snip thing, I use
to roam about in the local newsgroups (Hong Kong, that's where I
live), they have
different manners. They do top-posting and quote the whole thing. I
guess I got
lots to learn from all of you other then C questions.


Every group has it's own customs. In brief, here you don't top-post,
don't remove attributions, and try to snip stuff that's not relevant,
while leaving enough context. The last one may be difficult, so not
doing it is normally not commented upon (unless you quote a whole 100+
line post with little benefit). Also, your replies should be
interspersed with the quoted text if it makes it clearer to what part
of the post you're replying to. There are also rules on topicality, but
these are better learned by lurking.

Anyway, thanks for taking an interest in learning local customs.

PS
Asbestos underware may also come in handy. ;-)

--
BR, Vladimir

I read Playboy for the same reason I read National Geographic.
To see the sights I'm never going to visit.

Mar 4 '06 #40

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...
0
10153
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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();...
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
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.