C Language Exercise Class – Day 13

01

C Language Exercise Class - Day 13

The followingcannot be defined as a user identifier:

A) man

B) _0

C) _int

D) sizeof

C Language Exercise Class - Day 13

Answer: D

Explanation:

Among the given options, sizeof is a keyword in C language used to calculate the number of bytes occupied by a data type or variable. Since keywords cannot be used as user identifiers,

The naming rules for C language identifiers are:

1. Composed of valid characters

Can only contain the following characters:

Letters A-Z, a-z

Digits 0-9

Underscore _

[Note] Cannot start with a digit

2. Case sensitive

C language is strictly case-sensitive

3. Cannot use keywords

Keywords in C language cannot be used as identifiers

4. Length limit

Standard C specifies that the length of identifiers must support at least 31 characters (C99 standard), but the specific length may vary by compiler.

02 (Common Mistake)

C Language Exercise Class - Day 13

Given that i, j, k are int type variables, if the input from the keyboard is: 1,2,3<Enter>, making i’s value 1, j’s value 2, and k’s value 3, which of the following input statements iscorrect?

A) scanf(“%2d%2d%2d”,&i,&j,&k);

B) scanf(“%d %d %d”,&i,&j,&k);

C) scanf(“%d,%d,%d”,&i,&j,&k);

D) scanf(“i=%d,j=%d,k=%d”,&i,&j,&k);

C Language Exercise Class - Day 13

Answer: C

Explanation:

For option A: %2d indicates reading a 2-digit integer, but the input 1,2,3 consists of single-digit numbers and has no spaces.

For option B: %d %d %d requires input to be space-separated (like 1 2 3), but the input is 1,2,3 (comma-separated).

For option C: “%d,%d,%d” strictly matches the input format of 1,2,3 (comma-separated).

For option D: It requires the input format to be i=1,j=2,k=3, but the input is 1,2,3 (without prefixes like i=, j=, etc.).

03

C Language Exercise Class - Day 13

In the following program segment, the number of “*” output is

char *s= “\037\tcac”;

for( *s !=’\0′; s++) printf(” * “);

A) 9

B) 5

C) 6

D) 7

C Language Exercise Class - Day 13

Answer: B

Explanation:

The actual memory representation of the string is:

\037 (1 byte)

\t (1 byte)

c (1 byte)

a (1 byte)

c (1 byte)

\0 (null terminator, not counted)

Total length = 5 valid characters

04 (Common Mistake)

C Language Exercise Class - Day 13

Read the following program:

point(char *p) {p+=3;}

main()

{ char b[4]={‘a’,’b’,’c’,’d’}, *p=b;

point(p);

printf(“%c\n”,*p);

}

The output of the program is

A) a

B) b

C) c

D) d

C Language Exercise Class - Day 13

Answer: A

Explanation:

The function internally moves the pointer p 3 positions forward (p now points to b[3], which is ‘d’); but the key point is that the function parameter is passed by value, so the modifications inside the function do not affect the external p.

The final pointer direction: after the function call, the external p still points to b[0] (because the modification inside the function does not affect the external p).

Passing pointer by value

Characteristics: The function receives a copy of the pointer (i.e., the value of the pointer variable, which is a memory address). The function can modify the data pointed to by the pointer, but cannot modify the pointer itself (i.e., cannot make the external pointer point to a new address). Passing pointer value *p can only modify data, not change the pointer.

Passing pointer by address

Characteristics: The function receives the address of the pointer (i.e., int **p, a pointer to a pointer). The function can modify the data pointed to by the pointer and can also modify the external pointer itself (making it point to a new address). Passing pointer address **p can modify data and change the pointer.

05

C Language Exercise Class - Day 13

Let int x=11; then the value of the expression (x++ * 1/3) is

A) 3

B) 4

C) 11

D) 12

C Language Exercise Class - Day 13

Answer: A

Explanation: Omitted

06

C Language Exercise Class - Day 13

The following options cancorrectly define a one-dimensional array:

A) int a[5]={0,1,2,3,4,5};

B) char a[]={0,1,2,3,4,5};

C) char a={‘A’,’B’,’C’};

D) int a[5]=”0123″;

C Language Exercise Class - Day 13

Answer: B

Explanation:

For option A: The array size is 5, but the initialization list has 6 elements, exceeding the array capacity.

For option B: Omitting the array size, the compiler will automatically infer the size as 6 based on the initialization list.

For option C: a is declared as a single char variable, but attempts to initialize multiple characters with {}. This is a syntax error (a single char variable cannot be initialized with a list).

For option D: “0123” is a string (char[] type) and cannot be directly assigned to an int array.

07

C Language Exercise Class - Day 13

Read the following program segment:

int x,y;

x=13;

y=5;

printf(“%d”,x%=(y/=2));

The output of this program segment is

A) 3

B) 2

C) 1

D) 0

C Language Exercise Class - Day 13

Answer: C

Explanation: Omitted

08

C Language Exercise Class - Day 13

After executing the following program, the value of sum is

main()

{ int i , sum;

for(i=1;i<6;i++) sum+=i;

printf(“%d\n”,sum);}

A) 15

B) 14

C) Uncertain

D) 0

C Language Exercise Class - Day 13

Answer: C

Explanation:

The variable sum is uninitialized, its value is uncertain. It is best to assign an initial value when defining variables to develop good programming habits.

09

C Language Exercise Class - Day 13

Among the following statements, the one that iscorrect is

A) Input items can be a real number, e.g., scanf(“%f”,3.5);

B) Input can be correctly done without input items, e.g., scanf(“a=%d,b=%d”);

C) When inputting a real number, the format control part can specify the number of decimal places, e.g., scanf(“%4.2f”,&f);

D) When inputting data, the variable address must be specified, e.g., scanf(“%f”,&f);

C Language Exercise Class - Day 13

Answer: D

Explanation:

For option A: The second parameter of scanf must be the address of a variable, using & to get the address.

For option B: scanf requires the variable address to store the input data, and no variable address is provided here.

For option C: The format specifier of scanf cannot specify precision (like %4.2f).

For option D: scanf must receive the address of a variable (&f) to correctly store the input value.

Introduction to the scanf function

The scanf function is used in C language to read data from standard input (such as keyboard), belonging to the standard input-output library <stdio.h>.

Its core function is to parse input data according to the specified format and store the results in variables. It is necessary to pass the variable address (&a), otherwise it will lead to undefined behavior. By default, data is separated by spaces, tabs, or newlines during input.

Advanced usage of scanf function

Specify input width:

To prevent buffer overflow

For example, scanf(“%9s”,str);

Skip certain inputs:

%*d indicates reading but not storing that number

For example, scanf(“%d%*d%d”,&a,&b);

Match specific characters

For example, scanf(“%d/%d”,&a,&b);

10

C Language Exercise Class - Day 13

Read the following program segment:

main()

{

char a1=’M’,a2=’m’;

printf(“%c\n”,(a1, a2));

}

Among the following statements, the one that iscorrect is

A) The program outputs the uppercase letter M

B) The program outputs the lowercase letter m

C) Insufficient format specifier, compilation error

D) The program generates an error message at runtime

C Language Exercise Class - Day 13

Answer: B

Explanation:

Comma expression:

Form: (expression1, expression2, …, expressionn).

Rule: Calculated from left to right, the final result is the value of the last expression.

Note: The expressions in parentheses are calculated in order, and the values of the involved variables are dynamically updated.

Leave a Comment