Saturday, November 7, 2009

Use of output commands

What are the difference between getch(),getche(),getchar()

We have all used getch() at the end of our program in turbo C++ compiler,but its not used/required in the gcc/g++/visual studio compiler..the question is why?

Actually the getch() is an input function which expects a value to be taken as input from the standard input device of a computer i.e., the keyboard.But the fact is it doesnt wait for the pressing of enter button to take the value as input rather as soon as the value is typed it take it as the input and it doesnt throw the input value to the VDU.Until and unless we provide an input the function halts itself at this line and as soon as we give the input the execution of the line completes and that inputted value leavs in the input buffer memory.As turbo C++/C by default dont let the output screen to halt we use this function just to hold the output(definitly if there is any).In the gcc/g++/visual studio compiler they are capable of holding the output data by themself hence we dont need the getch() function here.

getche() function does the same thing as that of getch()..in addition it 'e'choes or send the input value to the VDU.We can also use this function in the turbo C++/C compiler to halt the output screen just that u will see the input key u pressed for a fraction of second before the screen goes away.

getchar() on the other hand takes the input and wait for the enter key to be hit before it can take the pressed key value as the input unlike the previous two.Hence it's very easy to see what is being given as input as the pressed key is echoed to the VDU screen as long as the enter key is not hit..and definitly also after that.

No comments:

Post a Comment