• C


    #define

    #include <stdio.h>
    
    #define TWO 2
    #define OW "woow"
    #define FOUR TWO * TWO
    #define PX printf("X is %d 
    ",x) // 宏 定义函数
    #define FMT "X is %d 
    "	// 定义格式模版
    #define SQUARE(X) X*X	// 带参数宏
    #define XNAME(n) x ## n	// ## 运算符
    int main(int argc, char *argv[])
    {
    	int x = TWO;
    	PX;
    	x = FOUR;
    	printf(FMT, x);
    	printf("square(2) is %d 
    ", SQUARE(2));
    	char * XNAME(4) = "x4";
    	printf("预处理器粘合剂 ##运算符:%s .
    ", XNAME(4));
    
    	return 0;
    }
    
    
    

    其他指令

    #include <stdio.h>
    
    #pragma c9x on // 编译器设置都可以定义 强大到没朋友
    #define TWO 2
    #undef TWO // 取消宏
    #ifdef TWO	// 条件编译 相反为 #ifndef
    #define TWO 23
    #else
    #define TWO 3
    #endif 
    int main(int argc, char *argv[])
    {
    #ifdef TWO
    	printf("TOW is %d 
    ", TWO);
    #endif // TWO
    
    #if TWO == 3    // 条件判断
    	printf("oh , 3 
    ");
    #elif TWO ==2
    	printf("oh , 2 
    ");
    #endif
    	return 0;
    }
    
    

    内联函数

    就是不需要声明,直接定义然后调用的使用inline修饰函数,无法获取地址,可放在头文件供外部调用。
    相当于将方法体拷贝到调用处。

    C库

    数学库 math.h

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>   // 数学库
    void bye();
    void print_array(const int array[] , int size);
    int my_compar(const void * p1, const void * p2);    // 自定义 排序比较器
    int main(int argc, const char * argv[]) {
        atexit(bye); // 程序结束时回调函数
        double angle = 30.0 * 3.14/180; // 角度是这么定义的。。要跪
        double x = sin(angle);
        printf("sin(%f) = %0.2f 
    ",angle,x);
      
        int array[3] = {4,2,3};
        print_array(array,3);
        int (* fp)(const void *, const void *)  = my_compar;
        qsort(array, 3, sizeof(int),fp); // 排序,传入自定义比较器
        print_array(array,3);
        return 0;
    }
    void bye()
    {
        printf("bye 
    ");
    }
    int my_compar(const void * p1, const void * p2)
    {
        const int * p3 = (const int *)p1;
        const int * p4 = (const int *)p2;
        if(*p3 > *p4)return 1;
        else if(*p3 < *p4)return -1;
        else return 0;
    }
    void print_array(const int array[] , int size)
    {
        for(int i = 0 ; i < size ; i++)
        {
            printf("%d",array[i]);
        }
        printf("
    ");
    }
    

    string.h库中的内存拷贝函数

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define SIZE 3
    void print_array(const int array[] , int size);
    int main(int argc, const char * argv[]) {
        int array[SIZE] ={2,1,3};
        int target[SIZE];
        print_array(array,SIZE);
        memcpy(target, array, SIZE * sizeof(int)); // 内存拷贝,若要考虑重叠则使用memmove
        print_array(target, SIZE);
        return 0;
    }
    
    void print_array(const int array[] , int size)
    {
        for(int i = 0 ; i < size ; i++)
        {
            printf("%d",array[i]);
        }
        printf("
    ");
    }
    
  • 相关阅读:
    ffmpeg处理视频与声音
    吸引力
    bzoj 2752: [HAOI2012]高速公路(road)
    bzoj 3653 [湖南集训]谈笑风生
    bzoj 3143: [Hnoi2013]游走
    16,docker入门
    15.9,python操作redis集群
    15.8,redis-cluster配置
    15.7,哨兵集群
    15.6,redis主从同步
  • 原文地址:https://www.cnblogs.com/hiqianqian/p/6851593.html
Copyright © 2020-2023  润新知