what is ungetch?and what this exactly do?

It puts the last character read from the input buffer back into the buffer so the next call to getch() will return it. Let's say you are reading numbers into a string buffer from stdin and you get a character. You want to process the numeric data and then start reading a string, so you put the character back with ungetch(). That way when you start reading a string, you start with the character you had read. Example where you are reading amounts and currency type and your incoming data looks like this:

100dollars
200euros
300pesos

So you read the 100, and then get the 'd'. You put it back in the buffer and then read the currency, starting with the 'd', getting "dollars", then 200 and 'e', putting the 'e' back and getting "euros", etc.