📄 c5
字号:
.ul10. External definitions.etA C program consists of a sequence of external definitions.External definitions may be given for functions, forsimple variables, and for arrays.They are used both to declare and to reserve storage forobjects.An external definition declares an identifier tohave storage class \fGextern\fR anda specified type.The type-specifier (\(sc8.2) may be empty, in whichcase the type is taken to be \fGint\fR..ms10.1 External function definitions.etFunction definitions have the form.dp 2.ta .5i 1i 1.5i 2i 2.5i function-definition: type-specifier\*(op function-declarator function-body.edA function declarator is similar to a declaratorfor a ``function returning ...'' except thatit lists the formal parameters ofthe function being defined..dp 2 function-declarator: declarator \fG( \fIparameter-list\*(op \fG).ft I parameter-list: identifier identifier \fG,\fI parameter-list.edThe function-bodyhas the form.dp 1 function-body: type-decl-list function-statement.edThe purpose of thetype-decl-list is to give the types of the formal parameters.No other identifiers should be declared in this list, and formalparameters should be declared only here..pgThe function-statementis just a compoundstatement which may havedeclarations at the start..dp 2 function-statement: { declaration-list\*(op statement-list }.edA simple example of a complete function definition is.sp .7.ne 7.nf.bG int max^(^a, b, c) int a, b, c; { int m; m = ^(^a^>^b^)? a^:^b^; return^(^m^>^c? m^:^c^)^; }.sp .7.eG.fiHere ``int'' is the type-specifier;``max(a,@b,@c)'' is the function-declarator;``int@a,@b,@c;'' is the type-decl-list forthe formalparameters;``{@.^.^.@}'' is the function-statement..pgC converts all\fGfloat\fR actual parametersto \fGdouble\fR, so formal parameters declared \fGfloat\fRhave their declaration adjusted to read \fGdouble\fR.Also, since a reference to an array in any context(in particular as an actual parameter)is taken to meana pointer to the first element of the array,declarations of formal parameters declared ``array of ...''are adjusted to read ``pointer to ...''.Finally, because neither structuresnor functions can be passedto a function, it is useless to declarea formal parameter to be a structure orfunction (pointers to structures orfunctions are of course permitted)..pgA free \fGreturn\fR statement is supplied at theend of each function definition, sorunning off the endcausescontrol, but no value, to be returned tothe caller..ms10.2 External data definitions.etAn external data definition has the form.dp 2 data-definition: \fGextern\*(op \fItype-specifier\*(op init-declarator-list\*(op \fG;.edThe optional.ft Gextern.ft Rspecifier is discussed in \(sc 11.2.If given, the init-declarator-list is acomma-separated list of declarators each of which may befollowed by an initializer for the declarator..dp 2 init-declarator-list: init-declarator init-declarator \fG, \fIinit-declarator-list.ed.dp 2 init-declarator: declarator initializer\*(op.edEach initializer represents the initial value for thecorresponding objectbeing defined (and declared)..dp 5 initializer: constant { constant-expression-list }.ed.dp 3 constant-expression-list: constant-expression constant-expression \fG,\fI constant-expression-list.edThus an initializer consists ofa constant-valued expression, orcomma-separated list of expressions,inside braces.The braces may be droppedwhen the expression is just a plainconstant.The exact meaning of a constant expression is discussedin \(sc15.The expression list is used to initialize arrays;see below..pgThe type of the identifier beingdefinedshould be compatiblewith the type of the initializer:a \fGdouble\fR constantmay initialize a \fGfloat\fR or \fGdouble\fR identifier;a non-floating-point expressionmayinitialize an \fGint\fR, \fGchar\fR, or pointer..pgAn initializer for an array may containa comma-separated list of compile-time expressions.The length of the array is taken to be themaximum of the number of expressions in the listand the square-bracketed constant in thearray's declarator.This constant may be missing,in which case 1 is used.The expressions initialize successive members of the arraystarting at the origin (subscript 0)of the array.The acceptable expressions for an array of type``array of ...'' are the same asthose for type ``...''.As a special case, a singlestring may be given as the initializerfor an array of \fGchar\fRs; in this case,the characters in the string are takenas the initializing values..pgStructures can be initialized, but this operationis incompletely implemented and machine-dependent.Basically the structure is regarded as a sequenceof words and the initializers are placed into thosewords.Structure initialization,using a comma-separated list in braces,is safe if all the members of the structureare integers or pointers but is otherwise ill-advised..pgThe initial value of any externally-definedobject not explicitly initialized is guaranteedto be 0..ul11. Scope rules.etA complete C program need not allbe compiled at the same time: the source text of theprogrammay be kept in several files, and precompiledroutines may be loaded fromlibraries.Communication among the functions of a programmay be carried out both through explicit callsand through manipulation of external data..pgTherefore, there are two kinds of scope to consider:first, what may be called the \fIlexical scope\fRof an identifier, which is essentially theregion of a program during which it maybe used without drawing ``undefined identifier''diagnostics;and second, the scopeassociated with external identifiers,which is characterized by the rulethat references to the same externalidentifier are references to the same object..ms11.1 Lexical scope.etC is not a block-structured language;this may fairly be considered a defect.The lexical scope of names declared in external definitionsextends from their definition throughthe end of the filein which they appear.The lexical scope of names declared at the head of functions(either as formal parametersor in the declarations heading thestatements constituting the function itself)is the body of the function..pgIt is an error to redeclare identifiers alreadydeclared in the current context,unless the new declaration specifies the same typeand storage class as already possessed by the identifiers..ms11.2 Scope of externals.etIf a function declares an identifier to be\fGextern\fR,then somewhere among the files or librariesconstituting the complete programthere must be an external definitionfor the identifier.All functions in a given program which refer to the sameexternal identifier refer to the same object,so care must be taken that the type and extentspecified in the definitionare compatible with those specifiedby each function which references the data..pgIn \s8PDP\s10-11 C,it is explicitly permitted for (compatible)external definitions of the same identifierto be present in several of theseparately-compiled pieces of a complete program,or even twice within the same program file,with the important limitation that the identifiermay be initialized in at most one of thedefinitions.In other operating systems, however, the compiler must knowin just which file the storagefor the identifier is allocated, and in which filethe identifier is merely being referred to.In the implementations of C for such systems,the appearance of the.ft Gextern.ft Rkeyword before an external definitionindicates that storage for the identifiersbeing declaredwill be allocated in another file.Thus in a multi-file program,an external data definition withoutthe.ft Gextern.ft Rspecifier must appear in exactly one of the files.Any other files which wish to give an external definitionfor the identifier mustinclude the.ft Gextern.ft Rin the definition.The identifier can be initialized only in the filewhere storage is allocated..pgIn \s8PDP\s10-11 C none of this nonsense is necessaryand the.ft Gextern.ft R
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -