[C Brushing Questions] day2

1. Multiple choice questions

1. The output result of the following program segment is ( )

#include<stdio.h>
int main()
{
char s[] = "\\123456\123456\t";
printf("%d\n", strlen(s));
return 0;
}

A: 12 B: 13 C: 16 D: None of the above
[Answer]:

A

[Analysis]:

Test point: escape characters

\\ means backslash, canceling the effect of escaping

\123 represents octal 123

\t represents a horizontal tab character, equivalent to the Tab key

These are all counted as one character, and the others are all counted individually, so there are 12


2. If there is the following program, the output result after running is ()

#include <stdio.h>
#define N 2
#define M N + 1
#define NUM (M + 1) * M / 2
int main()
{
printf("%d\n", NUM);
return 0;
}

A: 4 B: 8 C: 9 D: 6
[Answer]:

B

[Analysis]:

Test point: define macro definition is just a simple replacement

(1) First replace M to get (N+1+1)*N+1/2 (if M is replaced by N+1, there is no need to add parentheses, just replace it directly)

(2) Then replace N, (2+1+1)*2+1/2 to get 8.5

(3) Print in the form of an integer, and the integer part is 8


3. The value of f(1) of the following function is ( )

int f(int n)
{
static int i = 1;
if(n >= 5)
return n;
n = n + i;
i++;
return f(n);
}

A: 5 B: 6 C: 7 D: 8
[Answer]:

C

[Analysis]:

Test points:

Static modified local variables change the cycle of the variable (let the static local variables still exist out of scope, and the life cycle will not stop until the program ends)


4. Do the following three pieces of program code have the same effect? ​​( )

int b;
(1)const int *a = &b;
(2)int const *a = &b;
(3)int *const a = &b;

A: (2)=(3) B: (1)=(2) C: All different D: All the same
[Answer]:

B

[Analysis]:

Test point: pointer constants and constant pointers

Important difference: the position of *

We use * as the dividing line. If const is on the left side of *, then it is a constant pointer (the pointer points to a constant);

If const is on the right side of *, it is a pointer constant (a pointer is a constant)

1. Constant pointer: The content pointed to by the pointer is a constant (const only exists in the process pointed by the pointer, and has nothing to do with whether the variable itself is modified by const)

const  int *n;

int  const *n;

Note two points :

(1) The meaning of a constant pointer is that the value of variable (b) cannot be changed through pointer dereference , but the value of variable (b) can be changed through other references.

int b=5;
const int *a=&b;
b=6;

(2) The constant pointer can change its pointer, and the constant pointer can point to other addresses.

int a = 5;
int b = 6;
const int* n = &a;
n = &b;

2. Pointer constant: The pointer itself is a constant and cannot point to other addresses.

int * const n;

 Notice:

The address pointed to by a pointer constant cannot be changed , but the value stored in the address can be changed. The data can be modified through other pointers pointing to the changed address.

int a = 5;
int* p = &a;
int* const n = &a;
*p = 8;//通过p指针来改变a的值

5. For the following statement, the correct one is ( )

A: For struct X{short s;int i;char c;}, sizeof(X) is equal to sizeof(s) + sizeof(i) + sizeof(c)

B: For a double variable a, you can use a == 0.0 to determine whether it is zero.

C: The initialization method char a[14] = "Hello, world!"; has the same effect as char a[14]; a = "Hello, world!";

D: None of the above statements are correct

【Answer】:

D

[Analysis]:

A. Failure to consider memory alignment

B. Examine the comparison of floating point types. Since floating point types have errors, it cannot be directly judged whether two numbers are equal. Usually, the absolute value of the difference between two numbers is compared to whether it is less than a very small number, as the error.

C. The second type a represents the address of the first element. The address is a constant and cannot be changed.


2. Programming questions

1. Nicochis theorem

[Reference answer]:

Find the first number to be printed through mathematical rules. The first number is m*m-m+2*i+1

Note that the last number does not need to be printed with +, so special considerations are required.

#include <stdio.h>
int main() 
{
    int m=0;
    int count=0;
    //输入
    scanf("%d",&m);
    //输出
    for(int i=0;i<m;i++)
    {
        if(i!=m-1)
        printf("%d+",m*m-m+2*i+1);
        //最后一个没+
        else
        printf("%d",m*m-m+2*i+1);
    }
    return 0;
}

2. Arithmetic progression

[Reference answer]:

 It’s just a set of mathematical formulas, Sn=a1*n+n*(n-1)*d/2

#include <stdio.h>
int main()
{
   int n=0;
   //输入
   scanf("%d",&n);
   //输出
   printf("%d",2*n+n*(n-1)*3/2);
}

Guess you like

Origin blog.csdn.net/qq_73017178/article/details/132805697