tags:

views:

265

answers:

4

Possible Duplicate:
main() in C, C++, Java, C#

I'm new to programming in general, and C in particular. Every example I've looked at has a "main" function - is this pre-defined in some way, such that the name takes on a special meaning to the compiler or runtime... or is it merely a common idiom among C programmers (like using "foo" and "bar" for arbitrary variable names).

A: 

No, or you couldn't define one.

It's not predefined, but its meaning as an entry point, if it is present, is defined.

Jon Hanna
+12  A: 

No, you need to define main in your program. Since it's called from the run-time, however, the interface your main must provide is pre-defined (must return an int, must take either zero arguments or two, the first an int, and the second a char ** or, equivalently, char *[]). The C and C++ standards do specify that a function with external linkage named main acts as the entry point for a program1.

At least as the term is normally used, a predefined function would be one such as sin or printf that's in the standard library so you can use it without having to write it yourself.

1If you want to get technical, that's only true for a "hosted" implementation -- i.e., the kind most of us use most of the time that produces programs that run on an operating system. A "free-standing" implementation (one produces program that run directly on the "bare metal" with no operating system under it) is free to define the entry point(s) as it sees fit. A free-standing implementation can also leave out most of the normal run-time library, providing only a handful of headers (e.g., <stddef.h>) and virtually no standard library functions.

Jerry Coffin
True, my MCU doesn't care what I call the entry point, but I'm boring and point my reset vector to a void `main` :P
Nick T
+7  A: 

Yes, main is a predefined function in the general sense of the the word "defined". In other words, the C language standard specifies that the function called at program startup shall be named main. It is not merely a convention used by programmers as we have with foo or bar.

The fine print: from the perspective of the technical meaning of the word "defined" in the context of C programming, no the main function is not "predefined" -- the compiler or C library do not supply a predefined function named main. You need to define your own implementation of the main function (and, obviously, you should name it main).

Dan Moulding
+1 for taking into account he is new to programming.
Johannes Schaub - litb
Pedantic: The name of the function, its parameters and return type are **defined** by the language specification. *The content of the 'main' function is not defined by the language, and must be defined by the programmer.*
Thomas Matthews
@Thomas Matthews: To be even more pedantic, in C parlance, the name of a function, its parameters and its return type merely constitute a function *declaration*, not a definition.
Chuck
A: 

There is typically a piece of code that normal C programs are linked to which does:

extern int main(int argc, char * argv[], char * envp[]);
FILE * stdin;
FILE * stdout;
FILE * stderr;


    /* ** setup argv  ** */
    ...

    /* ** setup envp  ** */
    ...

    /* ** setup stdio ** */
    stdin = fdopen(0, "r");
    stdout = fdopen(1, "w");
    stderr = fdopen(2, "w");

    int rc;
    rc = main(argc, argv, envp); // envp may not be present here with some systems
    exit(rc);

Note that this code is C, not C++, so it expects main to be a C function. Also note that my code does no error checking and leaves out a lot of other system dependent stuff that probably happens. It also ignores some things that happen with C++, objective C, and various other languages that may be linked to it (notably constructor and destructor calling, and possibly having main be within a C++ try/catch block).

Anyway, this code knows that main is a function and takes arguments. If your main looks like:

int main(void) {

Then it still gets passed arguments, but they are ignored.

This code specially linked so that it is called when the program starts up.

You are completely free to write your own version of this code on many architectures, but it relies on intimate knowledge of how the operating system starts a new program as well as the C (and C++ and possibly Objective C) run time. It is likely to require some assembly programming and or use of compiler extensions to properly build.

The C compiler driver (the command you usually call when you call the compiler) passes the object file containing all of this (often called crt0.0, for C Run Time ...) along with the rest of your program to the linker, unless told not to.

When building an operating system kernel or an embedded program you often do not want to use the standard crt*.o file. You also may not want to use it if you are building a normal application in another programming language, or have some other non-standard requirements.

nategoose