What does this core C function indicate?

Possible duplicate:
Why is the type of the main function in C and C ++ left to the user to determine?

I came across many basic styles and syntax for writing C, but I really don't understand what this syntax means, can anyone give a brief description of each syntax? why emptiness? why int? why void, int as parameter?

void main() { } int main() { } int main(void) { } void main(void) { } int main(int) { } int main(int argc, char* argv[]) { } 
+4
source share
3 answers

There are two forms of corresponding implementations defined by the c standard:

  • Hosting and
  • Freelance implementation

They are based on two types of envrionments, which the c standard defines as:

  • Hosting Environment &
  • Freelance environment, respectively.

What is a freestanding Environment and what is a freestanding Environment ?

A freelance implementation is a program designed for programs that run without using the operating system. For Ex: The OS kernel or embedded environment will be a standalone environment.

A program that uses the operating system is usually located in a hosted version.

How is a c program executed in these two environments? What is the difference?

How C runs in both of these environments. For a Freestanding environment, a program can start using any function defined by a definition. It is not required that even << 22> exist .

Thus, any of the function definitions mentioned in the question may be valid depending on the implementation for this autonomous environment. Both their function parameters and return values ​​will have a specific implementation value, so you will need to check their documentation to find out their exact values.

Link:

5.1.2.1 Freelance

In a stand-alone environment (in which C program execution can be performed without any advantage of the operating system), the name and type of function called in the startup program are implementation specific . Any libraries available for a stand-alone program other than the minimum set required in accordance with clause 4 are determined by the implementation.

For a hosted environment, the standard indicates that program execution begins with the main() function, and also determines how this function will be defined.

Specifications for them are given in:

C99 Standard: 5.1.2.2 Hosting Environment

5.1.2.2.1 Starting the program

1 The function called when the program starts is called main. The implementation does not declare a prototype for this function. It is defined with an int return type and without Parameters:

  int main(void) { /* ... */ } 

or with two parameters (called argc and argv here, although any names can be used since they are local to the function in which they are declared):

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

or equivalent; or some other way of implementation.

+8
source

void , since the type of the return value means that the author would like to return the significance value to the caller (invoker of program). Depending on the operating system, this may be acceptable or may cause slight difficulties when starting the program.

void , because this parameter means that the program will not use the usual means of checking command line arguments. Some environments provide alternative methods for their preparation; others do not. In the latter case, this means that the program ignores any command line options.

main (int) allows the program to check the number of parameters passed to the program on the command line, but does not check their values. This arrangement is unusual, but one of the programs that does this in many implementations is the Unix / Linux who command, which displays a list of registered users, except that who am i contains a list of only the current user. Just like who is you (because both have two parameters:

 [ wally@lenovotower ~]$ who wally tty1 2012-01-31 22:24 (:0) wally pts/0 2012-01-31 22:24 (:0) wally pts/1 2012-01-31 22:33 (:0) wally pts/2 2012-01-31 22:34 (msi) root pts/3 2012-01-31 22:34 (msi) [ wally@lenovotower ~]$ who am i wally pts/0 2012-01-31 22:24 (:0) [ wally@lenovotower ~]$ who are you wally pts/0 2012-01-31 22:24 (:0) [ wally@lenovotower ~]$ 
+2
source

OK, briefly.

If you do not need the specified command line parameters, and you do not want to set the exit code, you can write this:

 void main() 

There are no parameters, but you want to set the exit code:

 int main() 

Same as before, but in the old C style:

 int main(void) 

Same as first, but old-style C:

 void main(void) 

If you want to know only the number of command line options:

 int main(int) 

And the most complete option:

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

argc is the parameter counter, argv is the array of parameters, and the function returns the exit code.

-one
source

Source: https://habr.com/ru/post/1394129/


All Articles