Void main (void)-the wrong thing

Source: Internet
Author: User

Many 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 clearly states the definition void main (){/*... */} is not and never has been c ++, nor has it even been c. (void main () never exists in C ++ or C ). The following describes the definition of the main function in the C and C ++ standards respectively.

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 (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 versions-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 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 0 to the generated target file ;.

2. c ++

The following two main functions are defined in C ++ 98:

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. The usage of int main (INT argc, char * argv []) is 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. In 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.

4. Functions of return values

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. Otherwise, the program exits abnormally. 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. Assume that the compiled 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 the benefit of int main. If you are interested, you can also change the main function's return value type to a non-int type (such as float), re-compile and execute "A & dir" to see what will happen, think about why that happened. By the way, if a | DIR is input, it indicates that if a exits abnormally, DIR is executed.

5. What about int main (INT argc, char * argv [], char * envp?

This is certainly not defined in standard C! Char * envp [] is an extension provided by some compilers to obtain system environment variables. Because it is not a standard, not all compilers support it. Therefore, it has poor portability and is not recommended.

 

Reprinted statement:

This article from http://bbs.pfan.cn/showtxt.asp? Id = 176821 (programmer's website)

========================================================== ======================================

 

Can I write "Void main?
(Boutique)

Source: C ++ style and technique FAQ by Bjarne stroustrup

 

Q: Can I write "Void main?
A: This definition
Void main (){/*...*/}
 
It is neither C ++ nor C. (See iso c ++ standard 3.6.1 [2] Or Iso c Standard 5.1.2.2.1) A standard-compliant compiler implementation should accept
Int main (){/*...*/}
And
Int main (INT argc, char * argv []) {/*... */}
 
The compiler can also provide more overloaded versions of main (), but all of them must return an int. This int is returned to the caller of your program. This is a "responsible" approach, "Nothing returns" is not great. If the caller of your program does not support "Return Value" for communication, this value will be automatically ignored-but it cannot make void main () Legal C ++ or C code. Even if your compiler supports this definition, it is best not to develop this habit-otherwise, you may be considered a little ignorant by other C/C ++ programmers.
 
In C ++, if you are too troublesome, you do not need to explicitly write a return statement. The compiler automatically returns 0. For example:
# Include <iostream>

Int main ()
{
STD: cout <"this program returns the integer value 0/N ";
}
 
Trouble? No problem. Int main () is one letter less than void main (): O). In addition, please note that: neither Iso c ++ nor c99 allows you to omit the return type. That is to say, c89 and arm C ++: the C ++] described in the "The annotated C ++ Reference Manual" jointly written by Margaret Ellis and Bjarne stroustrup in 1990 is different. Int Is not the default return value type. So,
# Include <iostream>

Main (){/*...*/}
An error occurs because the main () function lacks the return type.

 

Original English:

Http://www2.research.att.com /~ BS/bs_faq2.html # void-Main

Strongly recommended:
Bjarne stroustrup's c ++ style and technique FAQ (English)

 

Reprinted statement:

This article from http://stdcpp.cn/html/24/26/0611/176.htm
(Ant c/C ++)

========================================================== ======================================

 

Void main (void)-the wrong thing

 

The newsgroup, comp. lang. c, is plagued by an almost continuous discussion of whether we can or cannot use void as a return type for Main. the ANSI standard says "no", which shoshould be an end of it. however, a number of beginners 'books on C have used void main (void) in all of their examples, leading to a huge number of people who don't know any better.

When people ask why using a void is wrong, (since it seems to work), the answer is usually one of the following:
Because the standard says so.
(To which the answer is usually of the form "but it works for me! ")
Because the startup routines that call main cocould be assuming that the return value will be pushed onto the stack. if main () does not do this, then this cocould lead to stack resume uption in the program's exit sequence, and cause it to crash.
(To which the answer is usually of the form "but it works for me! ")
Because you are likely to return a random value to the invokation environment. this is bad, because if someone wants to check whether your program failed, or to call your program from a makefile, then they won't be able to guarantee that a non-zero return code implies failure.
(To which the answer is usually of the form "That's their problem ").

