“This is the 9th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Recently, I want to review C language, so I will update an article about C language in nuggets every day! Freshmen who are just learning C, and those who want to review C /C++, don’t miss it! Solid foundation, slow down is fast!

4. Write a function to determine if it’s a leap year

Leap year: can be4Divisible && cannot be divided100Aliquot | | can be400aliquotCopy the code

From Baidu Baike

A leap year is a term in the calendar, which is divided into common leap year and century leap year.

Leap years were created to make up for the difference between the number of days in the Year that the calendar dictates and the earth’s actual revolution. The years in which the time difference is made up are leap years. Leap years have 366 days (31, 29, 31, 30, 31, 30, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, respectively).

For years with a leap day (February 29) in the Gregorian calendar, a leap surplus is placed. The difference between the lunar year and the tropical year).

Notice that there is no direct connection between leap year (Gregorian calendar) and leap month (lunar calendar). The Gregorian calendar only has leap years and years. Ordinary years have 365 days, and leap years have 366 days (an extra day in February). There may also be leap months in ordinary years (for example, 2017 is an ordinary year, and the lunar calendar has leap months, leap months).


1 / / method
int is_leap_year(int year)
{
    if(year % 4= =0 && year % 100! =0 || year % 400= =0)
    {
        return 1;
    }
    else
        return 0;
}
Copy the code
// This is the way to write it
int is_leap_year(int year)
{
    return (((year % 4= =0) && (year % 100! =0)) || (year % 400= =0));
}
Copy the code

5. Implement binary search of an integer ordered array

The premise of binary search method: array is ordered each time can reduce the interval, is an efficient algorithm!

/ / dichotomy
int BinarySearch(int* arr, int sz,int k)
{
	int right = sz - 1;	/ / right subscripts
	int left = 0;	/ / left subscript
	while (left <= right)
	{
		int mid = (right + left) >> 1;	/ / intermediate value
		//mid = (right + left)/2
		if (arr[mid] > k)
		{
			right = mid - 1;	// The right subscript becomes the median -1 position
		}
		else if (arr[mid] < k)
		{
			left = mid + 1;		// The left subscript becomes the median +1 position
		}
		else
			return mid;
	}
	return - 1;	/ / couldn't find it
}
int main(a)
{
	int arr[] = { 0.1.2.3.4.5.6.7.8.9 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	int k = 7;	// The number to find
	int ret = BinarySearch(arr, sz,k);
	if(ret ! =- 1)
	{
		printf("Found, subscript: %d\n", ret);
	}
	else
	{
		printf("Can't find \n");
	}
	return 0;
}
Copy the code

TDD:

TDD: Write the test code before you write the code for a feature, then write only the feature code that makes the test pass, and use the test to drive the development


Return argument void:

If the return type of the function is void:, return can be left blank or written as return.


6. The value of num is incremented each time this function is called

Because the function changes the parameter value, it passes the address

void Add(int* p)
{
	(*p)++;
    // Can also be written as
    // *p++ + = (*p) ++
	return;
}
int main(a)
{
	int num = 0;
	printf(Num = %d\n, num);
	Add(&num);
	printf(Num = %d\n, num);
	Add(&num);
	printf(Num = %d\n, num);
	return 0;
}
Copy the code

Notation 2: Pass – Receive the return value

int Add(int n )
{
    return n+1;
}
int main(a)
{
	int num = 0;
	printf(Num = %d\n, num);	/ / 0
	num = Add(num);
	printf(Num = %d\n, num);	/ / 1
	num = Add(num);
	printf(Num = %d\n, num);	/ / 2
	return 0;
}
Copy the code

3. Chain access of functions

Chained access: Taking the return value of one function as an argument to another function

int main(a)
{
    int len = strlen("abc");
    printf("%d\n",len);	/ / 3
    // chain access
    printf("%d\n".strlen("abc"));	/ / 3
    
    char arr1[20] = "xxxxxx";
    char arr2[20] = "abc";
    printf("%s\n".strcpy(arr1,arr2));	//abc
    
    printf("%d".printf("%d".printf("%d".43)));	/ / 4321
    //printf returns the number of characters to print
    return 0;
}
Copy the code

1. Write files in chunks


Benefits of block writing: 1. Multiple collaborators 2. Encapsulation and hiding


2. How do I import static libraries

Import static library:#pragmaThe comment (lib, "add lib")The name of the static library is add.libCopy the code

Place add.c and add.h in the debug file to find the add.lib file


3. Prevent header files from being included repeatedly

// pragma 1: #pragma once

// Method 2: # ifdef. h file name #define. H file name end with #endif

#ifdef Add.h #define Add.h #endif


That’s all for today. Thank you for seeing us! Hope to help you! You are welcome to click on this topic and subscribe! At the same time, welcome the bigwigs to criticize and correct!