Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Program Entry and Execution

This chapter describes Co’s program entry and execution model. Entry dispatch, optional main, top-level return and 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 entry WinMain and the [[subsystem("windows")]] subsystem switch are implemented since V0.3.1; the top-level built-in command-line argc/argv are 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: main is 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 variant wWinMain is not yet supported). The return value is the process exit code.
  • Defining WinMain automatically 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).
  • WinMain is mutually exclusive with top-level bare statements: a file with both WinMain and top-level bare statements is a compile error (put the GUI logic inside WinMain); WinMain must 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:

SituationExit code
A top-level return V; is reachedV, and the whole program ends immediately (later top-level statements and main are not run)
Top level finishes with no return, and a main existsmain’s return value
Top level finishes with no return, and no main exists0
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:

  • argc is an int, the number of arguments (including the program name itself, so argc >= 1);
  • argv is a char** (a pointer to strings); argv[0] is usually the program’s own path, followed by the arguments in order;
  • iterate by the argc count; argv[argc] is always NULL (guaranteed by C). argv is a raw pointer, carries no length property, and has no bounds checking (C semantics) — keep within range using argc yourself;
  • both are scoped to the synthesized entry’s top-level code: referencing argv in a .c file, in a shared-library artifact (-o *.dll / *.so), or under the GUI WinMain entry 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 argv a globally visible, self-length-carrying string-array value type (readable inside functions and methods, with no argc) depends on Co’s array-carries-own-length infrastructure (same batch as string/general arrays) and is not yet provided; for now it ships as the C-form argc + 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 import lines in the main source has no effect on this order.
  • main of a non-main module is not a load hook: if a module defines main, 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 main also exists, main is entered automatically after all top-level statements finish, and main’s return value becomes the exit code.
  • return V; at the top level ends the whole program immediately with exit code V; a bare return; uses exit code 0.
  • 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 code 0.
  • 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 WinMain in the source takes the GUI entry (no console); or mark the entry function with [[subsystem("windows")]] to switch to the windows subsystem. WinMain is mutually exclusive with top-level bare statements and must take 4 parameters.
  • Command-line arguments (since V0.5.5): the .co top-level entry (synthesized console main) has built-in argc(int)/argv(char**), iterated by the argc count with argv[argc]==NULL; valid only as built-in names in the synthesized entry’s top-level code (referencing argv in .c/-o *.dll/*.so/WinMain reports undeclared); an explicit main (pure C style) still receives argc/argv as parameters.

Not yet provided:

  • argv as a globally visible, self-length-carrying string-array value type (readable in functions/methods, no argc needed) is not yet provided; it depends on the array-carries-own-length infrastructure (D37, same batch as string/general arrays). For now it ships only as the C-form argc + char** top-level built-in names.