Real Form of main function, true form of main Function

Source: Internet
Author: User

Real Form of main function, true form of main Function

We often used forms like int main () in the past, but what exactly does the full body of the main function look like?

The form of the main function below may be somewhat unexpected:

int main(int argc, char *argv[], char *envp[])

It may be different from what is described in most textbooks, but in fact, this is the real complete form of the main function.

The argc parameter specifies the number of command line parameters when the program is run. The array argv stores all command line parameters, and the array envp stores all environment variables. Environment Variables refer to a set of values that have existed since user login. Many applications need to rely on them to determine system details. Our most common environment variables are PATH, it points out where to search for application procedures, such as/bin; HOME is also a common environment variable, which points out our personal directory in the system. Environment variables generally exist as strings "XXX = xxx". XXX indicates the variable name, and xxx indicates the variable value.

It is worth mentioning that the argv array and the envp array both store pointer to the string. Both arrays end with a NULL element.

We can use the following program to view what is uploaded to argc, argv, and envp:

/* main.c */int main(int argc, char *argv[], char *envp[]){printf("\n### ARGC ###\n%d\n", argc);printf("\n### ARGV ###\n");while(*argv)printf("%s\n", *(argv++));printf("\n### ENVP ###\n");while(*envp)printf("%s\n", *(envp++));return 0;}

Compile it:

$ cc main.c -o main

When running, we intentionally add a few command line parameters that do not have any function:

$ ./main -xx 000### ARGC ###3### ARGV ###./main-xx000### ENVP ###PWD=/home/leiREMOTEHOST=dt.laser.comHOSTNAME=localhost.localdomainQTDIR=/usr/lib/qt-2.3.1LESSOPEN=|/usr/bin/lesspipe.sh %sKDEDIR=/usrUSER=leiLS_COLORS=MACHTYPE=i386-redhat-linux-gnuMAIL=/var/spool/mail/leiINPUTRC=/etc/inputrcLANG=en_USLOGNAME=leiSHLVL=1SHELL=/bin/bashHOSTTYPE=i386OSTYPE=linux-gnuHISTSIZE=1000TERM=ansiHOME=/home/leiPATH=/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/lei/bin_=./main

We can see that the program uses "./main" as 1st command line parameters, so we have three command line parameters. This may be a little different from what you usually get used to. Be careful not to make a mistake.

Finally, I like it.:

A person must always follow a strange path, look at strange scenery, and listen to strange songs. Then, in a casual moment, you will find that what you originally wanted to do with all your efforts, you can easily face it without knowing it.


Main () function with Parameters

To put it simply
Int argc; // indicates the number of strings read.
Char * argv []; // pointer array, used to store the read string
The program outputs all read strings one by one. Each string has an empty tab interval.

Main Function Type

Yes, but only main ().

Int main (void)
{Return 0 ;}

Int main (int argc, char argv * [])
{Return 0 ;}

Int main (int argc, char * argv [] char * envs [])
{Return 0 ;}

These three forms are recommended. The main function should have a return value. The system should judge the running status of the program based on the return value.

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.