[C Brushing Questions] day1

1. Multiple choice questions

1. The correct output is

int x=5,y=7;
void swap()
{
int z;
z=x;
x=y;
y=z;
} 
int main()
{
int x=3,y=8;
swap();
printf("%d,%d\n",x, y);
return 0;
}

【Answer】:

3,8

[Analysis] :

Test points:

(1) Local variables are only used in the area where the variable is defined. Local variables defined in the main function can only be used in the main function.

(2) When a local variable and a global variable have the same name, the local variable takes precedence.

The swap() function exchanges the x and y of the global variable, that is, the global variable x=5, y=7 is exchanged for x=7, y=5

You can print x and y in the swap function, and the result is x=7, y=5

In the output statement, consider local variables first


2. The following incorrect definition statement is ()

A: double x[5] = {2.0, 4.0, 6.0, 8.0, 10.0};

B: char c2[] = {'\x10', '\xa', '\8'};

C: char c1[] = {'1','2','3','4','5'};

D: int y[5+3]={0, 1, 3, 5, 7, 9};

【Answer】:

B

[Analysis]:

Test points:

\ddd: ddd represents 1-3 octal numbers (not necessarily three d, it can be one or two d)

\xdd:dd means two hexadecimal numbers

B. \8 is in the form of \ddd, but there is no 8 in octal

D. The [ ] after the array name must be an integer constant or an integer expression.


3. The test.c file includes the following statement. Among the four variables defined in the file, the variable that is a pointer type is [Multiple Choice] ()

#define INT_PTR int*
typedef int* int_ptr;
INT_PTR a, b;
int_ptr c, d;

A.a                                  B.b                                      C.c                                             D.d

【Answer】:

ACD

[Analysis]:

Test point: The difference between define and typedef

define macro definition is just a simple replacement

typedef defines an alias for the type, and the alias is an independent type

Let's understand an important point: 

int *p;
//
用int这个单类型和表达式“*p”来定义变量p
直接的意思是*p是int类型,因此倒推p是int的指针//

(1) The above define is directly replaced, and the result is int *a, b (write this * as far to the right as possible), then *a and b are both int types, that is, a is int * type, and b is int type.

(2) The following typedef renaming will be equivalent to redefining an int * type, int * a, b, that is, a and b are both int * types.


4. If the conditional expression (M)?(a++):(a--) is given, then the expression M ( )

A: Equivalent to (M==0) B: Equivalent to (M==1) C: Equivalent to (M!=0) D: Equivalent to (M!=1)

【Answer】 :

C

[Analysis]:

Test points: In C language, 0 means false, and non-zero means true

First determine whether expression 1, that is, M is true, if M is true, then a++, if M is false, then a--

That is, if M is 0, then a--; if M is non-0, then a++, which corresponds to option C.

5. If there is the following definition statement, the correct input statement is [Multiple Choice] ( )

int b;
char c[10];

A: scanf("%d%s",&b,&c); B: scanf("%d%s",&b,c); C: scanf("%d%s",b,c); D: scanf("%d%s",b,&c);


【Answer】:

AB

[Analysis]:

Test site: when does scanf need &

For those that cannot represent address information themselves, add &; for those that represent address information themselves, there is no need to add &.

Note: The two addresses of &c and c are the same here , and the effect of the program is the same, but for int b it must be &b


2. Programming questions

1. Print the n digits from 1 to the maximum

 【Answer】:

 * @param n int整型 最大位数
 * @return int整型一维数组
 * @return int* returnSize 返回数组行数
 */
#include<math.h>
int* printNumbers(int n, int* returnSize ) 
{
    //返回元素的个数
    *returnSize=pow(10,n)-1;
    //动态分配空间
    int *arr=(int*)malloc(*returnSize*sizeof(int));
    //赋值
    for(int i=0;i<*returnSize;i++)
    {
        arr[i]=i+1;
    }
    //返回
    return arr;
}

2. Calculate date to day conversion

【Answer】:

#include <stdio.h>

int main() 
{
    int year,month_real,day=0;
    int month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
    //输入年月日
    scanf("%d %d %d",&year,&month_real,&day);
    int date=day;
    for(int i=0;i<month_real-1;i++)
    {
        date+=month[i];
    }
    //闰年并且超过二月
    if((year%4==0&&year%100!=0)||(year%400==0)&&month_real>2)
    {
        date+=1;
    }
    printf("%d",date);
    return 0;
}

Guess you like

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