Why C Language Has the Main Function
Why Do Some Programing Language Require a Function Named Main? is it Necessary?

Why do most compiled programing language have “main” function? e.g. C Cpp Java. Is it necessary?
- The “main”, is man-made complexity.
- The concept does not exist in computer science, nor in math. (similar to closure.)
- It is not necessary.
here's a mathematician's view of what does programing means:
- We write a program, in a programing language, which is a language to specify algorithms, which is a sequence of step-by-step instructions, and this, is run on a specific machine.
- (think of an abacus as machine, and a language that says column n move bead 3 up/down as the programing language. A list of such instructions as the source code.)
- In the source code, the code runs from top to bottom.
- Such as defining functions, and calling the function.
- This top to bottom is always necessary, unless the programing language is designed in a special way to have other order (such as JavaScript function name-hoisting)
- Theoretically you never need to specify a “main” about where to “begin”.
- Again, unless the programing language for some reason is designed that way, and does something extra to make that work.
- So, why do we need main?
- I had this question for 20 years.
- Finally understand it today.
- The reason many programing languages such C Cpp Java have main, is due to historical reasons, specifically on how electronic computer happened to be designed and evolved, and the way we do things.
- C began it, and Cpp Java etc followed it by force of habit.
- Basically, code is compiled to several object files (for human management reason).
- The object files, each is a binary number.
- In the object files, parts are function definitions, parts are data, parts are instructions.
- We developed ways to distinguish them by various means (by binary marker, file name, or convention, etc.)
- And a so-called linker assembles them into so-called executable.
- All of the above, relates to how the physical machine is actually designed and how we created convention to work on them.
- so, in this convention, for technical reasons, the linker needs to know, where is the actualy instruction part that the machine should read to run the program.
- and by convention, that is the “main”, as a symbol in binary, searched by another program of so-called loader.
Languages Without Main
- JavaScript
- Ruby, Python, Perl, PHP
- Tcl, Lua
- VBScript
- Bash, PowerShell
- Emacs, Lisp, Common Lisp, Scheme, Racket
- Wolfram, R, MATLAB
- SQL
- Erlang, Elixir
- Prolog
- Forth
- Smalltalk
- Self
- Julia
- ML, SML, Ocaml
- Fsharp (in scripts)
- Clojure, Groovy
- Swift
- FORTRAN
Languages require Main
- C, Cpp
- D
- Java, Scala, Kotlin
- Go
- Rust
- Dart
- Zig
- Nim
- Ada
- Pascal
- Haskell
Grok Ai Explains Linker and Main
- grok ai explains linker, main. and in compiled languages sans main, how do they do linking? e.g. Julia, Ocaml, Clojure, Swift.
- https://x.com/i/grok/share/Eio3LlbW5NdMubClCvXweFWsM
Previous Exploration



Where did C lang's main came from?
Here's a sketch of history of where did C get its main
.

- Why do programming languages have a main() function?
- By Alexwlchan.
- https://alexwlchan.net/2020/why-do-programming-languages-have-a-main-function/
- ok.
main
, came from C (1972). and its daddy B (1969). - and the concept may came from BCPL (1967), which uses
start()
. - the main() do not exist before that. e.g. not in fortran (1957), lisp (1960). A decade before the C virus.
The real reason main is invented
- the first few paragraphs of the wp linker article makes it clear

- u see, they talk about object files, and refer to each other as symbols, and addresses.
- so, to run a program, the program in the end is bunch of machine code in disparate parts in the hardware.
- this is how things are , actually, in the era of electronic computers, or so i think.
- now, this is opposed to, modern times, or earlier times. for example, in modern times, we think of a python script, or ruby, bash, js, as something like a natural language, we read it from top to bottom. The meaning of the code is accumlated sequentially in order, like english.
- from a high level of view, a language, not for human but for computers, is like this. similar to human langs.
- and this is indeed so, for most high level langs, or scripting langs eg python ruby bash pwsh, or functional langs eg lisp wolfram, or math langs eg apl matlab.
- or, in more primitive times, eg punch cards, or hand written instructions on how to solve math problems, algorithems r written in a script, to be executed from top to bottom in order.
- but in the electronic computer era, eg the times of c, it created this... aberration, due to the concept of 'compilation'.
- in this scheme, u do not start by writing a program of step by step instructions of an algorithm. instead, u need to setup the machine hardware, so each component needs to contain certain code. and in order to do that easily, u created the concept of c, that 'compiles', into these diff 'objects', to be placed in diff places in the hardware.
- so, the source code of c, is not really, meant to be sequential spec of something. rather more like, a bunch of modules. as georrg said.
Programing Languages and their Machines
- Programing Language and Its Machine
- Programing Languages and Their Computational Models
- Costs of Programing Language Operations
- Why C Language Has the Main Function
- What is Closure in Programing Language (2012)
- Computer Science, Modeling Modern Software and Hardware on an Abacus
- Why is Array Access Constant Time
- On GPU vs CPU Algorithms Compilation, Cpp, Parallel Computation, Wolfram Physics (2023)