This page demonstrates a system on which a void main (void) program will very likely cause problems in the third class above. calling the program from a script may cause the script to die, whether or not its return code is checked. calling it from a makefile may cause make to complain. calling it from the command line may cause an error to be reported.

Reduced OS is the native Operating System of acorn's range of ARM based computers. one of the facilities of this OS is a system variable, sys $ rclimit. the value of this variable specifies the maximum value that a program may return to the OS without causing risc OS itself to raise an error. the default value of this variable is set by the OS at 256. i'm not too sure what the intended function of this variable was, but it exists, and that's that.

Now, let's look at an example program using int main (void ).
Int main (void)
{
Return 42;
}
Compiling it to arm assembly language, using gcc (as an aside: acorn's own C compiler reports a warning with void main (void) and converts it to an integer function returning zero) gives the following:
| Main |:
MoV IP, SP
Stmfd SP !, {RFP, FP, IP, LR, PC}
Sub FP, IP address, #4
CMPS sp, SL
Bllt | x $ stack_overflow |
BL | ___ main |

MoV r0, #42
Ldmdb FP, {RFP, FP, SP, PC} ^

The first six instructions are initialisation and stack checking. the final two return 42 to the library startup code. so, the return value of main is passed in r0.note that the library startup code is expecting to call a function returning an integer, so will happily use the value returned in r0.

What happens with a void main function? Well, here's an example.
# Include <stdio. h>

Char Buf [1024];
Void main (void)
{
(Void) fgets (BUF, 1024, stdin );
}
The program waits for a line of text from its standard input, nothing else. Again we compile it to author Er:
|. Lc0 |:
DCD |__ iob |
|. LC1 |:
DCD | Buf |
| Main |:
MoV IP, SP
Stmfd SP !, {RFP, FP, IP, LR, PC}
Sub FP, IP address, #4
CMPS sp, SL
Bllt | x $ stack_overflow |
BL | ___ main |

LDR R2, [PC, # |. lc0 |-.-8]
MoV R1, #1024.
LDR r0, [PC, # |. LC1 |-.-8]

BL | fgets |

Ldmdb FP, {RFP, FP, SP, PC} ^

Area | Buf |, Data, common, noinit
% 1024

Again, the first six instructions in main set things up. the next three set up the arguments for the call to fgets. then we call fgets and return to the caller. stdio. H says that fgets returns a pointer to the buffer. so, in this instance, what we are returning to the library startup code is a pointer to Buf. under risc OS, all C Programs are mapped into memory at 0x8000. so, we will be returning a value to the OS which is> 32768 (hence, certainly> the default value of sys $ rclimit ). the OS then raises an error.

Here's the result of compiling and running the program:
SCSI: void % GCC void. C-o void
Drlink aof linker version 0.28 30/07/95
SCSI: void % Show sys $ rclimit
Sys $ rclimit: 256
SCSI: void % void
I enter this line
Return code too large
SCSI: void %

And, in a script file:
SCSI: void % cat script

Void
Echo finished

SCSI: void % run script
I enter this line
Return code too large
SCSI: void %

The error interrupts the script before the second command is run.

Note that the example abve was a little contrived in order to make the final function call return a pointer. A better example where this cocould cause problems is one where the program uses printf to report a usage string> 256 characters long prior to returning or, worse still, one where the program uses printf to output data depending on user input. depending on the length of the user's input text, the program may or may not cause an error which is solely due to the use of void as a return type for Main.

So, if you want your software to be portable, please make main return Int. It does matter.

 

 

Reprinted statement:

This article from http://users.aber.ac.uk/auj/voidmain.shtml

========================================================== ======================================

 

Strongly recommended:
Bjarne stroustrup's c ++ style and technique FAQ (English)

Related Article

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.