Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What these C's main function indicates? [duplicate]

Tags:

c

main

Possible Duplicate:
Why is the type of the main function in C and c++ left to the user to define?

I've come across many C's main function style and syntax of writing but I'm not actually getting what this syntax mean can anybody give brief on each syntax? why void? 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[]) {

}
like image 360
Vishwanath Dalvi Avatar asked Feb 01 '12 06:02

Vishwanath Dalvi


People also ask

What is the main function of C?

Every C program has a primary function that must be named main . The main function serves as the starting point for program execution. It usually controls program execution by directing the calls to other functions in the program.

What is C repetition keyword?

Other than a sequence code execution, a common loops (repetition) in C/C++ programming is the for,while and do-while loops. For conditional execution (selection) we can use if and its variations such as if-else, if-else-if and switch-case-break.

What if there are two main functions in C?

No, you cannot have more than one main() function in C language. In standard C language, the main() function is a special function that is defined as the entry point of the program.


2 Answers

There are two forms of conforming implementations specified by the c standard:

  • Hosted Implementation &
  • Freestanding Implementation

There are based on two types of envrionments that the c standard defines as:

  • Hosted environment &
  • Freestanding environment respectively.

What is freestanding Environment & What is Hosted Environment?

A freestanding implementation is one that is designed for programs that are executed without the benefit of an operating system. For Ex: An OS kernel or Embedded environment would be a freestanding environment.

A program using the facilities of an operating system would normally be in a hosted implementation.

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

How a C program begins execution in both these environment differs.
For an Freestanding environment, the program startup can happen by any implementation defined function. There is no requirement that even a main() should exist.

So any of the functions definitions mentioned in the question can be valid depending upon implementation for that Freestanding Environment. And their function parameters and return values will have implementation defined meaning, So you will need to check their documentation to know their precise meanings.

Reference:

5.1.2.1 Freestanding environment

In a freestanding environment (in which C program execution may take place without any benefit of an operating system), the name and type of the function called at program startup are implementation-defined. Any library facilities available to a freestanding program, other than the minimal set required by clause 4, are implementation-defined.

For an Hosted environment the standard mandates the program execution begins by execution of a main() function and it also mandates how this function will be defined.

The specifications for the same are given in:

C99 Standard: 5.1.2.2 Hosted environment

5.1.2.2.1 Program startup

1 The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

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

or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

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

or equivalent; or in some other implementation-defined manner.

like image 135
Alok Save Avatar answered Oct 05 '22 12:10

Alok Save


void as the return type means the author meant to return no value of significance to the caller (invoker of the program). Depending on the operating system, this may be acceptable, or might cause subtle difficulties with whatever starts the program.

void as the parameter means that the program will not be using the conventional means of inspecting command line arguments. Some environments provide alternative ways of obtaining them; others do not. In the latter case, it means the program ignores any command line parameters.

main (int) allows the program to inspect the number of parameters passed to the program on the command line, but does not inspect their values. Such an arrangement is uncommon, but one program which does this on many implementations is the Unix/Linux who command which lists logged in users, except that who am i lists only the current user. Just as who is you does (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 ~]$ 
like image 30
wallyk Avatar answered Oct 05 '22 14:10

wallyk