C language exercises Day1

Insert image description here
Starting today, I will share C language exercises, every day, for almost 16 days. After reading this, my understanding of C language may be further improved, so let’s start sharing today!

  • Question 1

Execute the following code, the output result 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;
}

Analysis: First we can see that x and y are global variables. Then in the main function, we define x and y. When we encounter a swap function, we see that in the function it wants to exchange our x and y, but we know , the formal parameter is a temporary copy of the actual parameter. Changing the formal parameter will not change the actual parameter, so we cannot play the role of exchange here, but is our output the answer to the global variable, or the xy of the local variable? , the answer is the answer of local variables. In the stack frame of the main function, we create local variables. Then the local variables are used in this stack frame. Because the swap function does not perform real exchange, the answer is 3 and 8.

  • Question 2

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};

The answer is B. The reason is that our option B is a character array, and the content in it should be characters. It contains hexadecimal numbers. What does it mean?

  • Question three

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
Here we just look at it from top to bottom. First of all, #define means definition. The following INT_PTR means int*, which means it is a pointer, so a and b is a pointer, pointing to data of type int. Then we come to our typedef. In fact, the code behind it means int int d, so the answer is ABC.

  • Question 4

4. If the conditional expression (M)?(a++):(a–) is given, then the expression M ( )
A: and (M0) Equivalent to B: and (M1) Equivalent to C: Equivalent to (M!=0) D: Equivalent to (M!=1)

This test is a ternary operator. As long as we understand that M is true, we will execute (a++), otherwise it will be a –
so the answer to this question is C

  • Topic 5

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);
The answer is AB
analysis: our b is an int type variable, so to get its address, we need to add the & operator

The array name is the address of the first element, so we don't need to take the address. If we add it, it will be the address of the entire array.

Programming question:
Input the number n and print out the n decimal numbers from 1 to the largest in order. For example, if you enter 3, 1, 2, 3 will be printed out until the maximum 3-digit
number is 999.
Use return a list of integers instead of printing
n as a positive integer
OJ question [Niuke.com question number: JZ17 Print n digits from 1 to the maximum]

static int a[100010];
int* printNumbers(int n, int* returnSize )
{
    
    
    int k=(int)pow(10,n);
    int i=0;
    for(i=1;i<k;i++)
    {
    
    
        a[i-1]=i;
    }
    *returnSize=--i;
    return a;
}


2. Calculate the day of the year based on the entered date. Make sure the year is 4 digits and the date is valid.
Input description: Enter one line, each line separated by spaces, which are year, month, and day.
Output description: The output is the day of the year. Add link description to
OJ link [Niuke.com question number: HJ73 Calculate date to day conversion] [Difficulty: Easy] int b; char c[10]; Example: Input: 1 Return value: [1,2,3,4,5,6,7,8,9] int* printNumbers(int n, int* returnSize) { } Example: Input: 2012 12 31 Input: 1982 3 4 Output: 366 Output: 63









#include<stdio.h>
int main()
{
    
    
	int year = 0;
	int month = 0;
	int day = 0;
	int i = 0;
	scanf("%d %d %d", &year, &month, &day);
	if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
	{
    
    
		switch(month)
		{
    
    
			case 12:
				i += 31;
			case 11:
				i += 30;
			case 10:
				i += 31;
			case 9:
				i += 30;
			case 8:
				i += 31;
			case 7:
				i += 31;
			case 6:
				i += 30;
			case 5:
				i += 31;
			case 4:
				i += 30;
			case 3:
				i += 31;
			case 2:
				i += 29;
			case 1:
				i += 31;
				if (month == 8 || month == 1 || month == 3 || month == 5 || month == 7 || month == 10 || month == 12)
				{
    
    
					i = i - 31 + day;
				}
				else if (month == 2)
				{
    
    
					i = i - 29 + day;
				}
				else
				{
    
    
					i = i - 30 + day;
				}
				break;
			
		}
	}
	else
	{
    
    
		switch (month)
		{
    
    
		case 12:
			i += 31;
		case 11:
			i += 30;
		case 10:
			i += 31;
		case 9:
			i += 30;
		case 8:
			i += 31;
		case 7:
			i += 31;
		case 6:
			i += 30;
		case 5:
			i += 31;
		case 4:
			i += 30;
		case 3:
			i += 31;
		case 2:
			i += 28;
		case 1:
			i += 31;
			if (month == 8 || month == 1 || month == 3 || month == 5 || month == 7 || month == 10 || month == 12)
			{
    
    
				i = i - 31 + day;
			}
			else if (month == 2)
			{
    
    
				i = i - 28 + day;
			}
			else
			{
    
    
				i = i - 30 + day;
			}
			break;

		}
	}
	printf("%d",i);
	return 0;
}

In fact, there is a simple way to create an array to control. I will directly use switch control here. In fact, we don't need to write two switches, just add one to determine whether it is a leap year.

That’s it for today’s sharing, see you next time

Guess you like

Origin blog.csdn.net/2301_76895050/article/details/132519896