473,385 Members | 1,333 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

getch and ungetch ??

82
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 5932
JosAH
11,448 Expert 8TB
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 Expert Mod 8TB
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
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
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...
15
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...
4
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...
1
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...
45
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...
2
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...
5
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. ...
3
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...
16
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?
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.