|
|
HP C
|
Previous | Contents | Index |
Prototypes must be placed appropriately in each compilation unit of a program. The position of the prototype determines its scope. A function prototype, like any function declaration, is considered within the scope of a corresponding function call only if the prototype is specified within the same block as the function call, any enclosing block, or at the outermost level of the source file. The compiler checks all function definitions, declarations, and calls from the position of the prototype to the end of its scope. If you misplace the prototype so that a function definition, declaration, or call occurs outside the scope of the prototype, any calls to that function behave as if there were no prototype.
The syntax of the function prototype is designed so that you can extract the function header of each of your function definitions, add a semicolon (;), place the prototypes in a header, and include that header at the top of each compilation unit in your program. In this way, function prototypes are declared to be external, extending the scope of the prototype throughout the entire compilation unit. To use prototype checking for C library function calls, place the #include preprocessor directives for the .h files appropriate for the library functions used in the program.
It is an error if the number of arguments in a function definition, declaration, or call does not match the prototype.
If the data type of an argument in a function call does not match the corresponding type in the function prototype, the compiler tries to perform conversions. If the mismatched argument is assignment-compatible with the prototype parameter, the compiler converts the argument to the data type specified in the prototype, according to the argument conversion rules (see Section 5.6.1).
If the mismatched argument is not assignment-compatible with the prototype parameter, an error message is issued.
C functions exchange information by means of parameters and arguments. The term parameter refers to any declaration within the parentheses following the function name in a function declaration or definition; the term argument refers to any expression within the parentheses of a function call.
The following rules apply to parameters and arguments of C functions:
In a function call, the types of the evaluated arguments must match the types of their corresponding parameters. If they do not match, the following conversions are performed in a manner that depends on whether a prototype is in scope for the function:
void f(char, short, float, ...); char c1, c2; short s1,s2; float f1,f2; f(c1, s1, f1, c2, s2, f2); |
No other default conversions are performed on arguments. If a particular argument must be converted to match the type of the corresponding parameter, use the cast operator. For more information about the cast operator, see Section 6.4.6.
Function and array identifiers can be specified as arguments to a function. Function identifiers are specified without parentheses, and array identifiers are specified without brackets. When so specified, the function or array identifier is evaluated as the address of that function or array. Also, the function must be declared or defined, even if its return value is an integer. Example 5-1 shows how and when to declare functions passed as arguments, and how to pass them.
Example 5-1 Declaring Functions Passed as Arguments |
---|
(1)int x() { return 25; } /* Function definition and */ int z[10]; /* array defined before use */ (2)fn(int f1(), int (*f2)(), int a1[])) /* Function definition */ { f1(); /* Call to function f1 */ . . . } void caller(void) { (3) int y(); /* Function declaration */ . . . (4) fn(x, y, z); /* Function call: functions */ /* x and y, and array z */ /* passed as addresses */ . . . } int y(void) { return 30; } /* Function definition */ |
Key to Example 5-1:
fn(int f1(), int f2(), int a1[]) /* f1, f2 declared as */ {...} /* functions; a1 declared */ /* as array of int. */ fn(int (*f1)(), int (*f2)(), int *a1) /* f1, f2 declared as */ {...} /* pointers to functions; */ /* a1 declared as pointer */ /* to int. */ |
The function called at program startup is named main . The main function can be defined with no parameters or with two parameters (for passing command-line arguments to a program when it begins executing). The two parameters are referred to here as argc and argv, though any names can be used because they are local to the function in which they are declared. A main function has the following syntax:
int main(void) {...} |
int main(int argc, char *argv[ ]) {...}) |
argc
The number of arguments in the command line that invoked the program. The value of argc is nonnegative.argv
Pointer to an array of character strings that contain the arguments, one per string. The value argv[argc] is a null pointer.
If the value of argc is greater than zero, the array members argv[0] through argv[argc - 1] inclusive contain pointers to strings, which are given implementation-defined values by the host environment before program startup. The intent is to supply the program with information determined before program startup from elsewhere in the host environment. If the host environment cannot supply strings with letters in both uppercase and lowercase, the host environment ensures that the strings are received in lowercase.
If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] is the null character if the program name is not available from the host environment. If the value of argc is greater than one, the strings pointed to by argv[1] through argv[argc - 1] represent the program parameters.
The parameters argc and argv, and the strings pointed to by the argv array, can be modified by the program and keep their last-stored values between program startup and program termination.
In the main function definition, parameters are optional. However, only the parameters that are defined can be accessed.
See your platform-specific HP C documentation for more information on the passing and return of arguments to the main function.
An expression is any sequence of C operators and operands that produces a value or generates a side effect. The simplest expressions are constants and variable names, which yield values directly. Other expressions combine operators and subexpressions to produce values. An expression has a type as well as a value.
Except where noted in this chapter, the order of evaluation of subexpressions, and the order in which side effects take place, is unspecified. Code that depends on such order might produce unexpected results.
The operands of expressions must have compatible type. In some instances, the compiler makes conversions to force the data types of the operands to be compatible.
The following sections discuss these topics:
Simple expressions are called primary expressions; they denote values. Primary expressions include previously declared identifiers, constants, string literals, and parenthesized expressions.
Primary expressions have the following syntax:
primary-expression:
identifier |
The following sections describe the primary expressions.
An identifier is a primary expression provided it is declared as designating an object or a function.
An identifier that designates an object is an lvalue if its type is arithmetic, structure, union, or pointer. The name of an array evaluates to the address of the first element of the array; an array name is an lvalue but not a modifiable lvalue.
An identifier that designates a function is called a function designator. A function designator evaluates to the address of the function.
A constant is a primary expression. Its type depends on its form (integer, character, floating, or enumeration); see Section 1.9. A constant is never an lvalue.
A string literal is a primary expression. Its type depends on its form (character or wchar_t ); see Section 1.9. A string literal is an lvalue.
An expression within parentheses has the same type and value as the expression without parentheses would have. Any expression can be delimited by parentheses to change the precedence of its operators.
Variables and constants can be used in conjunction with C operators to create more complex expressions. Table 6-1 presents the set of C operators.
The C operators fall into the following categories:
Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator:
x = 7 + 3 * 2; /* x is assigned 13, not 20 */ |
The previous statement is equivalent to the following:
x = 7 + ( 3 * 2 ); |
Using parenthesis in an expression alters the default precedence. For example:
x = (7 + 3) * 2; /* (7 + 3) is evaluated first */ |
In an unparenthesized expression, operators of higher precedence are evaluated before those of lower precedence. Consider the following expression:
A+B*C |
The identifiers B and C are multiplied first because the multiplication operator (*) has higher precedence than the addition operator (+).
Previous | Next | Contents | Index |
|