1

I'm creating a presentation on how to program in C, and since I'm fairly new to C, I want to check whether my assumptions are correct, and what am I missing.

Every C program has to have an entry point for the OS to know where to begin execution. This is defined by the main() function. This function always has a return value, whether it be user defined or an implicit return 0;.

Since this function is returning something, we must define the type of the thing it returns.

This is where my understand starts to get hazy...

  1. Why does the entry point needs to have a return value?
  2. Why does it have to be an int?
  3. What does the OS do with the address of int main() after the program executes?
  4. What happens in that address when say a segfault or some other error halts the program without reaching a return statement?
Drew McGowen
  • 10,984
  • 1
  • 27
  • 55
Morgan Wilde
  • 15,065
  • 9
  • 47
  • 94
  • 1
    The return type is `int` because the language standard says so. (Actually other implementation-defined forms are permitted, but the only portable forms are `int main(void)` and `int main(int argc, char *argv[])` or equivalent.) – Keith Thompson Nov 15 '13 at 20:27
  • A4: there's no "address" to consider. When a program terminates due to some unrecovered situation such as segfault, the operating system decides what the exit code will be. The `main()` function doesn't get a chance to influence that if the app is being forcefully terminated. That being said, it's possible to intercept segfault (and most other causes of termination) to allow the app to terminate on its own terms -- specified exit code, close resources, etc. – mah Nov 15 '13 at 20:30
  • #2, a shot in the dark here as to the *why*: considering the standard defines that `int` "has the natural size suggested by the architecture of the execution environment", and thus is the most efficient type, it would make sense to use it as the return type for `main`. – netcoder Nov 15 '13 at 20:42

2 Answers2

4

Every program terminates with an exit code. This exit code is determined by the return of main().

Programs typically return 0 for success or 1 for failure, but you can choose to use exit codes for other purposes.

Havenard
  • 23,249
  • 4
  • 31
  • 59
2

1 and 2 are because the language says so.

For 3: Most operating systems have some sort of process management, and a process exits by invoking a suitable operating system service to do so, which takes a status value as an argument. For example, both DOS and Linux have "exit" system calls which accept one numeric argument.

For 4: Following from the above, operating systems typically also allow processes to die in response to receiving a signal which is not ignored or handled. In a decent OS you should be able to distinguish whether a process has exited normally (and retrieve its exit status) or been killed because of a signal (and retrieve the signal number). For instance, in Linux the wait system call provides this service.

Exit statuses and signals provide a simple mechanism for processes to communicate with one another in a generic way without the need for a custom communications infrastructure. It would be significantly more tedious and cumbersome to use an OS which didn't have such facilities or something equivalent.

Kerrek SB
  • 428,875
  • 83
  • 813
  • 1,025
  • 2
    Your last paragraph also makes a decent explanation for #1. :) – netcoder Nov 15 '13 at 20:44
  • As a addition, the return value of main gives to the OS some information about the way the program executed. 0 means success, while a non-zero value means that some error occured. For further information, you may take a look here: http://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c – Paul92 Nov 15 '13 at 20:48
  • Thanks @KerrekSB for taking the time to explain this thoroughly! – Morgan Wilde Nov 15 '13 at 20:49