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:
int
.
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);
The arguments c1
, s1
, and
f1
are passed with their respective types, while the
arguments c2
, s2
, and f2
are converted to int
, int
, and
double
, respectively.
float
are converted
to double
.
char
,
unsigned char
, short
, or
unsigned short
are converted to int
.
unsigned char
or unsigned short
to
unsigned int
. 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.
int x() { return 25; } /* Function definition and */ int z[10]; /* array defined before use */ fn(int f1(), int (*f2)(), int a1[])) /* Function definition */ { f1(); /* Call to function f1 */ . . . } void caller(void) { int y(); /* Function declaration */ . . . 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:
x
can be passed in an argument list because its
definition, located before the function caller
,
serves as its declaration.
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. */
When such parameters are declared as functions or arrays, the compiler automatically converts the corresponding arguments to pointers.
caller
, function y
must be
declared before passing it in an argument list.
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[ ]) { . . . })
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 DEC C
documentation for more information on the passing and return of
arguments to the main
function.