C language "Press any key to exit" implementation way

Source: Internet
Author: User
First, use System command pause

There is a pause command in the DOS command that pauses the current program and, according to the operating system language, gives

Please press any key to continue ...

or other language hints of the same meaning.

The program continues to run when any key input is detected

Header file:stdlib.h.

Function Purpose: Invoke system commands.

Function prototype: int system (const char *command);

Arguments: command is a string of commands to be executed.

Return value: The result of running the command.

by System ("pause"), the purpose of the suspension can be achieved.

Because the pause itself has output, there is no need to add additional prompts in the code.

#include <stdio.h>
#include <stdlib.h>
void Main ()
{
    //function code to execute
    system ("pause"); /wait for terminal to enter any character
}
second, using the Getch function

Header file:conio.h.

function Purpose: Reads a character from the console, but does not appear on the screen.

Function prototype: int getch (void);

Return value: the character read.

#include <stdio.h>
#include <conio.h>
void Main ()
{
    //function code to execute
    getch ();/ Wait for the terminal to enter any character
}
third, through the Exit Function

Header file:stdlib.h.

function Purpose: Force exit program.

Function prototype: void exit (int value);

Parameter: value, which is equivalent to the return value when exiting in main, is passed to the keynote process, that is, the process that invokes the executable program.

#include <stdio.h>
#include <stdlib.h>
void Main ()
{
    //function code to execute
    exit (0); Wait for the terminal to enter any character
}
four, by calling the return statement
#include <stdio.h>
int main ()
{
    //function code to perform return
    0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.