WG14/N1336Committee Draft — August 11, 2008ISO/IEC9899:201x5.1.2.2.3 Program terminationIf the return type of the main function is a type compatible with int, a return from theinitial calltothemainfunctionisequivalenttocallingtheexitfunctionwiththevaluereturned by the main function as its argument;10) reaching the ) that terminates themain function returns a value of o. If the return type is not compatible with int, thetermination status returned to the host environment is unspecified.Forward references: definition of terms (7.1.1), the exit function (7.20.4.3)5.1.2.3Programexecution1The semantic descriptions in this International Standard describe the behavior of anabstract machine in which issues of optimization are irrelevant.2Accessing a volatile object, modifying an object, modifying a file, or calling a functionthat does any of those operations are all side effects,'l) which are changes in the state ofthe execution environment. Evaluation of an expression in general includes both valuecomputations and initiation of side effects. Value computation for an lvalue expressionincludes determining the identity of the designated object.3Sequenced before is an asymmetric, transitive,pair-wise relation between evaluationsexecuted by a single thread, which induces a partial order among those evaluations.GivenanytwoevaluationsAandB,ifAissequencedbeforeB,thentheexecutionofAshall precede the execution of B. (Conversely, if A is sequenced before B, then B issequenced afier A.)If A is not sequenced before or after B, then A and B areunsequenced.Evaluations A and B are indeterminately sequenced when A is sequencedeither before or after B, but it is unspecified which 12) The presence of a sequence pointbetween the evaluation of expressions A and Bimplies that every value computation andside effect associated with A is sequenced before every value computation and side effectassociated withB.(A summary of the sequencepoints is given in annex C.)AIntheabstractmachine.allexpressionsareevaluatedasspecifiedbythesemantics.Aractualimplementationneed notevaluatepartofanexpressionif itcandeducethat itsvalue is not used and that no needed side effects are produced (including any caused by10)In accordance with 6.2.4, the lifetimes of objects with automatic storage duration declared in mainwill have ended in the former case, even where theywould not have in thelatter.11) The IEC 60559 standard for binary floating-point arithmetic requires certain user-accessible statusflags and control modes.Floating-point operations implicitly setthe status flags;modes affectresultvalues of floating-point operations.Implementations that support such floating-point state arerequired to regard changes to it as side effectsseeannex F fordetails.The floating-pointenvironment library <fenv.h>provides a programming facilityfor indicatingwhenthese sideeffectsmatter, freeingthe implementations inothercases.12)Theexecutionsof unsequencedevaluations caninterleave.Indeterminatelysequenced evaluationscannot interleave, but can be executed inanyorder.13$5.1.2.3Environment
WG14/N1336 Committee Draft — August 11, 2008 ISO/IEC 9899:201x 5.1.2.2.3 Program termination 1 If the return type of the main function is a type compatible with int,areturn from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument;10) reaching the } that terminates the main function returns a value of 0. If the return type is not compatible with int, the termination status returned to the host environment is unspecified. Forward references: definition of terms (7.1.1), the exit function (7.20.4.3). 5.1.2.3 Program execution 1 The semantic descriptions in this International Standard describe the behavior of an abstract machine in which issues of optimization are irrelevant. 2 Accessing a volatile object, modifying an object, modifying a file, or calling a function that does any of those operations are all side effects, 11) which are changes in the state of the execution environment. Evaluation of an expression in general includes both value computations and initiation of side effects. Value computation for an lvalue expression includes determining the identity of the designated object. 3 Sequenced before is an asymmetric, transitive, pair-wise relation between evaluations executed by a single thread, which induces a partial order among those evaluations. Given any two evaluations A and B, if A is sequenced before B, then the execution of A shall precede the execution of B. (Conversely, if A is sequenced before B, then B is sequenced after A.) If A is not sequenced before or after B, then A and B are unsequenced. Evaluations A and B are indeterminately sequenced when A is sequenced either before or after B, but it is unspecified which.12) The presence of a sequence point between the evaluation of expressions A and B implies that every value computation and side effect associated with A is sequenced before every value computation and side effect associated with B. (A summary of the sequence points is given in annex C.) 4 In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects are produced (including any caused by 10) In accordance with 6.2.4, the lifetimes of objects with automatic storage duration declared in main will have ended in the former case, even where they would not have in the latter. 11) The IEC 60559 standard for binary floating-point arithmetic requires certain user-accessible status flags and control modes. Floating-point operations implicitly set the status flags; modes affect result values of floating-point operations. Implementations that support such floating-point state are required to regard changes to it as side effects — see annexFfor details. The floating-point environment library <fenv.h> provides a programming facility for indicating when these side effects matter, freeing the implementations in other cases. 12) The executions of unsequenced evaluations can interleave. Indeterminately sequenced evaluations cannot interleave, but can be executed in any order. §5.1.2.3 Environment 13
ISO/IEC9899:201xCommittee Draft — August 11, 2008WG14/N1336calling a function or accessing a volatile object)5When the processing of the abstract machine is interrupted by receipt of a signal, thevalues ofobjects withtype otherthan volatile sig_atomic_t are unspecified, andthe value of any object with type other than volatile sig_atomic_t that ismodified by the handler becomes undefined.6The least requirements on a conforming implementation are:Accesses to volatile objects are evaluated strictlyaccording to therules of the abstractmachine.At program termination, all data written into files shall be identical to the result thatexecution of the program according to the abstract semantics would have produced.The input and output dynamics of interactive devices shall take place as specified in7.19.3. The intent of these requirements is that unbuffered or line-buffered outputappear as soon as possible,to ensure that prompting messages actually appear prior toaprogramwaitingforinput.7What constitutes an interactivedevice is implementation-defined.8More stringentcorrespondences between abstract andactual semanticsmaybedefinedbyeachimplementation.EXAMPLE1An implementation might define a one-to-one correspondence between abstract and actual9semantics:at every sequence point,the values oftheactual objects would agree withthose specified bytheabstract semantics.Thekeyword volatile would then beredundant.10Alternatively,an implementation might perform various optimizations within each translation unit,suchthat theactual semantics would agree withtheabstract semantics only when makingfunction calls acrosstranslation unitboundaries.In suchanimplementation,atthetimeof eachfunction entryand functionreturn where the calling function and the called function are in different translation units,the values of allexternally linked objects and of all objects accessible via pointers therein would agree with the abstractsemantics.Furthermore,at the time ofeach such function entry thevalues of the parameters of the calledfunction and of all objects accessible via pointers therein would agree with the abstract semantics.In thistypeof implementation, objects referredtoby interrupt serviceroutines activated bythe signal functionwould require explicit specification of volatile storage, as well as other implementation-definedrestrictions.EXAMPLE2Inexecuting thefragment11char cl, c2;/*..*/cl = cl+ c2;the“"integer promotions"require that the abstract machine promote the value of each variable to int sizeand thenadd thetwo intsand truncatethe sum.Provided the addition of two chars can be donewithoutoverflow, or with overflow wrapping silentlytoproducethecorrect result,the actual execution need onlyproduce the sameresult, possibly omitting the promotions.14$5.1.2.3Environment
ISO/IEC 9899:201x Committee Draft — August 11, 2008 WG14/N1336 calling a function or accessing a volatile object). 5 When the processing of the abstract machine is interrupted by receipt of a signal, the values of objects with type other than volatile sig_atomic_t are unspecified, and the value of any object with type other than volatile sig_atomic_t that is modified by the handler becomes undefined. 6 The least requirements on a conforming implementation are: — Accesses to volatile objects are evaluated strictly according to the rules of the abstract machine. — At program termination, all data written into files shall be identical to the result that execution of the program according to the abstract semantics would have produced. — The input and output dynamics of interactive devices shall take place as specified in 7.19.3. The intent of these requirements is that unbuffered or line-buffered output appear as soon as possible, to ensure that prompting messages actually appear prior to a program waiting for input. 7 What constitutes an interactive device is implementation-defined. 8 More stringent correspondences between abstract and actual semantics may be defined by each implementation. 9 EXAMPLE 1 An implementation might define a one-to-one correspondence between abstract and actual semantics: at every sequence point, the values of the actual objects would agree with those specified by the abstract semantics. The keyword volatile would then be redundant. 10 Alternatively, an implementation might perform various optimizations within each translation unit, such that the actual semantics would agree with the abstract semantics only when making function calls across translation unit boundaries. In such an implementation, at the time of each function entry and function return where the calling function and the called function are in different translation units, the values of all externally linked objects and of all objects accessible via pointers therein would agree with the abstract semantics. Furthermore, at the time of each such function entry the values of the parameters of the called function and of all objects accessible via pointers therein would agree with the abstract semantics. In this type of implementation, objects referred to by interrupt service routines activated by the signal function would require explicit specification of volatile storage, as well as other implementation-defined restrictions. 11 EXAMPLE 2 In executing the fragment char c1, c2; /* . */ c1 = c1 + c2; the ‘‘integer promotions’’ require that the abstract machine promote the value of each variable to int size and then add the two ints and truncate the sum. Provided the addition of two chars can be done without overflow, or with overflow wrapping silently to produce the correct result, the actual execution need only produce the same result, possibly omitting the promotions. 14 Environment §5.1.2.3
WG14/N1336Committee Draft—August 11, 2008ISO/IEC9899:201xEXAMPLE3Similarly,inthefragment12float fl,f2;double d;/*..*/f1 = f2 * d;themultiplicationmaybe executed using single-precision arithmetic if the implementation canascertainthat the result would be the same as if it wereexecuted using double-precision arithmetic (forexample, ifdwere replaced by the constant 2.0, which has type double).EXAMPLE4Implementationss employing wide registers have to take careto honor appropriate13semantics.Values are independent of whether they are represented in a register or in memory. Forexample,animplicitspillingofaregisterisnotpermittedtoalterthevalue.Also,anexplicitstoreandloadisrequiredtoroundtotheprecision ofthestoragetype.Inparticular,casts and assignments arerequired toperformtheirspecifiedconversion.Forthefragmentdouble dl, d2;float f;di = f = expression;d2=(float)expression;the values assigned to d1 and d2 are required to have been converted to floatEXAMPLE5Rearrangementforfloating-point expressions is often restricted because of limitations in14precision as well as range.The implementation cannot generally apply the mathematical associative rulesfor addition or multiplication, northedistributive rule,because of roundoff error,even in the absence ofoverflow and underflow.Likewise, implementations cannot generally replace decimal constants in order torearrange expressions.In thefollowing fragment,rearrangements suggested by mathematical rules for realnumbersareoftennotvalid(seeF.8)doublex,Y,z;*//*.x= (x*y)*z;/ /notequivalenttox*=y*z;z=(x-y)+y;/ / notequivalenttoz=x;=X+X*Y;/ /notequivalenttoz=x*(1.0+y);ZY=x/5.0;// notequivalenttoy=x*0.2;EXAMPLE6Toillustratethegroupingbehaviorof expressions, inthefollowingfragment15int a, b;/*..*/a=a+32760+b+5;the expression statement behaves exactly the same asa = (((a + 32760) + b) + 5) ;duetotheassociativityand precedenceoftheseoperators.Thus,theresultofthe sum(a+ 32760)isnext added tob, and that result is then added to 5 whichresults in the value assigned to a.On a machine inwhich overflows produce an explicit trap and in which the range of values representable by an int is[-32768,+32767],theimplementationcannotrewritethisexpressionasa=((a+b)+32765);since if the values for a and b were, respectively,-32754 and -15, the sum a + b would produce a trapwhile the original expression would not; nor can the expression be rewritten either as15$5.1.2.3Environment
WG14/N1336 Committee Draft — August 11, 2008 ISO/IEC 9899:201x 12 EXAMPLE 3 Similarly, in the fragment float f1, f2; double d; /* . */ f1 = f2 * d; the multiplication may be executed using single-precision arithmetic if the implementation can ascertain that the result would be the same as if it were executed using double-precision arithmetic (for example, if d were replaced by the constant 2.0, which has type double). 13 EXAMPLE 4 Implementations employing wide registers have to take care to honor appropriate semantics. Values are independent of whether they are represented in a register or in memory. For example, an implicit spilling of a register is not permitted to alter the value. Also, an explicit store and load is required to round to the precision of the storage type. In particular, casts and assignments are required to perform their specified conversion. For the fragment double d1, d2; float f; d1 = f = expression; d2 = (float) expression; the values assigned to d1 and d2 are required to have been converted to float. 14 EXAMPLE 5 Rearrangement for floating-point expressions is often restricted because of limitations in precision as well as range. The implementation cannot generally apply the mathematical associative rules for addition or multiplication, nor the distributive rule, because of roundoff error, even in the absence of overflow and underflow. Likewise, implementations cannot generally replace decimal constants in order to rearrange expressions. In the following fragment, rearrangements suggested by mathematical rules for real numbers are often not valid (see F.8). double x, y, z; /* . */ x=(x*y)*z; // not equivalent to x *=y*z; z=(x-y)+y;// not equivalent to z=x; z=x+x*y; // not equivalent to z=x*(1.0 + y); y=x/5.0; // not equivalent to y=x*0.2; 15 EXAMPLE 6 To illustrate the grouping behavior of expressions, in the following fragment int a, b; /* . */ a=a+32760 + b + 5; the expression statement behaves exactly the same as a=(((a + 32760) + b) + 5); due to the associativity and precedence of these operators. Thus, the result of the sum (a + 32760) is next added to b, and that result is then added to 5 which results in the value assigned to a. Onamachine in which overflows produce an explicit trap and in which the range of values representable by an int is [−32768, +32767], the implementation cannot rewrite this expression as a=((a + b) + 32765); since if the values for a and b were, respectively, −32754 and −15, the sum a+b would produce a trap while the original expression would not; nor can the expression be rewritten either as §5.1.2.3 Environment 15
WG14/N1336ISO/IEC9899:201xCommittee Draft — August 11, 2008a=((a+32765)+b);ora = (a + (b + 32765));since the values for a and b might havebeen, respectively,4and-8 or-17and 12.However,onamachinein which overflow silently generates some value and where positive and negative overflows cancel, theabove expression statement can be rewritten by the implementation in any of the above ways because thesame result will occur.EXAMPLE7The grouping of an expression does not completely determine its evaluation.In the16following fragment#include<stdio.h>int sum;char*p;/*. */sum=sum*10-o+(*p++=getchar());theexpressionstatementisgroupedasif itwerewrittenassum=(((sum *10)-0")+((*(p++))=(getchar())));but the actual increment of p can occur at any time between the previous sequence point and the nextsequence point (the :),and the call to getchar can occur at any point prior to the need of its returnedvalue.Forward references: expressions (6.5), type qualifiers (6.7.3), statements (6.8), thesigna1 function (7.14), files (7.19.3)16$5.1.2.3Environment
ISO/IEC 9899:201x Committee Draft — August 11, 2008 WG14/N1336 a=((a + 32765) + b); or a=(a+(b+32765)); since the values for a and b might have been, respectively,4and −8 or −17 and 12. However, onamachine in which overflow silently generates some value and where positive and negative overflows cancel, the above expression statement can be rewritten by the implementation in any of the above ways because the same result will occur. 16 EXAMPLE 7 The grouping of an expression does not completely determine its evaluation. In the following fragment #include <stdio.h> int sum; char *p; /* . */ sum = sum * 10 - '0' + (*p++ = getchar()); the expression statement is grouped as if it were written as sum = (((sum * 10) - '0') + ((*(p++)) = (getchar()))); but the actual increment of p can occur at any time between the previous sequence point and the next sequence point (the ;), and the call to getchar can occur at any point prior to the need of its returned value. Forward references: expressions (6.5), type qualifiers (6.7.3), statements (6.8), the signal function (7.14), files (7.19.3). 16 Environment §5.1.2.3
WG14/N1336Committee Draft August 11, 2008ISO/IEC9899:201x5.2Environmentalconsiderations5.2.1 Character setsTwo sets of characters and their associated collating sequences shall be defined: the set in1which source files are written (the source character set),and the set interpreted in theexecution environment(the execution character set).Each set is further divided into abasiccharacterset,whose contents aregiven bythis subclause,andaset of zero ormorelocale-specificmembers (which arenot members of the basiccharacter set)calledextended characters. The combined set is also called the extended character set. Thevalues of the members of the execution character set are implementation-defined.2Inacharacter constant orstringliteral,membersof the execution charactersetshall berepresented by corresponding members of the source character set or by escapesequences consisting of the backslashIfollowed by one or more characters.Abyte withall bits set to O, called the null character, shall exist in the basic execution character set; itis usedtoterminatea characterstring3Boththebasicsourceandbasic executioncharactersetsshallhavethefollowingmembers: the 26 uppercase letters of the Latin alphabetMFGHKLABCDEIJN0PQRsTUVWxAZthe26lowercase letters of theLatinalphabetfbdhiyk1cegAanrstuvwx0pqyzthe 10 decimal digits590234618thefollowing29graphiccharacters#%&!-)(><1;?the space character, and control characters representing horizontal tab, vertical tab, andform feed.The representation of each member of the source and execution basiccharacter sets shall fit in a byte.In boththe source and execution basic character sets,thevalue of each character after o in the above list of decimal digits shall be one greater thanthe value of the previous. In source files, there shall be some way of indicating the end ofeachlineof text,this International Standard treats such an end-of-line indicator as if itwere a single new-line character.In the basic execution character set, there shall becontrol characters representing alert, backspace, carriage return, and new line. If anyother characters are encountered in a source file (except in an identifier, a characterconstant, a string literal,a header name, a comment, or a preprocessing token that is never17$5.2.1Environment
WG14/N1336 Committee Draft — August 11, 2008 ISO/IEC 9899:201x 5.2 Environmental considerations 5.2.1 Character sets 1 Tw o sets of characters and their associated collating sequences shall be defined: the set in which source files are written (the source character set), and the set interpreted in the execution environment (the execution character set). Each set is further divided into a basic character set, whose contents are given by this subclause, and a set of zero or more locale-specific members (which are not members of the basic character set) called extended characters. The combined set is also called the extended character set. The values of the members of the execution character set are implementation-defined. 2 In a character constant or string literal, members of the execution character set shall be represented by corresponding members of the source character set or by escape sequences consisting of the backslash \ followed by one or more characters. A byte with all bits set to 0, called the null character, shall exist in the basic execution character set; it is used to terminate a character string. 3 Both the basic source and basic execution character sets shall have the following members: the 26 uppercase letters of the Latin alphabet ABCDEFGHIJKLM NOPQRSTUVWXYZ the 26 lowercase letters of the Latin alphabet abcdefghijklm nopqrstuvwxyz the 10 decimal digits 0123456789 the following 29 graphic characters !"#%&'()*+,-./: ;<=>?[\]^_{|}~ the space character, and control characters representing horizontal tab, vertical tab, and form feed. The representation of each member of the source and execution basic character sets shall fit in a byte. In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous. In source files, there shall be some way of indicating the end of each line of text; this International Standard treats such an end-of-line indicator as if it were a single new-line character. In the basic execution character set, there shall be control characters representing alert, backspace, carriage return, and new line. If any other characters are encountered in a source file (except in an identifier,acharacter constant, a string literal, a header name, a comment, or a preprocessing token that is never §5.2.1 Environment 17