Linguagem de Programação C - NORMA ISO/IEC 9899TC2

Linguagem de Programação C - NORMA ISO/IEC 9899TC2

(Parte 6 de 11)

7More stringent correspondences between abstract and actual semantics may be defined by each implementation.

8EXAMPLE 1An 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 keywordvolatilewould then be redundant.

9Alternatively,animplementation 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 thesignalfunction would require explicit specification ofvolatilestorage, as well as other implementation-defined restrictions.

10EXAMPLE 2In executing the fragment


char c1, c2; c1 = c1 + c2; the ‘‘integer promotions’’require that the abstract machine promote the value of each variable tointsize and then add the twointsand truncate the sum.Provided the addition of twocharscan be done without overflow, orwith overflowwrapping silently to produce the correct result, the actual execution need only produce the same result, possibly omitting the promotions.

11EXAMPLE 3Similarly,inthe 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, ifd were replaced by the constant2.0,which has typedouble).

14 Environment §

WG14/N1124 CommitteeDraft — May 6, 2005ISO/IEC 9899:TC2

12EXAMPLE 4Implementations employing wide registers have totakecare to honor appropriate semantics. Values are independent of whether theyare represented in a register or in memory.For example, an implicitspillingof a register is not permitted to alter the value. Also,an explicitstoreand 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 tod1andd2are required to have been converted tofloat.

13EXAMPLE 5Rearrangement 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 roundofferror,eveninthe absence of overflowand underflow. Likewise, implementations cannot generally replace decimal constants in order to rearrange expressions. Inthe 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 tox*=y*z; z=(x-y)+y;//not equivalent toz=x; z=x+x*y;//not equivalent toz=x*(1.0 + y); y=x/5.0; //not equivalent toy=x*0.2;

14EXAMPLE 6To illustrate the grouping behavior of expressions, in the following fragment


int a, b; a=a+32760 + b + 5; the expression statement behavesexactly 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 tob,and that result is then added to5which results in the value assigned toa.Onamachine in which overflows produce an explicit trap and in which the range of values representable by anintis [−32768, +32767], the implementation cannot rewrite this expression as since if the values foraandbwere, respectively,−32754 and −15, the suma+bwould produce a trap while the original expression would not; nor can the expression be rewritten either as

since the values foraandbmight have been, respectively,4and −8 or −17 and 12.However, onamachine in which overflowsilently generates some value and where positive and negative overflows cancel, the above expression statement can be rewritten by the implementation in anyofthe above ways because the same result will occur.

§ Environment 15

ISO/IEC 9899:TC2Committee Draft — May 6, 2005WG14/N1124

15EXAMPLE 7The grouping of an expression does not completely determine its evaluation. Inthe 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()))); butthe actual increment ofpcan occur at anytime between the previous sequence point and the next sequence point (the;), and the call togetcharcan occur at anypoint prior to the need of its returned value.

Forward references:expressions (6.5), type qualifiers (6.7.3), statements (6.8), the signalfunction (7.14), files (7.19.3).

16 Environment §

WG14/N1124 CommitteeDraft — May 6, 2005ISO/IEC 9899:TC2

5.2 Environmental considerations

5.2.1 Charactersets

1Tw osets of characters and their associated collating sequences shall be defined: the set in which source files are written (thesource character set), and the set interpreted in the execution environment (theexecution character set). Each set is further divided into a basic character set,whose contents are givenbythis 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 theextended character set.The values of the members of the execution character set are implementation-defined.

2In a character constant or string literal, members of the execution character set shall be represented by corresponding members of the source character set or byescape sequencesconsisting of the backslash\followed by one or more characters.Abyte with all bits set to 0, called thenull character,shall exist in the basic execution character set; it is used to terminate a character string.

3Both the basic source and basic execution character sets shall have the following members: the 26uppercase lettersof the Latin alphabet the 26lowercase lettersof the Latin alphabet abcdefghijklm nopqrstuvwxyz the 10 decimaldigits 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 after0in the above list of decimal digits shall be one greater than the value of the previous. Insource 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.Inthe basic execution character set, there shall be control characters representing alert, backspace, carriage return, and newline. Ifany 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

ISO/IEC 9899:TC2Committee Draft — May 6, 2005WG14/N1124 converted to a token), the behavior is undefined.

4Aletteris an uppercase letter or a lowercase letter as defined above;inthis International Standard the term does not include other characters that are letters in other alphabets.

5The universal character name construct provides a way to name other characters.

Forward references:universal character names (6.4.3), character constants (, preprocessing directives(6.10), string literals (6.4.5), comments (6.4.9), string (7.1.1). Trigraph sequences

1All occurrences in a source file of the following sequences of three characters (called trigraph sequences12))are replaced with the corresponding single character.

No other trigraph sequences exist. Each?that does not begin one of the trigraphs listed above isnot changed.

2EXAMPLE Thefollowing source line printf("Eh???/n"); becomes (after replacement of the trigraph sequence??/) printf("Eh?\n"); Multibyte characters

1The source character set may contain multibyte characters, used to represent members of the extended character set.The execution character set may also contain multibyte characters, which need not have the same encoding as for the source character set.For both character sets, the following shall hold:

—The basic character set shall be present and each character shall be encoded as a single byte.

—The presence, meaning, and representation of anyadditional members is localespecific.

—A multibyte character set may have astate-dependent encoding,wherein each sequence of multibyte characters begins in aninitial shift stateand enters other locale-specificshift stateswhen specific multibyte characters are encountered in the sequence. Whilein the initial shift state, all single-byte characters retain their usual interpretation and do not alter the shift state.The interpretation for subsequent bytes

12)The trigraph sequences enable the input of characters that are not defined in the Invariant Code Set as described in ISO/IEC 646, which is a subset of the seven-bit US ASCII code set.

18 Environment §

WG14/N1124 CommitteeDraft — May 6, 2005ISO/IEC 9899:TC2 in the sequence is a function of the current shift state.

—Abyte with all bits zero shall be interpreted as a null character independent of shift state. Suchabyte shall not occur as part of anyother multibyte character.

2Forsource files, the following shall hold:

—Anidentifier,comment, string literal, character constant, or header name shall begin and end in the initial shift state.

—Anidentifier,comment, string literal, character constant, or header name shall consist of a sequence of valid multibyte characters.

5.2.2 Characterdisplay semantics

(Parte 6 de 11)