0

This is a curious question i got from my sir. He said if we don't put return type before main() it will return a garbage value. He said that this garbage value is returned to a function in the C compiler.

  1. Which compiler function is responsible for that?
  2. What will happen if this compiler functions parameter is a garbage value?

e.g. :

main()
{

 printf("Hello world");

} //will return a garbage value since no return type
quantdev
  • 23,517
  • 5
  • 55
  • 88
  • The name of the function is not required to be the same for every compiler. Some compilers have a crt0.o object file with those functions defined in that object file. At link time the c code with that object. Names of the functions are usually something like _start or _startup. Example: If you are on Linux/UNIX you find the name by compiling a tiny 'Hello World' program, then use the nm command to list the functions (entrypoints) in the compiled file. – jim mcnamara Oct 11 '14 at 02:56
  • ' this garbage value is returned to a function in the C compiler' - that is not possible because the compiler is not running when the executable program is. – Martin James Oct 11 '14 at 03:54
  • 1
    Related: http://stackoverflow.com/q/25434850/694576 and http://stackoverflow.com/q/13200949/694576 – alk Oct 11 '14 at 08:14

5 Answers5

3

There isn't necessarily a name for the function that calls main() and arranges to exit the program with the value returned by main(). The object file often has a name such as crt0.o and is responsible for calling main() and calling exit() with the returned value.

The code main() { …; /* No return */ } is invalid in C99 and C11; you must specify a return type (of int — see What should main() return in C and C++?). In the C89/C90 standard, you could write main() without a return type, but you were then obliged to return a value explicitly from main() or you would, indeed, get garbage returned.

However (in what some people consider a mistake, though it is compatible with C++), C99 legislated that if you don't explicitly return a value from int main(void) { …; /* No return */ }, the compiler effectively inserts return 0; for you.

Thus, the code shown is C89/C90 (or pre-standard) C and should have a return.

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
1

I don't believe there is any convention for the name of such function, but it is responsible to use the return value of main() to set the ERROR LEVEL value recorded by the O/S. One can normally check that value (usually in batch file processing) to determine if the program completed successfully (0) vs. an error indication, with possible meaning to the different non zero values.

Ron Pinkas
  • 357
  • 2
  • 10
1

In standard C (and C++), main must have the return type int. If there is no return type, perhaps some random four bytes of memory will be returned. And in fact, only one byte will be used on many systems (like Unix and Linux) because return values from programs are always in [0,255]. If you return a garbage value, most systems will interpret zero as success and non-zero as failure, so invocations of your program may appear to "fail" from the outside caller's perspective.

On the other hand, some systems may tolerate this sort of misstep, because lots of legacy code does it, and simply return 0 for you.

There is some "startup" code which is run before main() usually. This is platform-dependent, but somehow it needs to communicate the parameters and return value of main from and to the operating system.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
0

If you add the -v flag to your invocation, i.e. g++ -std=c89 -x c -O2 -Wall -pedantic -pthread -v main.cpp, you'll notice that gcc adds some object files to your linker, i.e. crtbegin.o, crtend.o, crti.o and crtn.o, as well as crt1.o or crt0.o (it could be either.) The latter contains the entry point function (i.e., _start) that initializes the process and calls exit(main(argc, argv)).

Note this is actually implementation and operating system dependent (but gcc on linux was used as an example.)


In C89, if you don't specify a return value for main, the value returned is undefined.

2.1.2.2 Hosted environment

A return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument. If the main function executes a return that specifies no value, the termination status returned to the host environment is undefined.

In C99, you cannot declare a function without a return type (the implicit int rule is removed.) Compared to C89, C99 is explicit about the return type of main:

It shall be defined with a return type of int and with no parameters: [...] or in some other implementation-defined manner.

It also removed the undefined return value:

5.1.2.2.3

1 If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument;10) reaching the } that terminates the main function returns a value of 0. If the return type is not compatible with int, the termination status returned to the host environment is unspecified.

0

Which compiler function is responsible for that?

None. The value is returned to the environment. In an operating system environment that value may be used to determine the status of the program execution. By convention zero is success, non-zero an error. The value can be used by shell scripts or other programs that launch processes - The make utility for example uses it to determine whether a build step has failed.

When a C program is run, it is loaded and started by the OS, then start-up code included by the linker performs tasks such as setting the stack pointer, initialising static data, initialising the library, then calling main(). When main() returns, this came code may perform some clean-up before the OS unloads the code and performs its own clean-up.


What will happen if this compiler functions parameter is a garbage value?

A non-zero value may be interpreted as an error condition, but only is something s explicitly checking it and interpreting it that way - such as a shell script, batch file or make for example. The OS does nothing with it other than setting an environment variable ERRORLEVEL, so the value can be interrogated after the event.

Clifford
  • 88,407
  • 13
  • 85
  • 165