When to use these commands?

Asked

Viewed 71 times

5

What are the criteria I use to apply the commands getch(); , the return0; and the system("pause") at the end of a programme?

  • 1

    I recommend never using the system("pause") it will make a call to the operating system and execute the command pause

  • return 0 indicates to the system that the program has successfully completed

2 answers

4

getch

The getch() returns the key that was typed by the user, it is commonly used in menus with switch.

Return 0;

The return is a reserved word of language syntax C and several other programming languages. When we create a function, we define what type of data we will return (integer, decimal, text, boolean, no return), to return the value we use the command return to return the value we want.

In your case you must be creating your codes within the function main, is more or less like this:

int main()
{
    //Códigos...

    return 0;
}

This function of ours has the following signature:

int defines the return type of our function, in which case it is an integer value.

main is the name of our function (we can give the name we want).

return command to return a value. As noted by @Denis, the function main returns an integer value to the operating system and any non-zero value represents error, for this reason we returned zero to finish the application successfully.

system("pause")

It is the call of a function that aims to pause the execution of our program until the next user action.

  • 4

    Note: Return with any other value than 0 in the main function indicates error for the operating system, system("pause") not portable, works only in windows

1

getch is a function derived from conio.h. It reads the key pressed by the user. One day, conio.h may even have been something worthy and hand on the wheel, but please don’t use anymore.

system is a system call. Something similar to running a command on the terminal. Its own use is an indication that its code has an OS dependent part (as noted by Denis Rudnei de Souza here and here). In case, on Windows systems, there is a command called pause, that expects the user to have an action. Read more.

return is what Peter Paul put in your answer. Just to give some more detail, see a list of common error codes used for program output. Just note one thing: the program output code is used for a specific form of CPI, only accepting a single byte. Second that link, the return in practice is given as % 256, therefore as the least significant byte returned.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.