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

  1. .ul
  2. 10. External definitions
  3. .et
  4. A C program consists of a sequence of external definitions.
  5. External definitions may be given for functions, for
  6. simple variables, and for arrays.
  7. They are used both to declare and to reserve storage for
  8. objects.
  9. An external definition declares an identifier to
  10. have storage class \fGextern\fR and
  11. a specified type.
  12. The type-specifier (\(sc8.2) may be empty, in which
  13. case the type is taken to be \fGint\fR.
  14. .ms
  15. 10.1  External function definitions
  16. .et
  17. Function definitions have the form
  18. .dp 2
  19. .ta .5i 1i 1.5i 2i 2.5i
  20.     function-definition:
  21.         type-specifier\*(op function-declarator function-body
  22. .ed
  23. A function declarator is similar to a declarator
  24. for a ``function returning ...'' except that
  25. it lists the formal parameters of
  26. the function being defined.
  27. .dp 2
  28.     function-declarator:
  29.         declarator \fG( \fIparameter-list\*(op \fG)
  30.  
  31. .ft I
  32.     parameter-list:
  33.         identifier
  34.         identifier \fG,\fI parameter-list
  35. .ed
  36. The function-body
  37. has the form
  38. .dp 1
  39.     function-body:
  40.         type-decl-list function-statement
  41. .ed
  42. The purpose of the
  43. type-decl-list is to give the types of the formal parameters.
  44. No other identifiers should be declared in this list, and formal
  45. parameters should be declared only here.
  46. .pg
  47. The function-statement
  48. is just a compound
  49. statement which may have
  50. declarations at the start.
  51. .dp 2
  52.     function-statement:
  53.         { declaration-list\*(op statement-list }
  54. .ed
  55. A simple example of a complete function definition is
  56. .sp .7
  57. .ne 7
  58. .nf
  59. .bG
  60.     int max^(^a, b, c)
  61.     int a, b, c;
  62.     {
  63.         int m;
  64.         m = ^(^a^>^b^)? a^:^b^;
  65.         return^(^m^>^c? m^:^c^)^;
  66.     }
  67. .sp .7
  68. .eG
  69. .fi
  70. Here ``int'' is the type-specifier;
  71. ``max(a,@b,@c)'' is the function-declarator;
  72. ``int@a,@b,@c;'' is the type-decl-list for
  73. the formal
  74. parameters;
  75. ``{@.^.^.@}'' is the function-statement.
  76. .pg
  77. C converts all
  78. \fGfloat\fR actual parameters
  79. to \fGdouble\fR, so formal parameters declared \fGfloat\fR
  80. have their declaration adjusted to read \fGdouble\fR.
  81. Also, since a reference to an array in any context
  82. (in particular as an actual parameter)
  83. is taken to mean
  84. a pointer to the first element of the array,
  85. declarations of formal parameters declared ``array of ...''
  86. are adjusted to read ``pointer to ...''.
  87. Finally, because neither structures
  88. nor functions can be passed
  89. to a function, it is useless to declare
  90. a formal parameter to be a structure or
  91. function (pointers to structures or
  92. functions are of course permitted).
  93. .pg
  94. A free \fGreturn\fR statement is supplied at the
  95. end of each function definition, so
  96. running off the end
  97. causes
  98. control, but no value, to be returned to
  99. the caller.
  100. .ms
  101. 10.2  External data definitions
  102. .et
  103. An external data definition has the form
  104. .dp 2
  105.     data-definition:
  106.         \fGextern\*(op \fItype-specifier\*(op init-declarator-list\*(op \fG;
  107. .ed
  108. The optional
  109. .ft G
  110. extern
  111. .ft R
  112. specifier is discussed in \(sc 11.2.
  113. If given, the init-declarator-list is a
  114. comma-separated list of declarators each of which may be
  115. followed by an initializer for the declarator.
  116. .dp 2
  117.     init-declarator-list:
  118.         init-declarator
  119.         init-declarator \fG, \fIinit-declarator-list
  120. .ed
  121. .dp 2
  122.     init-declarator:
  123.         declarator initializer\*(op
  124. .ed
  125. Each initializer represents the initial value for the
  126. corresponding object
  127. being defined (and declared).
  128. .dp 5
  129.     initializer:
  130.         constant
  131.         { constant-expression-list }
  132. .ed
  133. .dp 3
  134.     constant-expression-list:
  135.         constant-expression
  136.         constant-expression \fG,\fI constant-expression-list
  137. .ed
  138. Thus an initializer consists of
  139. a constant-valued expression, or
  140. comma-separated list of expressions,
  141. inside braces.
  142. The braces may be dropped
  143. when the expression is just a plain
  144. constant.
  145. The exact meaning of a constant expression is discussed
  146. in \(sc15.
  147. The expression list is used to initialize arrays;
  148. see below.
  149. .pg
  150. The type of the identifier being
  151. defined
  152. should be compatible
  153. with the type of the initializer:
  154. a \fGdouble\fR constant
  155. may initialize a \fGfloat\fR or \fGdouble\fR identifier;
  156. a non-floating-point expression
  157. may
  158. initialize an \fGint\fR, \fGchar\fR, or pointer.
  159. .pg
  160. An initializer for an array may contain
  161. a comma-separated list of compile-time expressions.
  162. The length of the array is taken to be the
  163. maximum of the number of expressions in the list
  164. and the square-bracketed constant in the
  165. array's declarator.
  166. This constant may be missing,
  167. in which case 1 is used.
  168. The expressions initialize successive members of the array
  169. starting at the origin (subscript 0)
  170. of the array.
  171. The acceptable expressions for an array of type
  172. ``array of ...'' are the same as
  173. those for type ``...''.
  174. As a special case, a single
  175. string may be given as the initializer
  176. for an array of \fGchar\fRs; in this case,
  177. the characters in the string are taken
  178. as the initializing values.
  179. .pg
  180. Structures can be initialized, but this operation
  181. is incompletely implemented and machine-dependent.
  182. Basically the structure is regarded as a sequence
  183. of words and the initializers are placed into those
  184. words.
  185. Structure initialization,
  186. using a comma-separated list in braces,
  187. is safe if all the members of the structure
  188. are integers or pointers but is otherwise ill-advised.
  189. .pg
  190. The initial value of any externally-defined
  191. object not explicitly initialized is guaranteed
  192. to be 0.
  193. .ul
  194. 11.  Scope rules
  195. .et
  196. A complete C program need not all
  197. be compiled at the same time: the source text of the
  198. program
  199. may be kept in several files, and precompiled
  200. routines may be loaded from
  201. libraries.
  202. Communication among the functions of a program
  203. may be carried out both through explicit calls
  204. and through manipulation of external data.
  205. .pg
  206. Therefore, there are two kinds of scope to consider:
  207. first, what may be called the \fIlexical scope\fR
  208. of an identifier, which is essentially the
  209. region of a program during which it may
  210. be used without drawing ``undefined identifier''
  211. diagnostics;
  212. and second, the scope
  213. associated with external identifiers,
  214. which is characterized by the rule
  215. that references to the same external
  216. identifier are references to the same object.
  217. .ms
  218. 11.1  Lexical scope
  219. .et
  220. C is not a block-structured language;
  221. this may fairly be considered a defect.
  222. The lexical scope of names declared in external definitions
  223. extends from their definition through
  224. the end of the file
  225. in which they appear.
  226. The lexical scope of names declared at the head of functions
  227. (either as formal parameters
  228. or in the declarations heading the
  229. statements constituting the function itself)
  230. is the body of the function.
  231. .pg
  232. It is an error to redeclare identifiers already
  233. declared in the current context,
  234. unless the new declaration specifies the same type
  235. and storage class as already possessed by the identifiers.
  236. .ms
  237. 11.2  Scope of externals
  238. .et
  239. If a function declares an identifier to be
  240. \fGextern\fR,
  241. then somewhere among the files or libraries
  242. constituting the complete program
  243. there must be an external definition
  244. for the identifier.
  245. All functions in a given program which refer to the same
  246. external identifier refer to the same object,
  247. so care must be taken that the type and extent
  248. specified in the definition
  249. are compatible with those specified
  250. by each function which references the data.
  251. .pg
  252. In \s8PDP\s10-11 C,
  253. it is explicitly permitted for (compatible)
  254. external definitions of the same identifier
  255. to be present in several of the
  256. separately-compiled pieces of a complete program,
  257. or even twice within the same program file,
  258. with the important limitation that the identifier
  259. may be initialized in at most one of the
  260. definitions.
  261. In other operating systems, however, the compiler must know
  262. in just which file the storage
  263. for the identifier is allocated, and in which file
  264. the identifier is merely being referred to.
  265. In the implementations of C for such systems,
  266. the appearance of the
  267. .ft G
  268. extern
  269. .ft R
  270. keyword before an external definition
  271. indicates that storage for the identifiers
  272. being declared
  273. will be allocated in another file.
  274. Thus in a multi-file program,
  275. an external data definition without
  276. the
  277. .ft G
  278. extern
  279. .ft R
  280. specifier must appear in exactly one of the files.
  281. Any other files which wish to give an external definition
  282. for the identifier must
  283. include the
  284. .ft G
  285. extern
  286. .ft R
  287. in the definition.
  288. The identifier can be initialized only in the file
  289. where storage is allocated.
  290. .pg
  291. In \s8PDP\s10-11 C none of this nonsense is necessary
  292. and the
  293. .ft G
  294. extern
  295. .ft R
  296. specifier is ignored in external definitions.
  297. .ul
  298. 12.  Compiler control lines
  299. .et
  300. When a line of a C program begins
  301. with the character \fG#\fR, it is interpreted not by
  302. the compiler itself, but by a preprocessor which is capable
  303. of replacing instances of given identifiers
  304. with arbitrary token-strings and of inserting
  305. named files into the source program.
  306. In order to cause this preprocessor to be invoked, it
  307. is necessary that the very first line of the program
  308. begin with \fG#\fR.
  309. Since null lines are ignored by the preprocessor,
  310. this line need contain no other information.
  311. .ms
  312. 12.1  Token replacement
  313. .et
  314. A compiler-control line of the form
  315. .dp 1
  316.     \fG# define \fIidentifier token-string
  317. .ed
  318. (note: no trailing semicolon)
  319. causes the preprocessor to replace subsequent instances
  320. of the identifier with the given string of tokens
  321. (except within compiler control lines).
  322. The replacement token-string has comments removed from
  323. it, and it is surrounded with blanks.
  324. No rescanning of the replacement string is attempted.
  325. This facility is most valuable for definition of ``manifest constants'',
  326. as in
  327. .sp .7
  328. .nf
  329. .bG
  330.     # define tabsize 100
  331.     .^.^.
  332.     int table[tabsize];
  333. .sp .7
  334. .eG
  335. .fi
  336. .ms
  337. 12.2  File inclusion
  338. .et
  339. Large C programs
  340. often contain many external data definitions.
  341. Since the lexical scope of external definitions
  342. extends to the end of the program file,
  343. it is good practice
  344. to put all the external definitions for
  345. data at the start of the
  346. program file, so that the functions defined within
  347. the file need not repeat tedious and error-prone
  348. declarations for each external identifier they use.
  349. It is also useful to put a heavily used structure definition
  350. at the start and use its structure tag to declare
  351. the \fGauto\fR pointers to the structure
  352. used within functions.
  353. To further exploit
  354. this technique when a large C program
  355. consists of several files, a compiler control line of
  356. the form
  357. .dp 1
  358.     \fG# include "\fIfilename^\fG"
  359. .ed
  360. results in the replacement of that
  361. line by the entire contents of the file
  362. \fIfilename\fR.
  363. .pg
  364. .ul
  365. 13.  Implicit declarations
  366. .et
  367. It is not always necessary to specify
  368. both the storage class and the type
  369. of identifiers in a declaration.
  370. Sometimes the storage class is supplied by
  371. the context: in external definitions,
  372. and in declarations of formal parameters
  373. and structure members.
  374. In a declaration inside a function,
  375. if a storage class but no type
  376. is given, the identifier is assumed
  377. to be \fGint\fR;
  378. if a type but no storage class is indicated,
  379. the identifier is assumed to
  380. be \fGauto\fR.
  381. An exception to the latter rule is made for
  382. functions, since \fGauto\fR functions are mean$ing$less
  383. (C being incapable of compiling code into the stack).
  384. If the type of an identifier is ``function returning ...'',
  385. it is implicitly declared to be \fGextern\fR.
  386. .pg
  387. In an expression, an identifier
  388. followed by \fG(\fR and not currently declared
  389. is contextually
  390. declared to be ``function returning \fGint\fR''.
  391. .pg
  392. Undefined identifiers not followed by \fG(\fR
  393. are assumed to be labels which
  394. will be defined later in the function.
  395. (Since a label is not an lvalue, this accounts
  396. for the ``Lvalue required'' error
  397. message
  398. sometimes noticed when
  399. an undeclared identifier is used.)
  400. Naturally, appearance of
  401. an identifier as a label declares it as such.
  402. .pg
  403. For some purposes it is best to consider formal
  404. parameters as belonging to their own storage class.
  405. In practice, C treats parameters as if they
  406. were automatic (except that, as mentioned
  407. above, formal parameter arrays and \fGfloat\fRs
  408. are treated specially).
  409. .ul
  410. 14.  Types revisited
  411. .et
  412. This section summarizes the operations
  413. which can be performed on objects of certain types.
  414. .ms
  415. 14.1  Structures
  416. .et
  417. There are only two things that can be done with
  418. a structure:
  419. pick out one of its members (by means of the
  420. \fG^.^\fR or \fG\(mi>\fR operators); or take its address (by unary \fG&\fR).
  421. Other operations, such as assigning from or to
  422. it or passing it as a parameter, draw an
  423. error message.
  424. In the future, it is expected that
  425. these operations, but not necessarily
  426. others, will be allowed.
  427. .ms
  428. 14.2  Functions
  429. .et
  430. There are only two things that
  431. can be done with a function:
  432. call it, or take its address.
  433. If the name of a function appears in an
  434. expression not in the function-name position of a call,
  435. a pointer to the function is generated.
  436. Thus, to pass one function to another, one
  437. might say
  438. .bG
  439. .sp .7
  440. .nf
  441.     int f(^^);
  442.     ...
  443.     g(^f^);
  444. .sp .7
  445. .eG
  446. .ft R
  447. Then the definition of \fIg \fRmight read
  448. .sp .7
  449. .bG
  450.     g^(^funcp^)
  451.     int (\**funcp)^(^^);
  452.     {
  453.         .^.^.
  454.         (\**funcp)^(^^);
  455.         .^.^.
  456.     }
  457. .sp .7
  458. .eG
  459. .fi
  460. Notice that \fIf\fR was declared
  461. explicitly in the calling routine since its first appearance
  462. was not followed by \fG(\fR^.
  463. .ms
  464. 14.3  Arrays, pointers, and subscripting
  465. .et
  466. Every time an identifier of array type appears
  467. in an expression, it is converted into a pointer
  468. to the first member of the array.
  469. Because of this conversion, arrays are not
  470. lvalues.
  471. By definition, the subscript operator
  472. \fG[^]\fR is interpreted
  473. in such a way that
  474. ``E1[E2]'' is identical to
  475. ``\**(^(^E1)^+^(E2^)^)''.
  476. Because of the conversion rules
  477. which apply to \fG+\fR, if E1 is an array and E2 an integer,
  478. then E1[E2] refers to the E2-th member of E1.
  479. Therefore,
  480. despite its asymmetric
  481. appearance, subscripting is a commutative operation.
  482. .pg
  483. A consistent rule is followed in the case of
  484. multi-dimensional arrays.
  485. If E is an \fIn^\fR-dimensional
  486. array
  487. of rank
  488. .ft I
  489. i^\(mu^j^\(mu^.^.^.^\(muk,
  490. .ft R
  491. then E appearing in an expression is converted to
  492. a pointer to an (\fIn\fR\(mi1)-dimensional
  493. array with rank
  494. .ft I
  495. j^\(mu^.^.^.^\(muk.
  496. .ft R
  497. If the \fG\**\fR operator, either explicitly
  498. or implicitly as a result of subscripting,
  499. is applied to this pointer,
  500. the result is the pointed-to (\fIn\fR\(mi1)-dimensional array,
  501. which itself is immediately converted into a pointer.
  502. .pg
  503. For example, consider
  504. .sp .7
  505. .bG
  506.     int x[3][5];
  507. .sp .7
  508. .eG
  509. Here \fIx\fR is a 3\(mu5 array of integers.
  510. When \fIx\fR appears in an expression, it is converted
  511. to a pointer to (the first of three) 5-membered arrays of integers.
  512. In the expression ``x[^i^]'',
  513. which is equivalent to ``\**(x+i)'',
  514. \fIx\fR is first converted to a pointer as described;
  515. then \fIi\fR is converted to the type of \fIx\fR,
  516. which involves multiplying \fIi\fR by the
  517. length the object to which the pointer points,
  518. namely 5 integer objects.
  519. The results are added and indirection applied to
  520. yield an array (of 5 integers) which in turn is converted to
  521. a pointer to the first of the integers.
  522. If there is another subscript the same argument applies
  523. again; this time the result is an integer.
  524. .pg
  525. It follows from all this that arrays in C are stored
  526. row-wise (last subscript varies fastest)
  527. and that the first subscript in the declaration helps determine
  528. the amount of storage consumed by an array
  529. but plays no other part in subscript calculations.
  530. .ms
  531. 14.4  Labels
  532. .et
  533. Labels do not
  534. have a type of their own; they are
  535. treated
  536. as having type ``array of \fGint\fR''.
  537. Label variables should be declared ``pointer
  538. to \fGint\fR'';
  539. before execution of a \fGgoto\fR referring to the
  540. variable,
  541. a label (or an expression
  542. deriving from a label) should be assigned to the
  543. variable.
  544. .pg
  545. Label variables are a bad idea in general;
  546. the \fGswitch\fR statement makes them
  547. almost always unnecessary.
  548. .ul
  549. 15.  Constant expressions
  550. .et
  551. In several places C requires expressions which evaluate to
  552. a constant:
  553. after
  554. .ft G
  555. case,
  556. .ft R
  557. as array bounds, and in initializers.
  558. In the first two cases, the expression can
  559. involve only integer constants, character constants,
  560. and
  561. .ft G
  562. sizeof
  563. .ft R
  564. expressions, possibly
  565. connected by the binary operators
  566. .tr ^^
  567. .dp
  568.     +   \(mi   \**   /   %   &   \(or   ^   <<   >>
  569. .ed
  570. or by the unary operators
  571. .dp
  572.     \(mi   \*~
  573. .ed
  574. .tr ^\|
  575. Parentheses can be used for grouping,
  576. but not for function calls.
  577. .pg
  578. A bit more latitude is permitted for initializers;
  579. besides constant expressions as discussed above,
  580. one can also apply the unary \fG&\fR
  581. operator to external scalars,
  582. and to external arrays subscripted
  583. with a constant expression.
  584. The unary \fG&\fR can also
  585. be applied implicitly
  586. by appearance of unsubscripted external arrays.
  587. The rule here is that initializers must
  588. evaluate either to a constant or to the address
  589. of an external identifier plus or minus a constant.
  590.