473,564 Members | 2,758 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getch and ungetch ??

82 New Member
I can't understand how ungetch works. I read an explanation on kernighan and ritchie book but i can't understand.Can anyone give me a simple example??
Also, should i include any header file expect stdio?
Thanks
Aug 19 '07 #1
3 5944
JosAH
11,448 Recognized Expert MVP
I can't understand how ungetch works. I read an explanation on kernighan and ritchie book but i can't understand.Can anyone give me a simple example??
Also, should i include any header file expect stdio?
Thanks
If I remember well that name of the function is ungetc() (without the 'h'). It simply
'pushes back' the character you supply as an argument to that function.
Basically:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. ...
  3. char a;
  4. a= getc(stdin);
  5. printf("a: %c\n", a);
  6. ungetc(a);
  7. a= getc(stdin);
  8. printf("a again: %c\n", a);
  9.  
There are no other files you have to include. Read this.

kind regards,

Jos
Aug 19 '07 #2
Banfa
9,065 Recognized Expert Moderator Expert
As Jos says the standard library function is ungetc, however checking my trusty copy of Kernighan and Richie they also define an ungetch which (along with getch) is just a function they write to wrap the standard libraries getchar.

I will assume that you want an explaination for ungetch as define by K&R, the function simply writes the data back to a buffer allocated specifically for the purpose of holding this ungot data. Then the getch function checks to see if the unget buffer is empty and if it is not it returns a character from the unget buffer and decrements the amount of data in it. Otherwise if the buffer is empty it just calls the standard library getchar.
Aug 19 '07 #3
kalar
82 New Member
Yes, I mean ungetch. Thank you.
Aug 20 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
5360
by: voidstar | last post by:
Hi, I have the following problem: I use "getch" to monitor keypresses and the I use "cin" to input a string. When I type in 'y', the 'y' character appears, so I need to hit backspace before typing the string. c = getch(); cin >> str;
15
3732
by: Profetas | last post by:
Hi, How do I read a char from stdin before the '\n' is pressed? for example I know that I can use getch() to read a single char from the user input, but suppose I wanted to read before the carriage return is pressed. for example I type a char and the following actions is done before the '\n' is pressed.
4
11612
by: Crow | last post by:
Is there any way to make cin behave like getch()? Specifically, getch() returns immediately after a key is pressed and the cin family of input methods seem to block until a new line is encountered. I need some help.
1
21652
by: Ratheesh Kumar | last post by:
My doubt is on the working of getch() function based on the following observations 1) if the pressed key has an ascii code,only that can be read using a single call to getch() 2) if the key has not an ascii code then the first call to getch() returns a zero and ,if we made a second call, it will return the scan code with out an additional key...
45
3312
by: simnav | last post by:
In the following code something strange happens ! If I keep pressed any of ALT+Arrow, keys, they are extracted two times from buffer then getch seems to stop; if I release and press again ALT+arrow nothing changes: the only way to exit from this condition is press another key a single time. What seems to happen is that kbhit say some keys are...
2
4040
by: cworld | last post by:
when i use getch , then it reads the data from the keyboard but it does not display it on screen as in the case of getchar(). So should i use ungetch() or some thing else. but the program executes well and give output other then the current entered data from the keyboard. Plz help Regard cworld.
5
5230
by: Sankar | last post by:
Dear all, In my programming snippet compiled in Linux 2.6, I have a getch() , but the program when executed does not wait for my input.. fflush(stdin), fflush(stdout) - All these did not help. I would appreciate if somone suggests me proven solution to this. thanks in advance Sankar
3
1841
by: cworld | last post by:
Hi all I am making an RPN calculator in which i want to read data from input. so i used getchar( ) and able to read character by character. But when the while condition fullfill then it comes out from the loop but retain the next character read from input so i want to ungetch to the input. but it does not work. Plz help me, my code is: /********...
16
3436
by: fuzhen | last post by:
If I want to "press any key to continue" in Windows, I can use getch() But the getch() isn't a standard C function So what should I to do in Linux?
0
7665
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7583
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
7888
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. ...
0
8106
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
5484
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
3643
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...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2082
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
1
1200
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.