zoukankan      html  css  js  c++  java
  • About the return value of the main function

    Many programmers can't understand why the main function must have a integer as the return value. They have been told that the return value of the main function is helpful to check the exit state of a program. However, nobody has been told how to do the check. Today, I found that we can get the return value of a program after it finishes executing by viewing the $? enviromental variable (with "echo $?" command).

    For example:
    Source file (TestRV.c):
    #include <stdio.h>
    int main()
    {
     int rt;
     scanf("%d",&rt);
     return rt;
    }
    Compile & Run as:
    $ gcc -o TRV TestRV.c
    $ ./TRV
    34
    $ echo $?
    34
    $ echo $?
    0
    (Enviornment: linux kernel 2.6.17, gcc 4.1.2, echo 5.96)
    Can you understand this easy program? Yes, it get your input to return as the return value of the main function, and the "echo $?" command has test the last return value and output. You may ask that why "echo $?" output "0" the second time. The answer is that its output is the return value of the main function of the first time you type "echo $?".
     
    But, there is still one question left: What does program "echo" do exactly?
    I'm not sure of that yet, just to be continued.

  • 相关阅读:
    QTREE6
    洛谷 P4219 [BJOI2014]大融合 解题报告
    洛谷 P1501 [国家集训队]Tree II 解题报告
    斯特林数学习笔记
    洛谷 P4721 【模板】分治 FFT 解题报告
    xpath选择器
    修改树莓派用户名
    修改树莓派用户名
    ssh 不指定用户名登陆
    ssh 不指定用户名登陆
  • 原文地址:https://www.cnblogs.com/alexxyjiang/p/1882827.html
Copyright © 2011-2022 走看看