C Reference Manual.pdf

所需积分/C币:12 2019-08-04 13:35:20 83KB PDF
9
收藏 收藏
举报

C Reference Manual,C Reference Manual,C Reference Manual,C Reference Manual
C Reference Manual-3 3. Syntax notation In the syntax notation used in this manual, syntactic categories are indicated by italic type, and literal words and characters in gothic. Alternatives are listed on separate lines. An optional terminal or non-terminal symbol is in- dicated by the subscript" opt,”’ so that s expression would indicate an optional expression in braces 4. What's in a name? C bases the interpretation of an identifier upon two attributes of the identifier: its storage class and its type. The storage class determines the location and lifctime of the storage associated with an identifier; the type determines the meaning of the values found in the identifiers storage There are four declarable storage classes: automatic, static, external, and register. Automatic variables are local to each invocation of a function and are discarded on return static variables are local to a function but retain their val- ues independently of invocations of the function; external variables are independent of any function. Register vari ables are stored in the fast registers of the machine like automatic variables they are local to each function and dis appear on return C supports four fundamental types of objects: characters, integers, single-, and double-precision floating- poi numbers Characters(declared, and hereinafter called, char) are chosen from the ASCII set; they occupy the right most seven bits of an 8-bit byte. It is also possible to interpret chars as signed, 2s complement 8-bit numbers Integers(int)are represented in 16-bit 2s complement notation Single precision floating point(float) quantities have magnitude in the range approximately 10 or 0; their precision is 24 bits or about seven decimal digits Double-precision floating-point(double) quantities have the same range as floats and a precision of 56 bits or about 17 decimal digits Besides the four fundamental types there is a conceptually infinite class of derived types constructed from the fun damental types in the following ways arrays of objects of most types functions which return objects of a given type pointers to objects of a given type structures containing objects of various types In general these methods of constructing objects can be applied recursively 5. Objects and lvalues An object is a manipulatable region of storage; an lvalue is an expression referring to an object. An obvious ex ample of an lvalue expression is an identifier. There are operators which yicld lvalues: for example, if e is an c pression of pointer type, then *e is an lvalue expression referring to the object to which e points. The name Ivalue' comes from the assignment expression"El- E2"in which the left operand El must be an lvalue expres sion. The discussion of each operator below indicates whether it expects lvalue operands and whether it yields an Ivalu 6. Conversion A number of operators may, depending on their operands, cause conversion of the value of an operand from one type to another. This section explains the result to be expected from such conversions C Reference Manual-4 6. 1 Characters and integers A char object may be used anywhere an int may be. In all cases the char is converted to an int by propa gating its sign through the upper 8 bits of the resultant integer. This is consistent with the two's complement repre- sentation used for both characters and integers.(However, the sign-propagation feature disappears in other imple mentations 6.2 Float and double All floating arithmetic in C is carried out in double-precision; whenever a float appears in an expression it is engthened to double by zero-padding its fraction. When a double must be converted to float, for example by an assignment, the double is rounded before truncation to float length 6.3 Float and double; integer and character All ints and chars may be converted without loss of significance to float or double. Conversion of float or double to int or char takes place with truncation towards 0. Erroneous results can be expected if the magnitude of the result exceeds 32, 767(for int)or 127(for char) 6.4 Pointers and integers Integers and pointers may be added and compared; in such a case the int is converted as specified in the discus sion of the addition operator Two pointers to objects of the same type may be subtracted; in this case the result is converted to an integer as specified in the discussion of the subtraction operator 7. Expressions The precedence of expression operators is the same as the order of the major subsections of this section(highest precedence first). Thus the expressions referred to as the operands of+($7. 4) are those expressions defined in SS7.1 7.3. Within each subsection, the operators have the same precedence. Left- or right-associativity is specified in each subsection for the operators discussed therein. The precedence and associativity of all the expression opera tors is summarized in an appendix Otherwise the order of evaluation of expressions is undefined. In particular the compiler considers itself free to compute subexpressions in the order it believes most efficient, even if the subexpressions involve side effect 7.1 Primary expressions Primary expressions involving.,->, subscripting, and function calls group left to right 7.1.1 identifier An identifier is a primary expression, provided it has been suitably declared as discussed below. Its type is speci fied by its declaration. However, if the type of the identifier is" array of.. then the value of the identifier- expression is a pointer to the first object in the array, and the type of the expression is"pointer to moreover an array identifier is not an lvalue expression Likewise, an identifier which is declared"function returning. '' when used except in the function-name posi tion of a call, is converted to " pointer to function returning 7.1.2 constant A decimal, octal, character, or floating constant is a primary expression. Its type is int in the first three cases double in the last 7. 1.3 string A string is a primary expression. Its type is originally "array of char, but following the same rule as in $7.1.1 for identifiers, this is modified to"pointer to char' and the result is a pointer to the first character in the strin 7.1.4( expression A parenthesized expression is a primary expression whose type and value are identical to those of the unadorned expression. The presence of parentheses does not affect whether the expression is an lvalue C Reference Manual-5 7.1.5 primary-expression expression a primary expression followed by an expression in square brackets is a primary expression. The intuitive mean- ing is that of a subscript. Usually, the primary expression has type"pointer to.. , the subscript expression is int and the type of the result is"... The expression ']is identical (by definition)to"*((el)+(E2)) All the clues needed to understand this notation are contained in this section together with the discussions in $ 7.1.1,7.2.1, and 7. 4. 1 on identifiers, * and +respectively $14.3 below summarizes the implications 7. 1.6 primary-expression( expression-list A function call is a primary expression followed by parentheses containing a possibly empty, comma-separated list of expressions which constitute the actual arguments to the function The primary expression must be of function returning .. ', and the result of the function call is of type" .. , As indicated below, a hitherto unseen identifier followed immediately by a left parenthesis is contextually declared to represent a function returning an in- teger; thus in the most common case, integer-valued functions need not be declared Any actual arguments of type -loat are converted to double before the call; any of type char are converted nt In preparing for the call to a function, a copy is made of each actual parameter; thus, all argument-passing in C is strictly by value. A function may change the values of its formal parameters, but these changes cannot possibly af- fect the values of the actual parameters. On the other hand, it is perfectly possible to pass a pointer on the under- standing that the function may change the value of the object to which the pointer points Recursive calls to any function are permissible 7.1.7 primary-lvalue. member-of-structure An lvalue expression followed by a dot followed by the name of a member of a structure is a primary expression The object referred to by the lvalue is assumed to have the same form as the structure containing the structure mem- ber. The result of the expression is an lvalue appropriately offset from the origin of the given lvalue whose type is that of the named structure member. The given lvalue is not required to have any particular type Structures are discussed in $8.5 7. 1. 8 primary-expression-> member-of-structure 4t- The primary-expression is assumed to be a pointer which points to an object of the same form as the structure of ich the member-of-structure is a part. The result is an lvalue appropriately offset from the origin of the pointed-to structure whose type is that of the named structure member. The type of the primary-expression need not in fact be pointer; it is sufficient that it be a pointer, character, or integer Except for the relaxation of the requirement that el be of pointer type, the expressionEl-MOS is exactly equivalent to“(*E1).MOS” 7.2 Unary operators Expressions with unary operators group right-to-left 7.2.1 expression The unary operator means indirection: the expression must be a pointer, and the result is an Ivalue referring to the object to which the expression points. If the type of the expression is"pointer to.. '' the type of the result is 7.2.2& lvalue-expression The result of the unary operator is a pointer to the object referred to by the lvalue-expression. If the type of the Ivalue-expression is∵..”’, the type of the result is""pointer to..” 7.2.3 -expression The result is the negative of the expression, and has the same type. The type of the expression must be char int. float or double C Reference Manual-6 7. 2. 4! expression The result of the logical negation operator is I if the value of the expression is 0, 0 if the value of the expre sion is non-zero. The type of the result is int. This operator is applicable only to ints or chars 7.2.5 expression Thes operator yields the ones complement of its operand. The type of the expression must be int or char,and the result is int 7.2.6 ++ lvalue-expression The object referred to by the lvalue expression is incremented The value is the new value of the lvalue expres- sion and the type is the type of the lvalue. If the expression is int or char, it is incremented by l; if it is a pointer to an object, it is incremented by the length of the object. + is applicable only to these types. (Not, for example, to float or double 7.2.7--lvalue-expression The object referred to by the lvalue expression is decremented analogously to the +t operator. 7. 2.8 value-expression + The result is the value of the object referred to by the lvalue expression. After the result is noted, the object re ferred to by the lvalue is incremented in the same manner as for the prefix + operator: by 1 for an int or cnal, 6L the length of the pointed-to object for a pointer. The type of the result is the same as the type of the Ivalue- expression 7.2.9 lvalue-expression The result of the expression is the value of the object referred to by the the lvalue expression. After the result is noted, the object referred to by the lvalue expression is decremented in a way analogous to the postfix + operator 7.2.10 sizeof expression The sizeof operator yields the size, in bytes, of its operand. When applied to an array, the result is the total number of bytes in the array. The size is determined from the declarations of the objects in the expression. This ex pression is semantically an integer constant and may be used anywhere a constant is required. Its major use is in communication with routines like storage allocators and 1/0 systems 3 Multiplicative operators The multiplicative operators *,/ and group left-to-right 7.3.1 expression expression The binary s operator indicates multiplication. If both operands are int or char, the result is int; if one is nt or char and one float or double. the former is converted to double and the result is double: if both are float or double. the result is double. No other combinations are allowed 7.3.2 expression/expression The binary/ operator indicates division. The same type considerations as for multiplication apply 7.3.3 expression% expression The binary 8 operator yields the remainder from the division of the first expression by the second. Both operands must be int or char, and the result is int. In the current implementation the remainder has the same sign as the ⅴ lend 7.4 Additive operators The additive operators+ and-group left-to-right C Reference Manual-7 7.4.1 expression expression o. he result is the sum of the expressions. If both operands are int or char, the result is int. If both are float double. the result is double. If one is char or int and one is float or double. the former is converted to double and the result is double. If an int or char is added to a pointer, the former is converted by multiplying it by the length of the object to which the pointer points and the result is a pointer of the same type as the original pointer. Thus if P is a pointer to an object, the expression"P+1'is a pointer to another object of the same type as the first and immediately following it in storage No other type combinations are allowed 7.4.2 expression- expression The result is the difference of the operands. If both operands are int, char, float, or double, the same type considerations as for apply If an int or char is subtracted from a pointer, the former is converted in the same way as explained under + above If two pointers to objects of the same type are subtracted, the result is converted (by division by the length of the object) to an int representing the number of objects separating the pointed-to objects. This conversion will in gen- eral give unexpected results unless the pointers point to objects in the same array, since pointers, even to objects of the same type, do not necessarily differ by a multiple of the object-length 7.5 Shift operators The shift operators < and > group left-to-right 5.1 expres sion < expression 7.5.2 expression>> expression Both operands must be int or char, and the result is int. The second operand should be non-negative. The value of"<<E2 is el (interpreted as a bit pattern 16 bits long)left-shifted E2 bits; vacated bits are O-filled. The value of 'E1>>E2"' is el (interpreted as a twos complement, 16-bit quantity) arithmetically right-shifted E2 bit po- sitions. Vacated bits are filled by a copy of the sign bit of E1. [Note: the use of arithmetic rather than logical shift does not survive transportation between machines 7.6 Relational operators The relational operators group left-to-right, but this fact is not very useful; asb<" does not mean what it seems 7.6.1 expression expression 7.6. 2 expression > expression 7.6.3 expression <= expression 7.6.4 expression >=expression The operators <(less than),>(greater than), <=(less than or equal to) and >=(greater than or equal to) all yield 0 if the specified relation is false and l if it is true. Operand conversion is exactly the same as for the operator ex- cept that pointers of any kind may be compared; the result in this case depends on the relative locations in storage of the pointed-to objects. It does not seem to be very meaningful to compare pointers with integers other than 0 7.7 Equality operators 7.7.I expression = expression 7.7. 2 expression! expression The ==(equal to) and the !=(not equal to) operators are exactly analogous to the relational operators except for their lower precedence. ( Thus asb===c<d is l whenever asb and c<d have the same truth-value) 7.8 expression expression The operator groups left-to-right. Both operands must be int or char; the result is an int which is the bit- wise logical and function of the operands C Reference Manual-8 7.9 expression expression The operator groups left-to-right. The operands must be int or char; the result is an int which is the bit wise exclusive or function of its operands 7.10 expression expression The operator groups left-to-right. The operands must be int or char; the result is an int which is the bit-wise of its operand 7.11 expression & expression The & operator returns 1 if both its operands are non-zero, 0 otherwise. Unlike &,& guarantees left-to-right evaluation; moreover the second operand is not evaluated if the first operand is O The operands need not have the same type, but each must have one of the fundamental types or be a pointer 7.12 expression expression The operator returns I if either of its operands is non-zero, and 0 otherwise. Unlike guarantees left-to-right evaluation; moreover, the second operand is not evaluated if the value of the first operand is non-zero The operands need not have the same type, but each must have one of the fundamental types or be a pointer. 13 expression expression expression Conditional expressions group left-to-right. The first expression is evaluated and if it is non-zero, the result is the value of the second expression, otherwise that of third expression. If the types of the second and third operand are the same, the result has their common type; otherwise the same conversion rules as for+ apply. Only one of the sec nd and third expressions is evaluated 7.14 Assignment operators There are a number of assignment operators, all of which group right-to-left. All require an lvalue as their left operand, and the type of an assignment expression is that of its left operand. The value is the value stored in the left operand after the assignment has taken place 7.14. 1 lvalue expression The value of the expression replaces that of the object referred to by the lvalue. The operands need not have the same type, but both must be int, char, float, double, or pointer. If neither operand is a pointer, the assign- ment takes place as expected, possibly preceded by conversion of the expression on the right When both operands are int or pointers of any kind, no conversion ever takes place; the value of the expression is simply stored into the object referred to by the lvalue. thus it is possible to generate pointers which will cause ad- dressing exceptions when used 7. 14.2 lvalue=+ expression 7.14.3vale 7. 14.4 value =x expression 7. 14.5 lvalue = expression 7.14.6 lvalue=9 expression 7. 14.7 lvalue => expression 7. 14.8 value =<s expression 7.14.9 value = expression 7. 14.10/value expression 7. 14 11 lvalue= expression The behavior of an expression of the form 'El -op E2 may be inferred by taking it as equivalent to El-El op E2'', however, El is evaluated only once. Moreover, expressions like"i=+ p'in which a pointer is added to an integer are forbidden C Reference Manual-9 7. 15 expression, expression a pair of expressions separated by a comma is evaluated left-to-right and the value of the left expression is dis carded. The type and value of the result are the type and value of the right operand. This operator groups left-to to function calls($7. 1. 6)and lists of initializers( $10. 2 s give right. It should be avoided in situations where comma is given a special meaning, for example in actual arguments 8. Declarations Declarations are used within function definitions to specify the interpretation which C gives to each identifier they do not necessarily reserve storage associated with the identifier. Declarations have the form declaration decl-specifiers declarator-list The declarators in the declarator-list contain the identifiers being declared. The decl-specifiers consist of at most one type-specifier and at most one storage class specifier. decl-specifiers SC-specifler Sc-specifier type-specifier 8. 1 Storage class specifiers The Sc-specifiers are SC-Spec计f auto static extern register The auto, static, and register declarations also serve as definitions in that they cause an appropriate identifiers somewhere outside the function in which they are declared e+ amount of storage to be reserved. In the extern case there must be an extemal definition(see below) for the given There are some severe restrictions on register identifiers: there can be at most 3 register identifiers in any function, and the type of a register identifier can only be int, char, or pointer(not float, double,struc ture, function, or array). Also the address-of operator cannot be applied to such identifiers. Except for these re strictions (in return for which one is rewarded with faster, smaller code), register identifiers behave as if they were automatic. In fact implementations of C are free to treat register as synonymous with auto If the Sc-specifier is missing from a declaration, it is generally taken to be auto 8.2 Type specifiers The type-specifiers are type-specifier int har float double struct i type-decl-list struct identifier, type-decl-list struct identifier The struct specifier is discussed in $8.5. If the type-specifier is missing from a declaration, it is generally taken C Reference Manual-10 8.3 Declarators The declarator-list appearing in a declaration is a comma-separated sequence of declarators declarator-list declarator declarator declarator -list The specifiers in the declaration indicate the type and storage class of the objects to which the declarators refer. Declarators have the syntax declarator identifier declarator declarator() declarator constant-expressionopt declarator) The grouping in this definition is the same as in expressions 8.4 Meaning of declarators Each declarator is taken to be an assertion that when a construction of the same form as the declarator appears in an expression, it yields an object of the indicated type and storage class. Each declarator contains exactly one identi fier: it is this identifier that is declared If an unadorned identifier appears as a declarator, then it has the type indicated by the specifier heading the decla- ration If a declarator has the form D for d a declarator, then the contained identifier has the type "pointer to. . ', where .. 2 is the type which the identifier would have had if the declarator had been simply d If a declarator has the form D() then the contained identifier has the type function returning.,,, where.. is the type which the identifier would have had if the declarator had been simply d a declarator may have the form DI constant-expression In the first case the constant expression is an expression whose value is determinable at compile time, and whose type is int. in the second the constant I is used.( Constant expressions are defined precisely in $15. Such a declarator makes the contained identifier have type"array. ' If the unadorned declarator D would specify a non array of type".', then the declarator D[i]yields a 1-dimensional array with rank i of objects of type the unadorned declarator D would specify an n-dimensional array with rank i, xi,x. Xi, then the declarator D[1+1] vields an(n+1)- dimensional array with rank i1×i2×…xin× An array may be constructed from one of the basic types, from a pointer, from a structure, or from another array (to generate a multi-dimensional array) Finally, parentheses in declarators do not alter the type of the contained identifier except insofar as they alter the binding of the components of the declarator Not all the possibilities allowed by the syntax above are actually permitted. The restrictions are as follows: fune tions may not return arrays, structures or functions, although they may return pointers to such things; there are no ar- ays of functions, although there may be arrays of pointers to functions. Likewise a structure may not contain a unction, but it may contain a pointer to a function

...展开详情
试读 31P C Reference Manual.pdf
立即下载 低至0.43元/次 身份认证VIP会员低至7折
一个资源只可评论一次,评论内容不能少于5个字
您会向同学/朋友/同事推荐我们的CSDN下载吗?
谢谢参与!您的真实评价是我们改进的动力~
  • GitHub

    绑定GitHub第三方账户获取
  • 签到新秀

    累计签到获取,不积跬步,无以至千里,继续坚持!
  • 分享王者

    成功上传51个资源即可获取
关注 私信
上传资源赚积分or赚钱
最新推荐
C Reference Manual.pdf 12积分/C币 立即下载
1/31
C Reference Manual.pdf第1页
C Reference Manual.pdf第2页
C Reference Manual.pdf第3页
C Reference Manual.pdf第4页
C Reference Manual.pdf第5页
C Reference Manual.pdf第6页
C Reference Manual.pdf第7页

试读结束, 可继续读3页

12积分/C币 立即下载 >