【编程漫谈】程序的进入与退出

就像汽油发动机,需要进行一次人工点火才能一直自行运转下去。应用程序也是如此,应用程序的入口就是一个程序启动时最先执行到的地方,操作系统通过这个入口来启动你的应用程序。正常情况程序从开始的一行执行到最后一行,程序结束。

观察一段C语言写的程序代码:

#include <stdio.h>
#include <string.h>
void main()
{
    printf("Application Starting...
");//程序启动提示
    while (1)//保持程序一直处于运行状态
    {
        printf("Waiting for command, you can input 'quit' to exit.
");
        char c[10];
        scanf("%s", &c);
        if (strcmp(c, "quit") == 0)
        {
            break;
        }
        printf("Invalid command:%s
", c);
    }
    printf("Application exited. 
Bye bye!!");//程序退出提示
}

运行之后的输出结果:

PS E:devmydemo> ./a
Application Starting...
Waiting for command, you can input 'quit' to exit.
ljkk
Invalid command:ljkk
Waiting for command, you can input 'quit' to exit.
exit
Invalid command:exit
Waiting for command, you can input 'quit' to exit.
quit
Application exited.
Bye bye!!
PS E:devmydemo>

在这段C的代码中,我们只定义了一个函数main,然后启动程序后,就按顺序执行main里边的东西了。main就是C写的程序的入口,当你告诉操作系统说,我要运行这个a.exe的时候,操作系统先读取a.exe的所有放在磁盘上的静态内容加载到内存中,然后就在内存中找a.exe那个代表main的指针,然后将这个指针交给CPU,那这个程序就处于运行的状态了。

Java虽然不是独立运行的程序,但它跟C有着相同的机制,都要定义一个入口函数main,只不过,C是直接交给CPU来处理的,而Java的main是交给java虚拟机的。Java的代码和C的大同小异,只不过java是面向对象的,所有函数都要写在一个class里边。

public class Hello {

    public static void main(String[] args) throws Throwable {
        System.out.println("Application Starting..."); //程序启动提示
        while (true) { //保持程序一直处于运行状态
            System.out.println("Waiting for command, you can input 'quit' to exit."); //输入提示
            byte[] input = new byte[10];
            var r = System.in.read(input); //读取命令
            if (r == 0)
                continue;
            var cmd = new String(input);
            if (cmd.trim().equals("quit")) {
                break;
            }
            System.out.printf("Invalid command:%s
", cmd); //提示错误
        }
        System.out.println("Application exited. 
Bye bye!!"); //程序退出提示
    }
}

JAVA的输出

PS E:devmydemo> java Hello
Application Starting...
Waiting for command, you can input 'quit' to exit.
quit
Application exited.
Bye bye!!

程序的逻辑是一样的,只是语言风格上有些不同,调用的函数略有差别。总C跟JAVA目前来讲,还没有太大的差别。

我们再一个js版的。代码如下:

WScript.StdOut.WriteLine("Application Starting...");//程序启动提示
while (1)//保持程序一直处于运行状态
{
    WScript.StdOut.WriteLine("Waiting for command, you can input 'quit' to exit.");
    var cmd = WScript.StdIn.ReadLine();

    if (cmd == "quit") {
        break;
    }
    WScript.StdOut.WriteLine("Invalid command:"+cmd);
}
WScript.StdOut.WriteLine("Application exited. 
Bye bye!!");//程序退出提示

运行输出:

PS E:devmydemo> cscript hello.js
Microsoft (R) Windows Script Host Version 5.812
版权所有(C) Microsoft Corporation。保留所有权利。

Application Starting...
Waiting for command, you can input 'quit' to exit.
hello
Invalid command:hello
Waiting for command, you can input 'quit' to exit.
quit
Application exited.
Bye bye!!

JS的代码和前边两个基于编译的程序的差别是,JS中不需要定义入口函数,js的文件的开头就是入口,程序文件的结尾就是程序结束。

再来个Python的,跟JS的差不多。

print("Application Starting...");#程序启动提示
while (1):#保持程序一直处于运行状态
    print("Waiting for command, you can input 'quit' to exit.");
    cmd = input();
    if (cmd == "quit") :
        break;    
    print("Invalid command:"+cmd);
print("Application exited. 
Bye bye!!");#程序退出提示

输入输出:

PS E:devmydemo> py hello.py
Application Starting...
Waiting for command, you can input 'quit' to exit.
hello
Invalid command:hello
Waiting for command, you can input 'quit' to exit.
quit
Application exited.
Bye bye!!

还有PHP,PERL什么的大同小异,这里不列举了。

原文地址:https://www.cnblogs.com/icoolno1/p/11338737.html