home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V6 / usr / doc / c / c2 < prev    next >
Encoding:
Text File  |  1975-06-26  |  16.8 KB  |  549 lines

  1. .ul
  2. 7.  Expressions
  3. .et
  4. The precedence of expression operators is the same
  5. as the order of the major
  6. subsections of this section (highest precedence first).
  7. Thus the expressions referred to as the operands of \fG+\fR
  8. (\(sc7.4)
  9. are those expressions defined in \(sc\(sc7.1_7.3.
  10. Within each subsection, the operators have the same
  11. precedence.
  12. Left- or right-associativity is specified
  13. in each subsection for the operators
  14. discussed therein.
  15. The precedence and associativity of all the expression
  16. operators is summarized in an
  17. appendix.
  18. .pg
  19. Otherwise the order of evaluation of expressions
  20. is undefined.  In particular the compiler
  21. considers itself free to
  22. compute subexpressions in the order it believes
  23. most efficient,
  24. even if the subexpressions
  25. involve side effects.
  26. .ms
  27. 7.1  Primary expressions
  28. .et
  29. Primary expressions
  30. involving \fG.\fR^, \fG\(mi>\fR, subscripting, and function calls
  31. group left to right.
  32. .ms
  33. 7.1.1  \fIidentifier\fR
  34. .et
  35. An identifier is a primary expression, provided it has been
  36. suitably declared as discussed below.
  37. Its type is specified by its declaration.
  38. However, if the type of the identifier is ``array of .^.^.'',
  39. then the value of the identifier-expression
  40. is a pointer
  41. to the first object in the array, and the
  42. type of the expression is
  43. ``pointer to .^.^.''.
  44. Moreover, an array identifier is not an lvalue
  45. expression.
  46. .pg
  47. Likewise, an identifier which is declared
  48. ``function returning .^.^.'',
  49. when used except in the function-name position
  50. of a call, is converted to ``pointer to function returning .^.^.''.
  51. .ms
  52. 7.1.2  \fIconstant\fR
  53. .et
  54. A decimal, octal, character, or floating
  55. constant is a primary expression.
  56. Its type is \fGint\fR in the first three cases,
  57. \fGdouble\fR in the last.
  58. .ms
  59. 7.1.3  \fIstring\fR
  60. .et
  61. A string is a primary expression.
  62. Its type is originally ``array of \fGchar\fR''; but following
  63. the same rule as in \(sc7.1.1 for identifiers,
  64. this is modified to ``pointer to \fGchar\fR'' and the
  65. result is a pointer to the first character
  66. in the string.
  67. .ms
  68. 7.1.4  \fG(\fI expression \fG)\fR
  69. .et
  70. A parenthesized expression is a primary expression
  71. whose type and value are identical
  72. to those of the unadorned expression.
  73. The presence of parentheses does
  74. not affect whether the expression is an
  75. lvalue.
  76. .ms
  77. 7.1.5  \fIprimary-expression\fG [\fI expression \fG]\fR
  78. .et
  79. A primary expression followed by an expression in square
  80. brackets is a primary expression.
  81. The intuitive meaning is that of a subscript.
  82. Usually, the primary expression has type ``pointer to .^.^.'',
  83. the subscript expression is \fGint\fR,
  84. and the type of the result is ``^.^.^.^''.
  85. The expression ``E1[E2]'' is
  86. identical (by definition) to ``\**^(^^(^E1^)^+^(^E2^)^^)^''.
  87. All the clues
  88. needed to understand
  89. this notation are contained in this section together
  90. with the discussions
  91. in \(sc\(sc 7.1.1, 7.2.1, and 7.4.1 on identifiers,
  92. \fG\**\fR, and \fG+\fR respectively;
  93. \(sc14.3 below summarizes the implications.
  94. .ms
  95. 7.1.6  \fIprimary-expression \fG( \fIexpression-list\*(op \fG)
  96. .et
  97. A function call is a primary expression followed by parentheses
  98. containing a possibly
  99. empty, comma-separated list of expressions
  100. which constitute the actual arguments to the
  101. function.
  102. The primary expression must be of type ``function returning .^.^.'',
  103. and the result of the function call is of type ``^.^.^.^''.
  104. As indicated
  105. below, a hitherto unseen identifier followed
  106. immediately by a left parenthesis
  107. is contextually declared
  108. to represent a function returning
  109. an integer;
  110. thus in the most common case, integer-valued functions
  111. need not be declared.
  112. .pg
  113. Any actual arguments of type \fGfloat\fR are
  114. converted to \fGdouble\fR before the call;
  115. any of type \fGchar\fR are converted to \fGint\fR.
  116. .pg
  117. In preparing for the call to a function,
  118. a copy is made of each actual parameter;
  119. thus, all argument-passing in C is strictly by value.
  120. A function may
  121. change the values of its formal parameters, but
  122. these changes cannot possibly affect the values
  123. of the actual parameters.
  124. On the other hand, it is perfectly possible
  125. to pass a pointer on the understanding
  126. that the function may change the value
  127. of the object to which the pointer points.
  128. .pg
  129. Recursive calls to any
  130. function are permissible.
  131. .ms
  132. 7.1.7  \fIprimary-lvalue \fG.\fI member-of-structure\fR
  133. .et
  134. An lvalue expression followed by a dot followed by the name
  135. of a member of a structure is a primary expression.
  136. The object referred to
  137. by the lvalue is assumed to have the
  138. same form as the structure
  139. containing the structure member.
  140. The result of the expression is an lvalue appropriately
  141. offset from the origin of the given lvalue
  142. whose type is that of the
  143. named structure member.
  144. The given lvalue is not required to have
  145. any particular type.
  146. .pg
  147. Structures are discussed in \(sc8.5.
  148. .ms
  149. 7.1.8  \fIprimary-expression \fG\(mi>\fI member-of-structure\fR
  150. .et
  151. The primary-expression is assumed to be a pointer
  152. which points to an object of the same form
  153. as the structure of which the member-of-structure is
  154. a part.
  155. The result is an lvalue appropriately
  156. offset from the origin of the pointed-to structure
  157. whose type is that of the named structure member.
  158. The type of the primary-expression need not
  159. in fact be pointer; it is sufficient that
  160. it be a pointer, character, or integer.
  161. .pg
  162. Except for the relaxation of the requirement that
  163. E1 be of pointer type, the expression ``E1\(mi>MOS''
  164. is exactly equivalent to ``(\**E1).MOS''.
  165. .ms
  166. 7.2  Unary operators
  167. .et
  168. Expressions with unary operators
  169. group right-to-left.
  170. .ms
  171. 7.2.1  \fG\**\fI expression\fR
  172. .et
  173. The unary \fG\**\fR operator
  174. means
  175. .ft I
  176. indirection:
  177. .ft R
  178. the expression must be a pointer, and the result
  179. is an lvalue referring to the object to
  180. which the expression points.
  181. If the type of the expression is ``pointer to .^.^.'',
  182. the type of the result is ``^.^.^.^''.
  183. .ms
  184. 7.2.2  \fG&\fI lvalue-expression\fR
  185. .et
  186. The result of the unary \fG&\fR operator is a pointer
  187. to the object referred to by the
  188. lvalue-expression.
  189. If the type of the lvalue-expression is ``^.^.^.^'',
  190. the type of the result is ``pointer to .^.^.''.
  191. .ms
  192. 7.2.3  \fG\(mi\fI expression\fR
  193. .et
  194. The result is the negative of the expression,
  195. and has the same type.
  196. The type of the expression must be \fGchar\fR, \fGint\fR, \fGfloat\fR,
  197. or \fGdouble\fR.
  198. .ms
  199. 7.2.4  \fG!\fI expression\fR
  200. .et
  201. The result of the logical negation operator \fG!\fR
  202. is 1 if the value of the expression is 0, 0 if the value of the
  203. expression is non-zero.
  204. The type of the result is \fGint\fR.
  205. This operator is applicable only to \fGint\fRs or \fGchar\fRs.
  206. .ms
  207. 7.2.5  \fG\*~\fI expression\fR
  208. .et
  209. The \*~ operator yields the one's complement of its operand.
  210. The type of the expression must be \fGint\fR or \fGchar\fR,
  211. and the result is \fGint\fR.
  212. .ms
  213. 7.2.6  ++ \fIlvalue-expression\fR
  214. .et
  215. The object referred to by the lvalue expression
  216. is incremented.
  217. The value is the new value of the lvalue expression
  218. and the type is the type of the lvalue.
  219. If the expression is \fGint\fR or \fGchar\fR,
  220. it is incremented by 1;
  221. if it is a pointer to an object, it is incremented
  222. by the length of the object.
  223. ++ is applicable only to these types.
  224. (Not, for example, to \fGfloat\fR or \fGdouble\fR.)
  225. .ms
  226. 7.2.7  \fR\(mi\(mi\fI lvalue-expression\fR
  227. .et
  228. The object referred to by the lvalue expression is decremented
  229. analogously to the ++ operator.
  230. .ms
  231. 7.2.8  \fIlvalue-expression ++
  232. .et
  233. The result is the value of the object
  234. referred to by the lvalue expression.
  235. After the result is noted, the object
  236. referred to by the lvalue is incremented in the same
  237. manner as for the prefix ++ operator: by 1 for an \fGint\fR
  238. or \fGchar\fR, by the length of the pointed-to object for a pointer.
  239. The type of the result is the same as the
  240. type of the lvalue-expression.
  241. .ms
  242. 7.2.9  \fIlvalue-expression \(mi\(mi
  243. .et
  244. The result of the expression is the value of the object
  245. referred to by the
  246. the lvalue expression.
  247. After the result is noted, the object referred
  248. to by the lvalue expression is decremented in a way
  249. analogous to the postfix ++ operator.
  250. .ms
  251. 7.2.10  \fGsizeof \fIexpression
  252. .et
  253. The \fGsizeof\fR operator yields the size,
  254. in bytes, of its operand.
  255. When applied to an array, the result is the total
  256. number of bytes in the array.
  257. The size is determined from
  258. the declarations of
  259. the objects in the expression.
  260. This expression is semantically an integer constant and may
  261. be used anywhere a constant is required.
  262. Its major use is in communication with routines
  263. like storage allocators and I/O systems.
  264. .ms
  265. 7.3  Multiplicative operators
  266. .et
  267. The multiplicative operators
  268. \fG\**\fR, \fG/\fR, and \fG%\fR
  269. group left-to-right.
  270. .ms
  271. 7.3.1  \fIexpression\fG \** \fIexpression\fR
  272. .et
  273. The binary \fG\**\fR operator indicates multiplication.
  274. If both operands are \fGint\fR or \fGchar\fR, the result
  275. is \fGint\fR; if one is \fGint\fR or \fGchar\fR and one \fGfloat\fR or \fGdouble\fR, the
  276. former is converted to \fGdouble\fR, and the result is \fGdouble\fR;
  277. if both are \fGfloat\fR or \fGdouble\fR, the result is \fGdouble\fR.
  278. No other combinations are allowed.
  279. .ms
  280. 7.3.2  \fIexpression \fG/\fI expression\fR
  281. .et
  282. The binary \fG/\fR operator indicates division.
  283. The same type considerations as for multiplication
  284. apply.
  285. .ms
  286. 7.3.3  \fIexpression \fG%\fI expression\fR
  287. .et
  288. The binary \fG%\fR operator yields the remainder
  289. from the division of the first expression by the second.
  290. Both operands must be \fGint\fR or \fGchar\fR, and the
  291. result is \fGint\fR.
  292. In the current implementation,
  293. the remainder has the same sign as the dividend.
  294. .ms
  295. 7.4  Additive operators
  296. .et
  297. The additive operators \fG+\fR and \fG\(mi\fR group left-to-right.
  298. .ms
  299. 7.4.1  \fIexpression \fG+\fI expression\fR
  300. .et
  301. The result is the sum of the expressions.
  302. If both operands are \fGint\fR or \fGchar\fR,
  303. the result is \fGint\fR.
  304. If both
  305. are \fGfloat\fR or \fGdouble\fR, the result is \fGdouble\fR.
  306. If one is \fGchar\fR or \fGint\fR and one is \fGfloat\fR
  307. or \fGdouble\fR, the former is converted to \fGdouble\fR and the result is \fGdouble\fR.
  308. If an \fGint\fR or \fGchar\fR is added to a pointer, the former
  309. is converted by multiplying it
  310. by the length of the object to which the
  311. pointer points and the result is a pointer
  312. of the same type as the original pointer.
  313. Thus if P is a pointer
  314. to an object, the expression ``P+1'' is a pointer
  315. to another object of the same type as
  316. the first and immediately following
  317. it in storage.
  318. .pg
  319. No other type combinations are allowed.
  320. .ms
  321. 7.4.2  \fIexpression \fG\(mi \fIexpression\fR
  322. .et
  323. The result is the difference of the operands.
  324. If both operands are \fGint\fR, \fGchar\fR, \fGfloat\fR, or \fGdouble\fR,
  325. the same type considerations
  326. as for \fG+\fR apply.
  327. If an \fGint\fR or \fGchar\fR is subtracted
  328. from a pointer, the
  329. former is converted in the same way as explained under
  330. \fG+\fR above.
  331. .pg
  332. If two pointers to objects of the same type are subtracted,
  333. the result is converted
  334. (by division by the length of the object)
  335. to an \fGint\fR representing the number of
  336. objects separating
  337. the pointed-to objects.
  338. This conversion will in general give unexpected
  339. results unless the pointers point
  340. to objects in the same array, since pointers, even
  341. to objects of the same type, do not necessarily differ
  342. by a multiple of the object-length.
  343. .ms
  344. 7.5  Shift operators
  345. .et
  346. The shift operators \fG<<\fR and \fG>>\fR group left-to-right.
  347. .ms
  348. 7.5.1  \fIexpression \fG<< \fIexpression\fR
  349. .br
  350. 7.5.2  \fIexpression \fG>> \fIexpression\fR
  351. .et
  352. Both operands must be \fGint\fR or \fGchar\fR,
  353. and the result is \fGint\fR.
  354. The second operand should
  355. be non-negative.
  356. The value of ``E1<<E2'' is E1 (interpreted as a bit
  357. pattern 16 bits long) left-shifted E2 bits;
  358. vacated bits are 0-filled.
  359. The value of ``E1>>E2'' is E1 (interpreted as a two's
  360. complement, 16-bit quantity) arithmetically
  361. right-shifted E2 bit positions.
  362. Vacated bits are filled by a copy of the sign bit of E1.
  363. [Note: the use
  364. of arithmetic rather than logical shift does not
  365. survive transportation between machines.]
  366. .ms
  367. 7.6  Relational operators
  368. .et
  369. The relational operators group left-to-right, but
  370. this fact is not very useful; ``a<b<c'' does
  371. not mean what it seems to.
  372. .ms
  373. 7.6.1  \fIexpression \fG<\fI expression\fR
  374. .br
  375. .ne 4
  376. 7.6.2  \fIexpression \fG>\fI expression\fR
  377. .br
  378. .ne 4
  379. 7.6.3  \fIexpression \fG<=\fI expression\fR
  380. .br
  381. .ne 4
  382. 7.6.4  \fIexpression \fG>=\fI expression\fR
  383. .et
  384. The operators < (less than), > (greater than), <= (less than
  385. or equal to) and >= (greater than or equal to)
  386. all yield 0 if the specified relation is false
  387. and 1 if it is true.
  388. Operand conversion is exactly the same as for the \fG+\fR
  389. operator except that pointers of any kind may be compared;
  390. the result in this case depends on the relative
  391. locations in storage of the pointed-to objects.
  392. It does not seem to be very mean$ing$ful
  393. to compare pointers with integers
  394. other than 0.
  395. .ms
  396. .ti 0
  397. 7.7  Equality operators
  398. .et
  399. .ne 4
  400. .ti 0
  401. 7.7.1  \fIexpression \fG==\fI expression\fR
  402. .br
  403. .ne 4
  404. 7.7.2  \fIexpression \fG!=\fI expression\fR
  405. .et
  406. The \fG==\fR (equal to) and the \fG!=\fR (not equal to) operators
  407. are exactly analogous to the relational
  408. operators except for their lower
  409. precedence.
  410. (Thus ``a<b@==@c<d'' is 1 whenever
  411. a<b and c<d
  412. have the same truth-value).
  413. .ms
  414. 7.8  \fIexpression \fG&\fI expression\fR
  415. .et
  416. The \fG&\fR operator groups left-to-right.
  417. Both operands must be \fGint\fR or \fGchar\fR;
  418. the result is an \fGint\fR which is the bit-wise
  419. logical \fGand\fR function of the operands.
  420. .ms
  421. .tr ^^
  422. 7.9  \fIexpression \fG^ \fIexpression\fR
  423. .et
  424. The \fG^\fR operator groups left-to-right.
  425. The operands must be \fGint\fR or \fGchar\fR; the result is an \fGint\fR
  426. which is the bit-wise exclusive \fGor\fR function of
  427. its operands.
  428. .tr ^\|
  429. .ms
  430. 7.10  \fIexpression ^\(or \fIexpression\fR
  431. .et
  432. The \(or operator groups left-to-right.
  433. The operands must be \fGint\fR or \fGchar\fR; the result is an \fGint\fR
  434. which is the bit-wise inclusive \fGor\fR of its operands.
  435. .ms
  436. 7.11  \fIexpression \fG&&\fI expression
  437. .et
  438. The \fG&&\fR operator returns 1 if both its operands
  439. are non-zero, 0 otherwise.
  440. Unlike \fG&\fR, \fG&&\fR guarantees left-to-right
  441. evaluation; moreover the second operand is not evaluated
  442. if the first operand is 0.
  443. .pg
  444. The operands need not have the same type, but each
  445. must have one of the fundamental
  446. types or be a pointer.
  447. .ms
  448. 7.12  \fIexpression ^\(or\(or \fIexpression
  449. .et
  450. The ^\(or\(or operator returns 1 if either of its operands
  451. is non-zero, and 0 otherwise.
  452. Unlike ^\(or^,
  453. ^\(or\(or guarantees left-to-right evaluation; moreover,
  454. the second operand is not evaluated
  455. if the value of the first operand is non-zero.
  456. .pg
  457. The operands need not have the same type, but each
  458. must
  459. have one of the fundamental types
  460. or be a pointer.
  461. .ms
  462. 7.13  \fIexpression \fG? \fIexpression \fG:\fI expression\fR
  463. .et
  464. Conditional expressions group left-to-right.
  465. The first expression is evaluated
  466. and if it is non-zero, the result is the value of the
  467. second expression, otherwise that of third expression.
  468. If the types of the second and third operand are the same, the
  469. result has their common type;
  470. otherwise the same conversion rules as for \fG+\fR
  471. apply.
  472. Only one of the second and third
  473. expressions is evaluated.
  474. .ms
  475. 7.14  Assignment operators
  476. .et
  477. There are a number of assignment operators,
  478. all of which group right-to-left.
  479. All require an lvalue as their left operand,
  480. and the type of an assignment expression is that
  481. of its left operand.
  482. The value is the value stored in the
  483. left operand after the assignment has taken place.
  484. .ms
  485. 7.14.1  \fIlvalue \fG= \fIexpression\fR
  486. .et
  487. The value of the expression replaces that of the object
  488. referred
  489. to by the lvalue.
  490. The operands need not have the same type, but
  491. both must be
  492. \fGint\fR, \fGchar\fR, \fGfloat\fR, \fGdouble\fR,
  493. or pointer.
  494. If neither operand is a pointer,
  495. the assignment takes place as expected, possibly
  496. preceded by conversion of the expression on the right.
  497. .pg
  498. When both operands are \fGint\fR
  499. or pointers of any kind, no conversion
  500. ever takes place;
  501. the value of the expression is simply stored
  502. into the object referred to by the lvalue.
  503. Thus it is possible to
  504. generate pointers which will cause addressing
  505. exceptions when used.
  506. .ms
  507. .ta \w'0.00.00 'u
  508. 7.14.2    \fIlvalue \fG=+ \fIexpression\fR
  509. .br
  510. 7.14.3    \fIlvalue \fG=\(mi \fIexpression\fR
  511. .br
  512. 7.14.4    \fIlvalue \fG=\** \fIexpression\fR
  513. .br
  514. 7.14.5    \fIlvalue \fG=/ \fIexpression\fR
  515. .br
  516. 7.14.6    \fIlvalue \fG=% \fIexpression\fR
  517. .br
  518. 7.14.7    \fIlvalue \fG=>> \fIexpression\fR
  519. .br
  520. 7.14.8    \fIlvalue \fG=<< \fIexpression\fR
  521. .br
  522. 7.14.9    \fIlvalue \fG=& \fIexpression\fR
  523. .br
  524. .tr ^^
  525. 7.14.10    \fIlvalue \fG=^ \fIexpression\fR
  526. .br
  527. .tr ^\|
  528. 7.14.11    \fIlvalue \fG=^\(or \fIexpression\fR
  529. .et
  530. The behavior of an expression
  531. of the form ``E1@=op@E2'' may be inferred by
  532. taking it as equivalent to ``E1@=@E1@op@E2'';
  533. however, E1 is evaluated only once.
  534. Moreover,
  535. expressions like ``i@=+@p''
  536. in which a pointer is added to an integer, are forbidden.
  537. .ms
  538. 7.15  \fIexpression \fG,\fI expression\fR
  539. .et
  540. A pair of expressions separated by a comma is evaluated
  541. left-to-right and the value of the left expression is
  542. discarded.
  543. The type and value of the result are the
  544. type and value of the right operand.
  545. This operator groups left-to-right.
  546. It should be avoided in situations where comma is given
  547. a special meaning, for example in actual arguments
  548. to function calls (\(sc7.1.6) and lists of initializers (\(sc10.2).
  549.