C language functions and the use of functions

How to customize a function for addition operation, the code is as follows:

int add(int x, int y)
{
    int z = 0;
    z = x + y;
    return z;
}
int main()
{
    int a = 10;
    int b = 20;
    int sum = 0;
    sum = add(a, b);
    printf("sum = %d\n", sum);
    return 0;
}

The use of strcpy, the code is as follows:

int main()
{
    char arr1[] = "bit";
    char arr2[10] = "##########";
    strcpy(arr2, arr1);
    //string copy - 字符拷贝
    //strlen - string length - 字符串长度有关
    printf("%s\n", arr2);
    return 0;
}

The use of memset, the code is as follows:

int main()
{
    char arr[] = "hello world";
    memset(arr, '*', 5);
    printf("%s\n", arr);
    //***** world
    return 0;
}

Customize a function to compare the size of two numbers, the code is as follows:

int get_max(int x, int y)
{
    if (x < y)
        return y;
    else
    {
        return x;
    }
}
int main()
{
    int a = 10;
    int b = 20;
    int max = get_max(a, b);
    printf("max = %d\n", max);
    max = get_max(100,500);
    printf("max = %d\n", max);
    return 0;
}

Use the function to exchange two integers, the code is as follows:

//这样是不能转换的
//void swap1(int x, int y)
//{
//  int tmp = 0;
//  x = tmp;
//  x = y;
//  y = tmp;
//}
使用函数交换两个整数
void swap2(int* qa, int* qb)
{
    int tmp = 0;
    tmp = *qa;
    *qa = *qb;
    *qb = tmp;
}
int main()
{
    int a = 10;
    int b = 20;
    printf("a = %d,b = %d\n", a, b);
    //swap1(a, b);//传值调用
//调用swap2函数
    swap2(&a, &b);//传址调用
    printf("a = %d,b = %d\n", a, b);
    return 0;
}

Here to understand the difference between call by address and call by value, the scope of application

Use the function to print prime numbers between 100 and 200, the code is as follows:

//是素数返回1,不是返回0
int prime(int a)
{
    //2—>a-1
    int b = 0;
    for ( b = 2; b <= sqrt(a); b++)
    {
        if (a%b == 0)
            return 0;
    }
    return 1;
}
int main()
{
    //打印100到200之间的素数
    int i = 0;
    for ( i = 100; i <= 200; i++)
    {
        //判断i是否为素数
        if (prime(i) == 1)
            printf("%d\n", i);
    }
    return 0;
}

Write a function to print leap years from 1000 to 2000. The code is as follows:

int is_leap_year(int a)
{
    if ((a % 4 == 0 && a % 100 != 0)||(a%400 == 0))
    {
        return 1;
    }
    return 0;
}
int main()
{
    //写一个函数打印1000到2000年的闰年
    int year = 0;
    for ( year = 1000; year <= 2000 ; year++)
    {
        if (is_leap_year(year) == 1)
            printf("%d\n", year);
    }
    return 0;
}

In fact, it is much the same as using the martyrdom loop before, with one more call.
The function implements binary search, the code is as follows:

//              本质上arr是一个指针
int binary_search(int arr[], int k,int sz)
{
    //算法的实现

    int left = 0;
    int right = sz - 1;

    while (left <= right)
    {
        int mid = (left + right) / 2;//中间元素的下标
        if (arr[mid]<k)
        {
            left = mid + 1;
        }
        else if (arr[mid]>k)
        {
            right = mid - 1;
        }
        else
        {
            return mid;
        }
    }
    return -1;
}
int main()
{
    //二分查找
    //在一个有序数组中查找具体的某个数
    //如果找到了返回这个数的下标,找不到返回-1
    //
    int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    int k = 7;
    int sz = sizeof(arr) / sizeof(arr[0]);//求数组的元素个数
    int ret = binary_search(arr, k,sz);
    if (ret == -1)
    {
        printf("找不到指定的数字\n");
    }
    else
    {
        printf("找到了,下标是:%d\n", ret);
    }
    return 0;
}

Write a function, every time this function is called, the value of num will be +1

void add(int* p)
{
    (*p)++;
}
int main()
{
    int num = 0;
    add(&num);
    printf("num = %d\n", num);//1
    add(&num);
    printf("num = %d\n", num);//2
    add(&num);
    printf("num = %d\n", num);//3
    return 0;
}

Chained access to functions

int main()
{
    printf("%d", printf("%d", printf("%d", 43)));//4321

    return 0;
}

Function declaration, call, definition

//函数的声明
int add(int x, int y);

int main()
{
    int a = 10;
    int b = 20;
    int sum = 0;
    //函数的调用
    sum = add(a, b);
    printf("%d\n", sum);
    return 0;
}
//函数的定义
int add(int x, int y)
{
    int z = x + y;
    return z;
}

The above is today's learning content, I hope to stick to the code and blog every day.
2021.1.22

Guess you like

Origin blog.51cto.com/15080720/2602823