ISO/IEC9899:201xCommittee Draft —August 11,2008WG14/N1336A conforming program is one that is acceptable to a conforming implementation.4)78An implementation shall be accompanied by a document that defines all implementation-defined and locale-specific characteristics and all extensions.Forward references:conditional inclusion (6.10.1),errordirective (6.10.5),characteristics of floating types <f1oat.h> (7.7), alternative spellings <iso646.h>(7.9), sizes of integer types<1imits.h>(7.10), variable arguments<stdarg.h>(7.15),booleantypeandvalues<stdboo1.h>(7.16),commondefinitions<stddef.h>(7.17), integertypes<stdint.h>(7.18)gprogramsareintendedto bemaximallyportableamongconforming4)Strictlyconformingimplementations.Conforming programs maydepend upon nonportable features of a conformingimplementation.8General$4
ISO/IEC 9899:201x Committee Draft — August 11, 2008 WG14/N1336 7 A conforming program is one that is acceptable to a conforming implementation.4) 8 An implementation shall be accompanied by a document that defines all implementationdefined and locale-specific characteristics and all extensions. Forward references: conditional inclusion (6.10.1), error directive (6.10.5), characteristics of floating types <float.h> (7.7), alternative spellings <iso646.h> (7.9), sizes of integer types <limits.h> (7.10), variable arguments <stdarg.h> (7.15), boolean type and values <stdbool.h> (7.16), common definitions <stddef.h> (7.17), integer types <stdint.h> (7.18). 4) Strictly conforming programs are intended to be maximally portable among conforming implementations. Conforming programs may depend upon nonportable features of a conforming implementation. 8 General §4
WG14/N1336Committee Draft — August 11, 2008ISO/IEC9899:201x5.EnvironmentAn implementation translates C source files andexecutes C programs in two dataprocessing-system environments, which will be called the translation emvironment andthe execution environment in this International Standard.Their characteristics define andconstrain the results of executing conforming C programs constructed according to thesyntactic and semantic rules for conforming implementations.Forward references:In this clause, only a few of many possible forward referenceshave been noted.5.1 Conceptual models5.1.1Translation environment5.1.1.1 Program structureACprogram need not allbe translated at the sametime.Thetext of theprogram iskept1inunitscalledsourcefiles,(orpreprocessingfiles)inthisInternationalStandard.Asource file together with all the headers and source files included via the preprocessingdirective#includeisknown as apreprocessingtranslationunit.Afterpreprocessing,apreprocessing translation unit is called a translation unit.Previously translated translationunits may be preserved individually or in libraries.The separate translation units of aprogram communicateby (forexample)callstofunctions whoseidentifiers haveexternallinkage, manipulation of objects whose identifiers have external linkage, or manipulationof data files. Translation units may be separately translated and then later linked toproduce an executable program.Forward references:linkages of identifiers (6.2.2),external definitions (6.9),preprocessing directives (6.10).5.1.1.2 Translation phasesThe precedence among the syntax rules of translation is specified by the followingphases.5)1. Physical source file multibyte characters are mapped, in an implementation-defined manner, to the source character set (introducing new-line characters forend-of-line indicators) if necessary.Trigraph sequences are replaced bycorresponding single-character internal representations.5)Implementations shall behave as if these separate phases occur, even though many are typically foldedtogether in practice. Source files, translation units, and translated translation units need notnecessarily be stored as files,nor need therebe any one-to-onecorrespondencebetween these entitiesand any external representation.Thedescription is conceptual only,and does not specify anyparticular implementation.9$5.1.1.2Environment
WG14/N1336 Committee Draft — August 11, 2008 ISO/IEC 9899:201x 5. Environment 1 An implementation translates C source files and executes C programs in two dataprocessing-system environments, which will be called the translation environment and the execution environment in this International Standard. Their characteristics define and constrain the results of executing conforming C programs constructed according to the syntactic and semantic rules for conforming implementations. Forward references: In this clause, only a few of many possible forward references have been noted. 5.1 Conceptual models 5.1.1 Translation environment 5.1.1.1 Program structure 1 ACprogram need not all be translated at the same time. The text of the program is kept in units called source files, (or preprocessing files) in this International Standard. A source file together with all the headers and source files included via the preprocessing directive #include is known as a preprocessing translation unit. After preprocessing, a preprocessing translation unit is called a translation unit. Previously translated translation units may be preserved individually or in libraries. The separate translation units of a program communicate by (for example) calls to functions whose identifiers have external linkage, manipulation of objects whose identifiers have external linkage, or manipulation of data files. Translation units may be separately translated and then later linked to produce an executable program. Forward references: linkages of identifiers (6.2.2), external definitions (6.9), preprocessing directives (6.10). 5.1.1.2 Translation phases 1 The precedence among the syntax rules of translation is specified by the following phases.5) 1. Physical source file multibyte characters are mapped, in an implementationdefined manner, to the source character set (introducing new-line characters for end-of-line indicators) if necessary. Trigraph sequences are replaced by corresponding single-character internal representations. 5) Implementations shall behave as if these separate phases occur, even though many are typically folded together in practice. Source files, translation units, and translated translation units need not necessarily be stored as files, nor need there be any one-to-one correspondence between these entities and any external representation. The description is conceptual only, and does not specify any particular implementation. §5.1.1.2 Environment 9
WG14/N1336ISO/IEC9899:201xCommittee Draft August 11, 2008Each instance of a backslash character()immediately followed by a new-line2.character is deleted, splicing physical source lines to form logical source lines.Only the last backslash on any physical source line shall be eligible for being partof such a splice. A source file that is not empty shall end in a new-line character.which shall not be immediately preceded by a backslash character before any suchsplicingtakesplace3.The source file is decomposed into preprocessing tokens) and sequences ofwhite-space characters (including comments). A source file shall not end in apartial preprocessingtoken or in apartial comment.Each comment isreplaced byone space character.New-line characters are retained.Whether each nonemptysequence of white-space characters other than new-line is retained or replaced byone space character is implementation-defined.Preprocessing directives are executed, macro invocations are expanded, and4Pragma unary operator expressions are executed.If a character sequence thatmatches the syntax of a universal character name is produced by tokenconcatenation (6.10.3.3),the behavior is undefined.A#includepreprocessingdirective causes the named header or source file to be processed from phase 1through phase 4, recursively.All preprocessing directives are then deleted.5.Each source character set member and escape sequence in character constants andstringliteralsisconvertedtothecorrespondingmemberoftheexecutioncharacterset, if there is no corresponding member, it is converted to an implementation-definedmemberotherthanthenull (wide)character.7)6.Adjacent string literal tokens are concatenated7.White-space characters separating tokens are no longer significant. Eachpreprocessing token is converted into a token.The resulting tokens aresyntactically and semantically analyzed and translated as a translation unit8.All external object and function references are resolved.Library components arelinked to satisfy external references to functions and objects not defined in thecurrent translation. All such translator output is collected into a program imagewhichcontainsinformationneededforexecutioninitsexecutionenvironmentForward references:universal character names (6.4.3),lexical elements (6.4),preprocessing directives (6.10), trigraph sequences (5.2.1.1), external definitions (6.9)As described in 6.4, the process of dividing a source file's characters into preprocessing tokens is6)context-dependent.For example, see thehandling of<withina#includepreprocessing directiveAn implementation need not convert all non-corresponding source characters to the same execution7character.10Environment$5.1.1.2
ISO/IEC 9899:201x Committee Draft — August 11, 2008 WG14/N1336 2. Each instance of a backslash character (\) immediately followed by a new-line character is deleted, splicing physical source lines to form logical source lines. Only the last backslash on any physical source line shall be eligible for being part of such a splice. A source file that is not empty shall end in a new-line character, which shall not be immediately preceded by a backslash character before any such splicing takes place. 3. The source file is decomposed into preprocessing tokens6) and sequences of white-space characters (including comments). A source file shall not end in a partial preprocessing token or in a partial comment. Each comment is replaced by one space character. New-line characters are retained. Whether each nonempty sequence of white-space characters other than new-line is retained or replaced by one space character is implementation-defined. 4. Preprocessing directives are executed, macro invocations are expanded, and _Pragma unary operator expressions are executed. If a character sequence that matches the syntax of a universal character name is produced by token concatenation (6.10.3.3), the behavior is undefined. A #include preprocessing directive causes the named header or source file to be processed from phase 1 through phase 4, recursively. All preprocessing directives are then deleted. 5. Each source character set member and escape sequence in character constants and string literals is converted to the corresponding member of the execution character set; if there is no corresponding member, it is converted to an implementationdefined member other than the null (wide) character. 7) 6. Adjacent string literal tokens are concatenated. 7. White-space characters separating tokens are no longer significant. Each preprocessing token is converted into a token. The resulting tokens are syntactically and semantically analyzed and translated as a translation unit. 8. All external object and function references are resolved. Library components are linked to satisfy external references to functions and objects not defined in the current translation. All such translator output is collected into a program image which contains information needed for execution in its execution environment. Forward references: universal character names (6.4.3), lexical elements (6.4), preprocessing directives (6.10), trigraph sequences (5.2.1.1), external definitions (6.9). 6) As described in 6.4, the process of dividing a source file’s characters into preprocessing tokens is context-dependent. For example, see the handling of < within a #include preprocessing directive. 7) An implementation need not convert all non-corresponding source characters to the same execution character. 10 Environment §5.1.1.2
WG14/N1336Committee Draft— August 11, 2008ISO/IEC9899:201x5.1.1.3 DiagnosticsA conforming implementation shall produce at least one diagnostic message (identified inan implementation-defined manner) if a preprocessing translation unit or translation unitcontains a violation of any syntax rule or constraint, even if the behavior is also explicitlyspecified as undefined or implementation-defined.Diagnostic messages need not beproduced inothercircumstances.8)EXAMPLEAn implementation shall issue a diagnostic for the translation unit:2char i;int i;because in those cases where wording in this Intermational Standard describes the behavior for a constructas being both a constraint error and resulting in undefined behavior,the constraint error shall bediagnosed5.1.2ExecutionenvironmentsTwo execution environments are defined:freestanding and hosted. In both cases,program startup occurs when a designated C function is called by the executionenvironment.All objects with static storage duration shall be initialized (set to theirinitial values)before program startup.The manner and timing of such initialization areotherwiseunspecified.Programtermination returns control totheexecutionenvironment.Forward references: storage durations of objects (6.2.4), initialization (6.7.8)5.1.2.1FreestandingenvironmentIn a freestanding environment (in which Cprogram execution may take place without any1benefit of an operating system), the name and type of the function called at programstartup are implementation-defined.Any library facilities available to a freestandingprogram, other than the minimal set required by clause 4,are implementation-defined.2The effect of program termination in a freestanding environment is implementationdefined.5.1.2.2 Hosted environmentA hosted environment need not be provided, but shall conform to the followingspecifications if present.The intent is that an implementation should identify the nature of, and where possible localize, each8)violation. Of course, an implementation is free to produce any number of diagnostics as long as avalid program is still correctly translated.Itmay also successfully translate an invalid program11$5.1.2.2Environment
WG14/N1336 Committee Draft — August 11, 2008 ISO/IEC 9899:201x 5.1.1.3 Diagnostics 1 A conforming implementation shall produce at least one diagnostic message (identified in an implementation-defined manner) if a preprocessing translation unit or translation unit contains a violation of any syntax rule or constraint, even if the behavior is also explicitly specified as undefined or implementation-defined. Diagnostic messages need not be produced in other circumstances.8) 2 EXAMPLE An implementation shall issue a diagnostic for the translation unit: char i; int i; because in those cases where wording in this International Standard describes the behavior for a construct as being both a constraint error and resulting in undefined behavior, the constraint error shall be diagnosed. 5.1.2 Execution environments 1 Tw o execution environments are defined: freestanding and hosted. In both cases, program startup occurs when a designated C function is called by the execution environment. All objects with static storage duration shall be initialized (set to their initial values) before program startup. The manner and timing of such initialization are otherwise unspecified. Program termination returns control to the execution environment. Forward references: storage durations of objects (6.2.4), initialization (6.7.8). 5.1.2.1 Freestanding environment 1 In a freestanding environment (in which C program execution may take place without any benefit of an operating system), the name and type of the function called at program startup are implementation-defined. Any library facilities available to a freestanding program, other than the minimal set required by clause 4, are implementation-defined. 2 The effect of program termination in a freestanding environment is implementationdefined. 5.1.2.2 Hosted environment 1 A hosted environment need not be provided, but shall conform to the following specifications if present. 8) The intent is that an implementation should identify the nature of, and where possible localize, each violation. Of course, an implementation is free to produce any number of diagnostics as long as a valid program is still correctly translated. It may also successfully translate an invalid program. §5.1.2.2 Environment 11
ISO/IEC9899:201xCommittee Draft — August 11, 2008WG14/N13365.1.2.2.1 Program startupThe function called at program startup is named main. The implementation declares noprototype for this function. It shall be defined with a return type of int and with noparameters:int main(void)(/*.*/)or with two parameters (referred to here as argc and argv, though any names may beused, as they are local to the function in which they are declared)int main(int argc,char *argv[])/*... */)or equivalent:9) or in some other implementation-defined manner.2If they aredeclared,the parameters tothemainfunction shall obey the followingconstraints:Thevalueof argcshall benonnegative.argv[argc] shall beanull pointer.If the value of argc is greater than zero, the array members argv[o] throughargv[argc-1] inclusive shall contain pointers to strings, which are givenimplementation-defined valuesbythehostenvironment priortoprogram startup.Theintent is to supply to the program information determined prior to program startupfrom elsewhere in the hosted environment. If the host environment is not capable ofsupplying strings with letters in both uppercase and lowercase, the implementationshall ensure that the strings are received in lowercase.If the value of argc is greater than zero, the string pointed to by argv[o]represents the program name, argv[o][o] shall be the null character if theprogram name is not available from the host environment. If the value of argc isgreater 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 shallbe modifiable by theprogram, and retain their last-stored values between programstartup and program termination5.1.2.2.2 Program executionIn a hosted environment, a program may use all the functions, macros, type definitions,and objects described in the library clause (clause7).Thus, int can be replaced by a typedef name defined as int, or the type of argv can be written as91char ** argy,andsoon12Environment$5.1.2.2.2
ISO/IEC 9899:201x Committee Draft — August 11, 2008 WG14/N1336 5.1.2.2.1 Program startup 1 The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters: int main(void) { /* . */ } or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared): int main(int argc, char *argv[]) { /* . */ } or equivalent;9) or in some other implementation-defined manner. 2 If they are declared, the parameters to the main function 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.2 Program execution 1 In a hosted environment, a program may use all the functions, macros, type definitions, and objects described in the library clause (clause 7). 9) Thus, int can be replaced by a typedef name defined as int, or the type of argv can be written as char ** argv, and so on. 12 Environment §5.1.2.2.2