-5

I like to know what is the main reason that all C programs should start with the standard function of "int main()"? and why it should be "int" ? Thank you

tinlyx
  • 18,900
  • 26
  • 81
  • 148
Arash.H
  • 81
  • 1
  • 9
  • 2
    They have to start *somewhere*... – Csq Sep 21 '14 at 23:59
  • 1
    And for the why `int` return value: [What should main() return in C and C++?](http://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c) – Csq Sep 22 '14 at 00:00
  • 4
    The short version is: because the standard says so. – user657267 Sep 22 '14 at 00:00
  • I think the OP is more interested in why the main() function has to have certain signature/return type, rather than why programs have to have a starting point, as in the question @Csq linked. I edited the title to avoid potential misunderstanding IMHO. – tinlyx Sep 22 '14 at 00:07
  • Well, if it has to be linked with the 'crt' C runtime that will call it, the signatures must match. – Martin James Sep 22 '14 at 00:32

7 Answers7

2

Not all C "programs" start with "int main." I put program in quotes to highlight the fact that you can have c files that do not have main in them, such as class definitions. Essentially main is the section of code that tells the computer what to do, and in what order -- all other files can be thought of as helper files that partition the code to make it more readable and maintainable. Main has an int type because the program will return an integer value describing if the program executed without a problem, which corresponds to a return of 0, or what went wrong, which can be any non-zero number that usually has documentation that will tell you what the corresponding number meant in terms of failure.

user3854447
  • 215
  • 3
  • 15
1

When you execute a program, the first thing to run is initialization code in the C library. When it finishes, it calls the standard entry point, main, with the parsed command line arguments. The int return value from main is the return value of the program.

stark
  • 10,399
  • 2
  • 29
  • 44
1

Actually, not all C programs have to have a main. The ISO C standard supports two types of environments. The first is hosted, and that is the one that requires a main of a specific format (from several allowable ones).

Freestanding environments (the second type) have no such requirement and, in fact, there's a lot of leeway for freestanding behaviour at many other points in the standard as well.

That's why you can still consider the Linux kernel to be a C program despite the fact there's not a main in sight.


As to why hosted environments have this requirement, that's the way C was originally written back in the 70s and, when ANSI came to do the standard, their primary brief was to codify existing practice, not create a new language.

The long chain of standards committees following that, ISO C89/90, C99 and C11 have either not been convinced that there was any need to change it, or their contributors have not put forward the idea.

paxdiablo
  • 772,407
  • 210
  • 1,477
  • 1,841
0

A program has to start running somewhere; this is called its "entry point". The standard entry point for a C program is called "main" because that's what someone chose long ago and there's no real reason to change it to some other name. However, some compilers (actually, linkers) have options to build a program using some other function name as the entry point.

The main function returns an int to provide feedback about whether it ran successfully. By convention, returning 0 indicates that the program succeeded, and returning some other value indicates that it failed. You can check this result with, for example, the $? environment variable in the bash shell.

Wyzard
  • 31,764
  • 3
  • 60
  • 78
0

This is because of the fact how your code is to be called and the Standard says how it should happen. As to the return value, there is a rule that says that main function has to return the integer as a result of a call to the caller - it should indicate how the program exited.

4pie0
  • 27,469
  • 7
  • 70
  • 110
0

Why all c programs should start with int main ()?

This is because:

When the operating system runs a program in C, it passes control of the computer over to that program. This is like the captain of a huge ocean liner handing you the wheel. Aside from any fears that may induce, the key point is that the operating system needs to know where inside your program the control needs to be passed. In the case of a C language program, it's the main() function that the operating system is looking for.


and why it should be "int" ?

5.1.2.2.1 Program startup:

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 [...]

haccks
  • 97,141
  • 23
  • 153
  • 244
  • Well, OS loader raises a thread to run the process at the entry point defined in the executable header. In the case of C or C++, that would likely be the crt entry point. The crt sets up the C environment, (and calls C++ static ctors if required), before calling main(). – Martin James Sep 22 '14 at 00:29
  • The OS has no idea whether the executable was written in C or not. – Martin James Sep 22 '14 at 00:33
0

it is a matter of standard, check out ansi C standard it states that main function should return int value. Also from operating system point of view returning value from main function is only indicator if program ended properly.

djra
  • 3,475
  • 2
  • 14
  • 12