Why there is always main function in C?

Viewed 518

When writing code using C, the code must include the main function. I always had a question about this. In the case of Python or JavaScript, the main function is not required. However, in the case of C, why should I include the main function?

Does this improve performance?

Or is it just to distinguish between the main part and the other part?

I'd appreciate it if you could explain the reason.

3 Answers

There's two primary reasons, and they're sort of related.

Firstly, for any given executable it's important to know where to start executing. In a language like Python there's a script that's executed and while it can include other code, it's understood that there's a "main" script.

This isn't the case in a language like C where many files are compiled and linked together into a final executable. The order in which these files are compiled and linked is largely irrelevant, in general terms they're all treated equally. As such, a "special" function named main is reserved for the entry point.

In C, and by extension C++, there's a lot of things you must do inside of a function, that is not permitted in the main body of your code. In other languages this distinction does not apply. In C you're going to need at least one function to do anything useful, and that function is going to be called main.

This distinction isn't specifically scripting languages like Python versus compiled languages like C. Swift is compiled and yet allows script-like coding where no main() function is required. It's all a function of language design and what makes sense given the constraints.

This also only applies to executables. For libraries there's not one entry point, but as many as you have functions. Any functions you export are able to be called at-will and in any order, at least is as permitted by the API defined by the library.

You could even consider an executable to be a "library" with effectively one function: main().

The main function is called at program startup, after all objects with static storage duration are initialized. It is the designated entry point to a program that is executed in hosted environment (that is, with an operating system). The name and type of the entry point to any freestanding program (boot loaders, OS kernels, etc) are implementation-defined.

The main() function is the first function in your program that is executed when it begins executing, but it's not the first function executed. The first function is _start(), which is typically provided by the C runtime library, linked in automatically when your program is compiled. The details are highly dependent on the operating system and compiler toolchain, so I'm going to pretend I didn't mention it.

You can write C libraries that don't have the main function and call those functions from other C program that has the main.

There is a point required form where the compiler starts to compile the main code this is provided in c using main() function.

Related