How to differentiate between getch() and getche() function?
What is the difference between getch() and getche() function ? Why do we use these two functions in a C++ program? They are actually built-in functions in standard library of c++. Both getch() and getche() functions are built-in function in C++ library and they are used for taking single character input from the user. Mostly getch() function is used at the end of main function for exiting the output mode. When we do write getch() function it allows the user to enter a single key from keyboard. Whatever the key user strikes it does not display on screen. But getche() function is used not only for taking a character input but it also displays the pressed key on output screen. Mostly it is used for taking character input in a loop. So, the answer is given here of the question: what is the difference between getch() and getche() function ?
What is purpose of clrscr() function?
The purpose of clrscr() function is to remove or erase or clear the output window. This function is usually used at the start of main function so the the output of any previous program does not display on the screen. If we do not write clrscr() function at the start of main function then we will see previous output as well on the screen each time when we will run a new program.
Why do we write void before the main function?
The void keyword can be written at the beginning of any function that will not return any value. SO basically void mentions the data type of the output of function that it will produce. If we write void before the main() function then it means this function should not return any value.
What is the difference between variable declaration and initialization?
Whatever the variables we want to include in our program, we need to tell them to the compiler. This is done using the variable declaration. It is just like making a sport team. When we are making a sport team the first step is to nominate the participants. Similarly all those parameters/variables we want to include in out program first need to be mentioned.
These are the three variables that we have declared.
As previously mentioned for a sport team, once the members have been nominated they are assigned the role. Similarly after mentioning the variables names we need to assign them values. This can be done directly or by taking input from user.
If we declare variables in the program but do not assign value to them then it will not cause any compilation error but a logical error. Why? because compiler will assign some garbage values to all those variables that have not been assigned any value.
When do we write return 0?
This statement is written when we do not write void as return type of any function. When function return type has been mentioned as int, float, double or char but it does not return any value then we need to write return 0 at the end of that function.
If we will not write return 0 at the end then we will have compilation error.
Also read here: