Program Entry and Execution
This chapter describes Co’s program entry and execution model. Entry dispatch, optional
main, top-levelreturnand the exit-code model are implemented since V0.2.0; cross-module (import) top-level execution in a centralized topological order is implemented since V0.3.0; the Windows GUI entryWinMainand the[[subsystem("windows")]]subsystem switch are implemented since V0.3.1; the top-level built-in command-lineargc/argvare implemented since V0.5.5 (see “Command-line arguments”).
Top-level statements run in order
A Co program starts at the “top level”: in a .co file, the commands written outside definitions of functions, types, and so on (top-level bare statements) run in the order they appear in the source. You do not have to write a main function to serve as the entry point.
With or without main
To stay compatible with C-style code, Co accepts both kinds of program:
- No
main: the program simply runs its top-level statements in order, then ends. - With
main:mainis an ordinary function, entered only after all top-level statements have run.
GUI entry (WinMain)
Besides the console entry of “top-level statements / main”, Co also supports the Windows GUI entry WinMain:
int WinMain(void* hInstance, void* hPrevInstance, char* lpCmdLine, int nCmdShow)
{
// create the window, run the message loop...
return 0;
}
- Fixed signature: four parameters, returning
int(the ANSI form; the wide variantwWinMainis not yet supported). The return value is the process exit code. - Defining
WinMainautomatically takes the GUI entry: the artifact is a Windows GUI subsystem program (no console window), with no command-line flag needed. - To drop the console while keeping the
main/top-level entry, mark the entry function with[[subsystem("windows")]](see the “Attributes” chapter). WinMainis mutually exclusive with top-level bare statements: a file with bothWinMainand top-level bare statements is a compile error (put the GUI logic insideWinMain);WinMainmust take exactly 4 parameters, otherwise the compiler errors.
Process exit code
The number the program returns to the operating system when it ends (the exit code) is determined as follows:
| Situation | Exit code |
|---|---|
A top-level return V; is reached | V, and the whole program ends immediately (later top-level statements and main are not run) |
Top level finishes with no return, and a main exists | main’s return value |
Top level finishes with no return, and no main exists | 0 |
A bare return; (no value) | 0 |
Key point: a return at the top level ends the whole program (not some function), so it skips main; a return inside a function body still ends only that function (see the “Function Definition” chapter).
Command-line arguments
Since V0.5.5, the .co top-level entry (the case where you do not write main and the compiler synthesizes the console entry) can directly reference two compiler built-in names, argc and argv:
argcis anint, the number of arguments (including the program name itself, soargc >= 1);argvis achar**(a pointer to strings);argv[0]is usually the program’s own path, followed by the arguments in order;- iterate by the
argccount;argv[argc]is alwaysNULL(guaranteed by C).argvis a raw pointer, carries no length property, and has no bounds checking (C semantics) — keep within range usingargcyourself; - both are scoped to the synthesized entry’s top-level code: referencing
argvin a.cfile, in a shared-library artifact (-o *.dll/*.so), or under the GUIWinMainentry makes the compiler report an undeclared identifier.
// hello.co -- no main, read argc/argv directly at the top level
int i = 1; // start at 1, skipping argv[0] (the program name)
while (i < argc) // iterate by the argc count, do not probe past the end
{
// handle argv[i] (e.g. argv[i][0] for its first character)...
i = i + 1;
}
return 0;
If you write an explicit main (C style), arguments still arrive as its parameters, per C convention: int main(int argc, char** argv).
Planned: making
argva globally visible, self-length-carrying string-array value type (readable inside functions and methods, with noargc) depends on Co’s array-carries-own-length infrastructure (same batch asstring/general arrays) and is not yet provided; for now it ships as the C-formargc+char**.
When there is no executable entry
If a program has neither top-level statements nor main (there is genuinely nothing to do) and the target is an executable file: the compiler issues a warning and produces an empty executable that does nothing and exits with code 0 — the build still counts as successful. This rule does not apply when building a dynamic / static library (a library never requires an entry point).
Cross-module execution timing
Since V0.3.0, when a program consists of several modules (.co files) that import one another, each module’s top-level statements follow:
- Exactly once at first load: no matter how many times a module is imported, its top-level code runs only once.
- Centralized topological order: all imported modules’ top-level statements are emitted before the main entry, arranged in DFS-postorder of the dependency closure (dependencies run before dependents). The physical position of
importlines in the main source has no effect on this order. mainof a non-main module is not a load hook: if a module definesmain, it is treated as an ordinary function symbol and does not execute automatically.
See the “import (Module Import)” and “Module” chapters for details.
Currently implemented boundary
Since V0.2.0, entry dispatch and exit-code behavior are implemented as specified above:
- When a file contains top-level bare statements, the compiler synthesizes an entry and runs the top level in source order; if a user-defined
mainalso exists,mainis entered automatically after all top-level statements finish, andmain’s return value becomes the exit code. return V;at the top level ends the whole program immediately with exit codeV; a barereturn;uses exit code0.- A file with neither top-level bare statements nor
main: when producing an executable, the compiler warns and still emits an empty entry that does nothing and exits with code0. - Pure C style (no top-level bare statements + a
main) keeps the traditional C entry chaining and does not enter the synthesized entry. - Windows GUI entry (since V0.3.1): defining
WinMainin the source takes the GUI entry (no console); or mark the entry function with[[subsystem("windows")]]to switch to the windows subsystem.WinMainis mutually exclusive with top-level bare statements and must take 4 parameters. - Command-line arguments (since V0.5.5): the
.cotop-level entry (synthesized consolemain) has built-inargc(int)/argv(char**), iterated by theargccount withargv[argc]==NULL; valid only as built-in names in the synthesized entry’s top-level code (referencingargvin.c/-o *.dll/*.so/WinMainreports undeclared); an explicitmain(pure C style) still receives argc/argv as parameters.
Not yet provided:
argvas a globally visible, self-length-carrying string-array value type (readable in functions/methods, noargcneeded) is not yet provided; it depends on the array-carries-own-length infrastructure (D37, same batch asstring/general arrays). For now it ships only as the C-formargc+char**top-level built-in names.