The main return value in C language.

Source: Internet
Author: User

The main return value in C Language
 

From http://blog.csdn.net/piaojun_pj/article/details/5986516

 

 

C language is almost the first programming language for everyone, and the main function is the first function we come into contact. All our programs require the main function. I believe you will be familiar with the following code.

[CPP]
View plaincopyprint?
  1. # Include <stdio. h>
  2. VoidMain ()
  3. {
  4. Printf ("I Am Main function./N ");
  5. }

# Include <stdio. h> </P> <p> void main () <br/> {<br/> printf ("I am main function. /n "); <br/>}

 

Next, let's look at a very similar piece of code.

[C-sharp]
View plaincopyprint?
  1. # Include <stdio. h>
  2. IntMain ()
  3. {
  4. Printf ("I Am Main function./N ");
  5. Return0;
  6. }

# Include <stdio. h> </P> <p> int main () <br/> {<br/> printf ("I am main function. /n "); <br/> return 0; <br/>}

 

You may be wondering, what is the role of the main function return value? This problem is usually not concerned when I first learned the C language, but with the compilation of C Programs on different compilers and platforms, I am concerned about this issue. First, I found some information about the C main function return value on the Internet for your reference.

 

Analysis on the return value of main function in C Language

Analysis of the return value of the main function in C language: Many people and even some books on the market use void main (), which is actually incorrect. Void main () has never been defined in C/C ++ (). The father of C ++, Bjarne stroustrup, wrote "The definition void main (){/*... */} is not and never has been c ++, nor has it even been c. "Next I will talk about
Main function definition. The C programming language (C programming language) uses main () --- this is because there is only one type of C language in the first version, that is, Int, NO char, no long, no float ,...... Since there is only one type, you can leave it empty. In order to be compatible with the previous code, the later version will stipulate that if the returned value is not explicitly indicated, the default return value is int, that is, main () equivalent to int main (), rather than void main (). In c99, the standard requires the compiler to give a warning at least for the usage of main.

 

1. C:

In c89, main () is acceptable. Brian W. kernighan and Dennis M. Ritchie use main (), the classic masterpiece of the C programming language 2e (the second edition of C programming language (). However, in the latest c99 standard, only the following two definitions are correct: int main (void) int main (INT argc, char * argv []) (reference: ISO/IEC 9899: 1999 (E) programming ages
-C 5.1.2.2.1 program startup ). Of course, we can also make a small change. For example, char * argv [] can be written as char ** argv; argv and argc can be changed to other variable names (such as intval and charval), but they must comply with the variable naming rules. If you do not need to obtain parameters from the command line, use int main (void); otherwise, use int main (INT argc, char * argv []). The Return Value Type of the main function must be int, so that the return value can be passed to the program activator (such as the operating system ). If
If the return statement is not written at the end of the main function, c99 requires the compiler to automatically add return 0 to the generated target file (such as the EXE file), indicating that the program Exits normally. However, I suggest you add the return statement at the end of the main function. Although this is not necessary, it is a good habit. Note that vc6 does not include return 0; in the target file. This feature is probably not supported because vc6 is a 98-year product. Now I understand why I suggest you add the return statement! However, gcc3.2 (C compiler in Linux) will add return to the generated target file.
0 ;.

 

2. c ++:

The following two main functions are defined in C ++ 98: [Page] int main () int main (INT argc, char * argv []) (reference: ISO/IEC 14882 () programming ages-C ++ 3.6 start and termination) int main () is equivalent to int main (void) in c99; int main (INT argc, char * argv []) and
The same as that defined in c99. Similarly, the return value type of the main function must be Int. If the return statement is not written at the end of the main function, C ++ 98 requires the compiler to automatically add return 0 to the generated target file ;. Similarly, vc6 does not support this feature, but G ++ 3.2 (c ++ compiler in Linux) does.

 

3. About void main:

In C and C ++, the function prototype that does not receive any parameters or return any information is "Void Foo (void );". This may be the reason why many people mistakenly believe that the main function can be defined as void main (void) If no program return value is required ). However, this is wrong! The Return Value of the main function should be defined as the int type, which is specified in the C and C ++ standards. In some compilers, void main can be compiled (such as vc6), but not all compilers support void main, because void main has never been defined in the standard. G ++ 3.2
If the return value of the main function is not of the int type, it cannot be compiled. Gcc3.2 issues a warning. Therefore, if you want your program to be highly portable, use int main.

All in all: The Void main function does not return the value of Main. The default value is int type, that is, int main (), and an integer is returned. Note that the new standard does not allow the use of the default return value, that is, the int value cannot be saved, and the corresponding main function no longer supports the void type return value. Therefore, in order to make the program have good portability, it is strongly recommended to use: int main () {return 0;/* the return value of the new standard main function. This statement can be omitted */}

 

Here, you should understand the ins and outs of the return values of the main function. The following describes how to obtain the return value of the main function. The Return Value of the main function is used to indicate the exit status of the program. If 0 is returned, the program Exits normally. The meaning of other numbers returned is determined by the system. Generally, if the return value is non-zero, the program exits unexpectedly. Next we will do a small experiment in the WINXP environment. First, compile the following program:

Int main (void)
{

Return 0;
}

Open the "command prompt" in the attachment, run the compiled executable file in the command line, enter "Echo % errorlevel %", and press Enter, the Return Value of the program is 0. The prepared file is a.exe. If "A & dir" is entered, the folders and files in the current directory are listed. However, if it is changed to "Return-1", or other non-0 values, enter "A & dir" after re-compilation, the Dir will not be executed. The meaning of & is: if the program before & Exits normally, continue to execute & subsequent programs; otherwise, do not execute. That is to say, using the return value of the program, we can control whether to execute the next program. This is int
Main benefits. You can also change the return value type of the main function to a non-int type (such as float), re-compile and execute "A & dir", the program exits unexpectedly, and the Dir will not be executed. By the way, if a | DIR is input, it indicates that if a exits abnormally, DIR is executed.

In Linux, input echo $? In the shell terminal ?, The Return Value of the main function is displayed. For example:

Maintest. c

# Include <stdio. h>

Int main ()

{

Printf ("I Am Main function./N ");

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.