C programs must start with the main() function (exceptions are not considered at this point). It’s a rule that everyone follows. (written in the C99 specification), but since we started learning C, we seem to have seen many versions of main, so which one is the right one?

Main function version

main()

If you look at old-fashioned C code, you’ll see that the program starts like this:

#include <stdio.h>

main(){

	printf("hello,minger\n");
}

Copy the code

Compile and run:

However, when compiled using today’s compiler, an alarm is reported and its return value defaults to int.

In fact, if a function does not explicitly declare a return type, the compiler defaults the return value to int.

void main()

The return value is void with no input arguments

void main(a)
Copy the code

Void main() : void main() : void main() : void main() : void main() : void main

And even if it does return a value, where does it return to?

The void main() function is a program entry that is called by the operating system and returns a status code to the operating system.

The return code is usually 0 to indicate that the program executed successfully. If an exception is thrown during execution, or the program is forced to terminate by the task manager, the return code is not 0.

Void main() : void main() : void main() : void main() : void main() : void main()

#include"project.h"
void Timer0Init(void)		/ / 2 milliseconds @ 11.0592 MHz
{
	AUXR &= 0x7F;		// Timer clock in 12T mode
	TMOD &= 0xF0;		// Set timer mode
	TMOD |= 0x01;		// Set timer mode
	TL0 = 0xCD;		// Set the initial timing value
	TH0 = 0xF8;		// Set the initial timing value
	TF0 = 0;		// Clear the TF0 flag
	TR0 = 1;		// Timer 0 starts the timer
	ET0=1;
	EA=1;
}

void main(a)
{
	init();
	Timer0Init();
	while(1)
	{
		text();	//
		switch(display)
		{
			case 0:display_wenshidu();break;// Display the temperature and humidity
			case 1:display_ds1302();break;	// Display the clock
			case 2:cover_time();break; // Photosensitive resistance display
		}
		TEMP=ds18b20_read();/ / read 18 b20
		ds1302_read();/ / read 1302
		TEM = PCF8591_read(0);// Determine that the humidity is 99% when the Rb2 voltage output is 5V
// Cover=PCF8591_read(3); // Photosensitive resistor

		button();/ / button}}void T0_time(a) interrupt 1
{
	TL0 = 0xCD;		// Set the initial timing value
	TH0 = 0xF8;		// Set the initial timing value
	smg_display(); // Digital display

}
Copy the code

Look, this single chip code is a dome I wrote in school. It can be seen that using void main()…

int main(void)

The return value is int and the argument is void, which is a common notation. The void parameter indicates that it cannot pass in any arguments when called, so it cannot take command line arguments.

int main()

The return value is int with no arguments, which may look like int main(void), but the form is a little different. Although its declaration has no input arguments, the actual call can pass in arguments. Here’s an example:

#include<stdio.h>


void test(a);

int main(a) { 

    test(10);

    return 0; 
}

void test(int a){

    printf("%d\n",a);
}
Copy the code

Compile and run:

Isn’t that amazing?

The return value is int and has two input arguments

int main(int argc,char *argv[])
Copy the code

And this is the most common way to write it. The first entry is the number of command-line arguments, and the second is the array of command-line arguments. Typically used to implement functions that require parameters from the command line.

#include <stdio.h>

int main(int argc, char ** argv){
	
    int i;
    for (i=0; i < argc; i++)
        printf("Argument %d is %s.\n", i, argv[i]);

    return 0;
}
Copy the code

Compile and run:

The return value is int and has three input arguments

#include <stdio.h>

int main(int argc, char ** argv,char *env[]) {

	int i;


	for (i = 0; i <= argc; i++){
		printf("argv[%d] = %s\n", i,argv[i]);
	}
	

	for (i = 0; env[i] ! =NULL; i++){
		printf("env[%d] = %s\n", i,env[i]);
	}

	return 0;
}
Copy the code

Compile and run:

This form comes mostly from compiler extensions. However, the global environ variable can take the place of env, and getenv or putenv can be used to fetch or set environment variables, so this form is not necessary.

conclusion

In the C89/C99/C11 standard documentation. There are only two ways to write main

int main(void) { /* … */ } int main(int argc, char argv[]) { / … */ }

That said, the rest of the writing is not up to standard, some are legacy issues, some are compiler extensions, and some come from nowhere.

Welcome to pay attention to the public number [program ape code], add my wechat account (17865354792), reply: get learning materials. Or reply: enter the technical exchange group. The web disk information is as follows: