5

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[]) {

}
Community
  • 1
  • 1
Vishwanath Dalvi
  • 33,724
  • 39
  • 121
  • 151
  • 2
    Neither `void main()` or `void main(void)` are valid. The `main` function must return a value of type `int`. – Cody Gray Feb 01 '12 at 06:29
  • @CodyGray: ... except in a "freestanding environment" (5.1.2.1). – undur_gongor Feb 01 '12 at 08:28
  • @undur_gongor: In fact a Freestanding Environment might not even have a `main()`. – Alok Save Feb 01 '12 at 08:38
  • The complete answer can be found [here](http://stackoverflow.com/questions/5296163/why-is-the-type-of-the-main-function-in-c-and-c-left-to-the-user-to-define/5296593#5296593). The first line of that answer also applies to the answers posted here... – Lundin Feb 01 '12 at 09:20

3 Answers3

8

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.

Alok Save
  • 196,531
  • 48
  • 417
  • 525
  • -1 Since this answer only applies to a hosted environment. 5.1.2.2.1 is part of the chapter "hosted environment" (5.1.2.2). – Lundin Feb 01 '12 at 09:18
  • 2
    @Lundin: Well, I already had an comment regarding *Freestanding Environments* under the Question long before you posted this comment.As for the answer, it says nothing wrong. – Alok Save Feb 01 '12 at 09:23
  • The answer is strictly correct, but misleading and incomplete. There is a whole lot of programmers who incorrectly believe that `void main()` is never allowed in C, and they need no more fuel. – Lundin Feb 01 '12 at 09:25
  • @Lundin: There you go.Updated the answer to clarify in detail, since I feel there is truth to *lot of programmers who incorrectly believe that void main() is never allowed*. – Alok Save Feb 01 '12 at 09:40
  • Ok from downvote to upvote. (Also as a parenthesis, the C and C++ standards are equivalent in this particular matter.) – Lundin Feb 01 '12 at 09:55
2

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 ~]$ 
wallyk
  • 55,472
  • 16
  • 84
  • 144
  • "Depending on the operating system, this may be acceptable". No. If you have an operating system you are running on a _hosted_ environment and then your main _must_ return int. C99 5.1.2.2.1. – Lundin Feb 01 '12 at 09:28
  • @Lundin: Not so. Many operating systems have a different mechanism for returning a program status value: pSoS, iRMX86, RT-11, GCOS, lots of embedded O/S's. Even OpenVMS, a fairly standards-compliant O/S "translates" main's return value if it is zero. – wallyk Feb 01 '12 at 09:50
  • It doesn't matter. The C standard explicitly tells us that the main function _must_ return int in a hosted environment. There is no way around it, return int or your program does not conform to the C standard. The C compiler for a particular OS will perhaps have to dissolve that int into thin air, but that is the concern for the compiler and not the programmer. – Lundin Feb 01 '12 at 09:58
  • 1
    @Lundin: OP's question is not about *Standard C*, but about the realities of real programs written for real systems. The C standard is not mandatory for implementation, only for *compliant implementations*. – wallyk Feb 01 '12 at 10:04
  • What can I say... there is only one C language, as defined by ISO 9899. If someone tags their question C and I give them an answer in C++ or Object C or MyOwnMumboJumboC, then I think my answer is off topic. The allowed exceptions to the standard for a _conforming implementation_ are also specified by the standard, see chapter 4. – Lundin Feb 01 '12 at 10:11
-1

ОК, briefly.

If you don't care about command line parameters user specified and you don't want to set an exit code you may write this:

void main()

No parameters, but want to set an exit code:

int main()

The same as previos, but in old C style:

int main(void)

The same as the first one, but in old C style:

void main(void)

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

int main(int)

And the most complete variant:

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

argc is a count of parameters, argv is an array of parameters and the function returns an exit code.

Lenar
  • 296
  • 3
  • 7
  • The parts of this answer where you suggest that `void main()` and/or `void main(void)` might be valid are incorrect. I don't know what you mean by "old C style", but `main` didn't return `void` then, either. – Cody Gray Feb 01 '12 at 07:39
  • This answer is just confusing and doesn't mention hosted/freestanding environments. Some of those forms are incorrect in a hosted environment. I don't know what "old C style" is either. – Lundin Feb 01 '12 at 09:22
  • @CodyGray: Only if you assume a hosted environment, which the OP did not assume. – Lundin Feb 01 '12 at 09:22
  • @Lundin: Yes, there's always an embedded C programmer who comes along to say, "Well, actually, in certain cases that the average programmer will never work in, this isn't true because the implementation defines something different and the standard technically says that it can be implementation defined." Yes. All of that is correct. And yes, indeed, the asker never specified whether it was a hosted environment or not. But the reality is that most of C programmers work in a hosted environment, and nearly everyone **learning** the C language are doing so in a hosted environment. – Cody Gray Feb 01 '12 at 20:26
  • Given that this is such a rampant source of confusion, and every embedded developer that I've ever met is aware that their situation is an *exception* to the rule, it seems perfectly acceptable to teach people the rule without the exception in order to avoid such confusion. If and when they need to learn the exceptions, they can learn them. At that point, they'll have already mastered C and will understand the difference. (Comments just don't have enough room to express all the infinite caveats to every claim...) – Cody Gray Feb 01 '12 at 20:27
  • @CodyGray: Somewhere around 95% of all computers are embedded systems. Every hosted program made, must by definition run on top of a hostless program. So quantitatively, the average program is _not_ a hosted program. Also, as you can tell by a brief look at any embedded forum, there are numerous people learning C on freestanding environments. – Lundin Feb 02 '12 at 09:02
  • @CodyGray: It is _not_ acceptable to teach people that "main must always return int", because I've had several cases where beginner embedded programmers implemented main with int because they were taught that way, and then get calling convention and stack issues because of it. You seem to think that people first "master" C, then become embedded programmers. This is just prejudices, even if we disregard the beginners, there are countless electronic engineers who pick up C programming after doing stuff like VHDL and assembler. And those will not know a jack about hosted environments. – Lundin Feb 02 '12 at 09:06
  • *"Somewhere around 95% of all computers are embedded systems."* A straw man argument. Nice job, thanks for playing. – Cody Gray Feb 03 '12 at 02:31