C Language Exercise Class – Day 7

01

C Language Exercise Class - Day 7

Observe the following statement:

char str1[10], str2[10] = {“books”};

The correct statement that can assign the string “books” to the array str1 is

A) str1 = {“Books”};

B) strcpy(str1, str2);

C) str1 = str2;

D) strcpy(str2, str1);

C Language Exercise Class - Day 7

Answer: B

Explanation:

For option A: Correct, using the standard library function to copy the string.

For option B: Incorrect, the array name is a constant pointer and cannot be assigned directly.

For option C: str1 and str2 are the base addresses of the arrays and cannot be modified.

For option D: Incorrect, strcpy(str2, str1) will lead to uninitialized data overwriting str2.

strcpy function

The strcpy function is used for string copying in the C standard library, which copies the source string (including the null terminator \0) to the target character array.

Function prototype:

char *strcpy(char *dest, const char *src);

dest: target character array (must be large enough to hold the source string)

src: source string, can be a character array or string constant

Return value: returns a pointer to the target array

Thought 1: The strcpy function does not check the target size, which can easily lead to overflow; using strncpy or non-standard safe functions can be alternatives.

Thought 2: strcpy must copy the entire string (up to \0); if partial copying is needed, use strncpy or manually loop through each element.

Supplement: strncpy function

Function prototype: char *strncpy(char *dest, const char *src, size_t n);

dest: target character array

src: source character array or string constant

n: maximum number of characters to copy (if n ≤ src length, the null terminator must be added manually)

Return value: returns a pointer to the target array

02

C Language Exercise Class - Day 7

The output result of the following statement is ( )

printf(“%d\n”, strlen(“\tc\065\xff\n”));

A) 5

B) 14

C) 8

D) Output item is invalid, no normal output

C Language Exercise Class - Day 7

Answer: A

Explanation: The strlen function calculates the length of the string until it encounters the \0 terminator, not counting \0.

[Variation question] The output result of the following statement is

printf(“%d\n”, sizeof(“\tc\065\xff\n”));

A) 5

B) 14

C) 6

D) Output item is invalid, no normal output

Answer: C

Explanation: sizeof() is used to calculate the number of bytes occupied by an object and can be used for any data type; it includes \0 in the calculation, while the strlen() function is only for strings and does not include \0 in the calculation.

03

C Language Exercise Class - Day 7

If there are statements int *point, a = 4; and point = &a; the following options represent a group of addresses ( )

A) a, point, *&a

B) &*a, &a, *point

C) *&point, *point, &a

D) &a, &*point, point

C Language Exercise Class - Day 7

Answer: D

Explanation:

1. Variables and addresses

a: integer variable

&a: address of variable a

point: pointer variable, stores the address of a

*point: dereference pointer, gets the value of a

2. Operator meanings

&: address-of operator

*: dereference operator (or used when declaring a pointer)

1. & (address-of operator)

Function: gets the address of a variable in memory

Syntax: &variable_name

Return type: pointer to that variable type

2. * (dereference operator / pointer declaration symbol)

Function: in variable declaration, * indicates that the variable is a pointer; dereference pointer to access the value in the memory address pointed to by the pointer.

3. The relationship between * and &: inverse operations

& gets the address, * gets the value at the address.

04

C Language Exercise Class - Day 7

Assuming the definition: float x; then the following statements defining the pointer variable p and assigning an initial value arecorrect ( )

A) float *p = 1024;

B) int *p = (float)x;

C) float p = &x;

D) float *p = &x;

C Language Exercise Class - Day 7

Answer: D

Explanation:

For option A: Incorrect: directly assigning the integer value 1024 to float* pointer, type mismatch and illegal address.

For option B: Incorrect, (float)x is a float value, cannot be directly assigned to int* pointer.

For option C: Incorrect, p is of float type, cannot store an address (should be declared as float*).

For option D: Correct: p is of float* type, &x is the address of the float variable, type matches.

05

C Language Exercise Class - Day 7

Assuming there are statements: int a = 1, b = 2, *p1 = &a, *p2 = &b; the following statement that can make pointer p1 point to variable b is ( )

A) p1 = *p2

B) *p1 = p2

C) p1 = p2

D) *p1 = *p2

C Language Exercise Class - Day 7

Answer: C

Explanation:

For option A: *p2 is the value of b, while p1 is of int* type, cannot directly assign int value.

For option B: *p1 is the value of a, p2 is of int* type, cannot assign to int.

For option D: Assigning the value of b to a, but p1 still points to a, does not change the pointer’s direction.

06

C Language Exercise Class - Day 7

Observe the following program:

main()

{

int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, *p;

for(p = a; p < a + 10; p++)

printf(“%d,”, *p);

}

The output result of the program is ( )

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

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

C) 0,1,2,3,4,5,6,7,8,9,

D) 1,1,1,1,1,1,1,1,1,1,

C Language Exercise Class - Day 7

Answer: A

Explanation:

Loop count p points to element output value

1 a[0] 1

2 a[1] 2

3 a[2] 3

… … …

10 a[9] 0

07 (Common Mistake Question)

C Language Exercise Class - Day 7

If char s[10] is defined, then in the following expressionscannot represent the address of s[1] ( )

A) s + 1

B) s++

C) &s[0] + 1

D) &s[1]

C Language Exercise Class - Day 7

Answer: B

Explanation:

For option A: The array name s degenerates to a pointer, s + 1 is equivalent to &s[1].

For option B: s++ is a statement rather than an expression, and s is an array name (constant pointer), cannot be modified directly.

For option C: &s[0] is the address of the first element, +1 points to s[1].

For option D: Directly takes the address of s[1].

08

C Language Exercise Class - Day 7

Among the following options, the operationis not valid is ( )

A) int x[6], *p; p = &x[0];

B) int x[6], *p; *p = x;

C) int x[6], *p; p = x;

D) int x[6], p; p = x[0];

C Language Exercise Class - Day 7

Answer: B

Explanation:

For option B: x is an array name (degenerated to int*), cannot be directly assigned to int type

09 (Common Mistake Question)

C Language Exercise Class - Day 7

Among the following statements, thecorrect is ( )

A) The scope of global variables is always larger than that of local variables

B) The lifetime of static variables spans the entire duration of the program

C) The parameters of functions are all global variables

D) Auto variables and static variables that are not initialized in the definition statement have random initial values

C Language Exercise Class - Day 7

Answer: B

Explanation:

For option A: The scope of global variables is from the definition point to the end of the file, while the scope of local variables is limited to the code block they are in (such as functions or compound statements). Although global variables usually have a wider scope, it is not “always” larger (for example, in nested scopes, they may be overshadowed by local variables).

For option B: Static variables (whether global or local) have a lifetime from program startup to termination, but their scope depends on the definition location.

For option C: Parameters are local variables, and their scope is limited to the function.

For option D: Auto variables (default storage class) have initial values that are random; static variables that are not explicitly initialized will automatically be assigned 0 (or null pointer/null character).

10 (Common Mistake Question)

C Language Exercise Class - Day 7

There is the following program:

main()

{ char s[] = “159”, *p;

p = s;

printf(“%c”, *p++);

pritnf(“%c”, *p++);

}

The output result of the program is ( )

A) 15

B) 16

C) 12

D) 59

C Language Exercise Class - Day 7

Answer: A

Explanation: *p++ always dereferences the current value first, then moves the pointer.

Comparing with other notations:

(*p)++: first takes *p, then adds 1 to *p’s value

*++p: first increments p by 1, then gets the value currently pointed to by p

Leave a Comment