C Language Exercise Class – Day 9

01

C Language Exercise Class - Day 9

The number of actual parameters in the function call statement func is

func(f2(v1, v2), (v3, v4, v5), (v6, max(v7, v8)));

A) 3

B) 4

C) 5

D) 8

C Language Exercise Class - Day 9

Answer: A

Analysis:

The first actual parameter is f2(v1, v2), which is the result of a function call.

The second actual parameter is (v3, v4, v5), which is a comma expression whose value is the value of the last expression v5. Note that the values of the involved variables may change dynamically.

The third actual parameter is (v6, max(v7, v8)), which is also a comma expression whose value is the value of max(v7, v8).

02 (Common Mistake)

C Language Exercise Class - Day 9

Read the following program:

fun(int x, int y)

{ static int m=0, i=2;

i+=m+1;

m=i+x+y;

return m;

}

main()

{ int j=1, m=1, k;

k=fun(j,m);

printf(“%d,”,k);

k=fun(j,m);

printf(“%d\n”,k);

}

The output result after execution is

A) 5, 5

B) 5, 11

C) 11, 11

D) 11, 5

C Language Exercise Class - Day 9

Answer: B

Analysis:

Inside the fun function: static int m=0, i=2;The static variables m and i are initialized only once, and subsequent calls will retain the last modified values..

In the main function: j=1, m=1, when calling fun(j,m), x=1, y=1

First call fun(j, m)

Initial values: x=1, y=1

Static variables m=0, i=2

Execute i += m + 1

m + 1 = 0 + 1 = 1

i = 2 + 1 = 3

Execute m = i + x + y:

m = 3 + 1 + 1 = 5

Return value: 5

Second call fun(j,m)

Static variables retain the last values: m=5, i=3

(Note: here m is the static variable of fun, not the local variable m of main)

Execute i += m + 1

m + 1 = 5 + 1 = 6

i = 3 + 6 = 9

Execute m = i + x + y

m = 9 + 1 + 1 = 11

Return value: 11

In C language, the static keyword is used to modify variables or functions, and its effect depends on the context of use.

Static modifies local variables:

The lifecycle of static local variables starts from the first call of the function until the program ends (not destroyed after the function call ends); initialized only once (at the first call),subsequent calls will retain the last modified values;the scope is still limited to the function that defines the static variable, and cannot be accessed outside the function.

Static modifies global variables:

Limits the scope, making the defined global variable only visible in the current source file, and other files cannot access it through extern; avoids naming conflicts, suitable for multi-file programming, preventing global variables from being accidentally modified.

Static modifies functions:

Limits the scope, making the function only visible within the current source file, and other files cannot call it.

03 (Common Mistake)

C Language Exercise Class - Day 9

The return value type of the following function is

fun ( float x )

{ float y;

y= 5*x-8;

return y;

}

A) int

B) Uncertain

C) void

D) float

C Language Exercise Class - Day 9

Answer: A

Analysis:

In C language, if the function does not explicitly declare the return value type, the default return type isint (this is stipulated by the C89/C90 standard, while C99 and later standards require explicit declaration of return value type, otherwise it will report an error)

If the function defaults to returning int but actually returns float, the compiler will attempt an implicit type conversion, but this is an unsafe operation and may trigger warnings.

04

C Language Exercise Class - Day 9

The output result of the following program is

fun(int x, int y, int z)

{

z=x*x+y*y;

}

main()

{ int a=31;

fun(5,2,a);

printf(“%d”,a);

}

A) 0

B) 2

C) 31

D) Undefined value

C Language Exercise Class - Day 9

Answer: C

Analysis:

Function parameter passing method

In C language, function parameters are passed by default asvalue passing, meaningmodifications to parameters inside the function do not affect external variables.

In fun(5, 2, a), the value 31 of a is copied to z, and the modification inside the function affects z, not a.

The behavior of the fun function: z = x * x + y * y calculates 5² + 2² = 29, but z is a local variable and does not affect a in main; the value of a remains 31, and the call to fun does not change its value.

05

C Language Exercise Class - Day 9

Read the following program:

void f(int x,int y)

{ int t;

if(x<y)

{

t=x;

x=y;

y=t;

}

}

main()

{ int a=4,b=3,c=5;

f(a,b);

f(a,c);

f(b,c);

printf(“%d,%d,%d\n”,a,b,c);

}

The output after execution is ( )

A) 3,4,5

B) 5,3,4

C) 5,4,3

D) 4,3,5

C Language Exercise Class - Day 9

Answer: D

Analysis:

The function f’s effect

If x < y, then swap the values of x and y; but the function parameters are passed by value, meaning modifications to x and y inside the function do not affect the external variables.

Since the f function cannot modify the values of a, b, and c, they always retain their initial values: a=4, b=3, c=5.

[Variant Question]

Read the following program:

void f(int *x,int *y)

{ int t

if(*x<*y)

{

t=*x;

*x=*y;

*y=t;

}

}

main()

{ int a=4,b=3,c=5;

f(&a,&b);// no swap

f(&a,&c);// swap

f(&b,&c);// swap

printf(“%d,%d,%d\n”,a,b,c);

}

The output after execution is ( )

A) 3,4,5

B) 5,3,4

C) 5,4,3

D) 4,3,5

Answer: C

06

C Language Exercise Class - Day 9

When calling a function, if the actual parameter is a simple variable, the data passing method between it and the corresponding formal parameter is ( )

A) Address passing

B) Unidirectional value passing

C) Passed from actual parameter to formal parameter, then from formal parameter back to actual parameter

D) Passing method specified by the user

C Language Exercise Class - Day 9

Answer: B

Analysis:

When the actual parameter is a simple variable, belonging to a basic data type. C language defaults to value passing, meaningthe value of the actual parameter is copied to the formal parameter,modifications to the formal parameter do not affect the actual parameter.

In C language, there are mainly two methods for passing function parameters:Value passing and Address passing.

Value passing: Copies the value of the actual parameter to the formal parameter, modifications to the formal parameter inside the function do not affect the actual parameter, suitable forbasic data types

Address passing: Passes the address of the actual parameter to the formal parameter (pointer), allowing the function to modify the value of the actual parameter through the pointer, suitable forarrays,structures or cases where the actual parameter needs to be modified.

07

C Language Exercise Class - Day 9

There is the following program

void f(int b[])

{ int i;

for (i=2; i<6; i++)

b[i] *= 2;

}

main()

{ int a[10]={1,2,3,4,5,6,7,8,9,10}, i;

f(a);

for (i=0; i<10; i++)

printf(“%d,”, a[i]);

}

The output result after the program runs is ( )

A) 1,2,3,4,5,6,7,8,9,10,

B) 1,2,6,8,10,12,7,8,9,10,

C) 1,2,3,4,10,12,14,16,9,10,

D) 1,2,6,8,10,12,14,16,9,10,

C Language Exercise Class - Day 9

Answer: B

Analysis:

When an array is passed as a function parameter

In C language, when an array is passed as a parameter, theaddress of the array is passed (i.e., address passing), so modifications to the array inside the function will affect the original array.

f(a) is equivalent to f(&a[0]), and the b[] in function f actually operates on the array a in main.

Function f modifies the elements of the array from index 2 to 5 (a[2]~a[5]) by performing *2 operations:

a[2]=6, a[3]=8, a[4]=10, a[5]=12

08

C Language Exercise Class - Day 9

In C language, the type of the function return value ultimately depends on

A) The function type specified in the function header at the time of function definition

B) The type of the expression value in the return statement

C) The type of the actual parameter passed by the calling function when calling the function

D) The type of the formal parameter at the time of function definition

C Language Exercise Class - Day 9

Answer: A

Analysis:

For option A: The return value type explicitly specified in the function header determines the final return value type; if the return type is not specified (which traditional C allows, but modern C standards prohibit), the default return type isint.

For option B: If the type of the expression in the return statement does not match the return type declared in the function, the compiler will attempt an implicit type conversion;but the final return value type is still based on the declaration at the time of function definition.

For options C/D: The types of actual parameters and formal parameters are irrelevant to the return value type.

09 (Common Mistake)

C Language Exercise Class - Day 9

(Multiple Choice) When calling a function, if there is no return statement in that function, which of the following statementsis correct:

A) The function may have no return value

B) The function may return several system default values

C) The function may return a value that the user expects

D) The function may return an uncertain value

C Language Exercise Class - Day 9

Answer: AD

Analysis:

For option A: Correct, applicable to void functions (explicitly not returning a value)

For option B: Incorrect, C language does not have the concept of “system default return value”.

For option C: Incorrect, without a return statement, the return value is completely uncontrollable.

For option D: Applicable to non-void functions,undefined behavior, may return random values

10

C Language Exercise Class - Day 9

Among the following calling statements, theincorrect function header of fun is

main()

{ …

int a[50],n;

fun(n, &a[9]);

}

A) void fun(int m, int x[])

B) void fun(int s, int h[41])

C) void fun(int p, int *s)

D) void fun(int n, int A)

C Language Exercise Class - Day 9

Answer: D

Analysis:

For option A: int m matches n; int x[] can accept &a[9] (the array name degrades to a pointer, int* type)

For option B: int s matches n; int h[41] can accept &a[9] (array parameter degrades to pointer, int* type)

For option C: int p matches n; int *s directly matches &a[9] (int* type)

For option D: int n matches n; int A cannot accept &a[9] (int* type cannot be assigned to int type)

Leave a Comment