Skip to content

Instantly share code, notes, and snippets.

@zackw
Last active May 27, 2018 18:45
Show Gist options
  • Save zackw/b35a23bc5d0a4443f04c734a3d7e9fe1 to your computer and use it in GitHub Desktop.
Save zackw/b35a23bc5d0a4443f04c734a3d7e9fe1 to your computer and use it in GitHub Desktop.
Draft revision to ISO C section 5.1.2

5.1.2 Execution environments

p1 Two execution environments are defined: freestanding and hosted. The implementation shall provide a freestanding environment, and may also provide a hosted environment.

p2 In both environments, all of the facilities of the language (clause 6) are available. In a hosted environment, all of the facilities of the library (clause 7) are also available. In a freestanding environment, the set of library facilities available, beyond the minimal set required by clause 4, is implementation-defined.

5.1.2.1 Program startup

p1 Program startup occurs when a designated function, the entry point, is called by the execution environment. Before the entry point is called, all objects with static storage duration shall be initialized (set to their initial values). Clause 7 specifies several other actions that occur "at program startup". These shall also occur before the entry point is called. (In a freestanding environment, only the actions relevant to the supported subset of clause 7 occur.) The manner and timing of all this initialization is otherwise unspecified.

p3 The name and type of the entry point are implementation-defined. The implementation may support more than one possible name and type for the entry point. The implementation shall not declare any of the supported entry points in any header file it provides.

p4 Each program shall define its entry point as a function with external linkage, without the inline function-specifier, and with a name and type compatible with one of the names and types that the implementation supports.[fn10] These requirements are constraints.

[fn10] For instance, int in the prototype of the entry point may be replaced with a typedef name defined as int, char ** and char *[] are interchangeable as usual, the names of parameters are not significant, and so on.

p5 If the implementation supports more than one possible name for the entry point, and a program defines functions or objects with external linkage matching more than one of those names, then only one of these definitions shall be treated as the entry point; which one is implementation-defined. Only the entry point must satisfy the constraints in paragraph 3; all of the other definitions are treated as ordinary functions or objects.

p6 A program may make recursive calls to its entry point. This shall not cause objects with static storage duration to be reinitialized, or any of the other "at program startup" actions to be repeated.

5.1.2.1.1 Hosted program startup

p1 If a hosted environment is provided, then the implementation shall support at least the following two names and types for the entry point for programs that use the hosted environment:

int main(void) { ... }
int main(int argc, char *argv[]) { ... }

(The parameters argc and argv may be given any name, as these names are local to the function.)

p2 The data passed to the program by the execution environment via the parameters argc and argv shall obey the following constraints:

  • The value of argc shall be nonnegative.
  • argv[argc] shall be a null pointer.
  • If the value of argc is greater than zero, the array members argv[0] through argv[argc-1] inclusive shall contain pointers to strings, which are given implementation-defined values by the host environment prior to program startup. The intent is to supply to the program information determined prior to program startup from elsewhere in the hosted environment. If the host environment is not capable of supplying strings with letters in both uppercase and lowercase, the implementation shall ensure that the strings are received in lowercase.
  • If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] shall be the null character if the program name is not available from the host environment. If the value of argc is greater than one, the strings pointed to by argv[1] through argv[argc-1] represent the program parameters.
  • The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination.

5.1.2.2 Program termination

p1 The effect of returning from the initial call to the entry point is implementation-defined.

5.1.2.2.1 Hosted program termination

p1 In a hosted environment, when the entry point is defined with the name main, the effect of returning from the initial call to the entry point is to cause a call to the exit function.[fn11] The value passed to exit is the value returned by the entry point.

p2 If an entry point named main has a return type incompatible with int, the value passed to exit is unspecified.

p3 If control reaches the } that terminates an entry point named main, and its return type is compatible with int, the behavior is as-if the function had returned the value 0. This applies to both initial and recursive calls.

Forward references: definition of terms (7.1.1), the exit function (7.22.4.4).

[fn11] In accordance with 6.2.4, when this happens, the lifetimes of objects with automatic storage duration declared in main end before exit is called, even when they would not have if main had called exit itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment