1. 把宏名全部大写,函数名不要全部大写。
  2. 注意宏定义表示数据类型和用 typedef 定义数据说明符的区别。宏定义只是简单的字符串替换,由预处理器来处理; typedef 是在编译阶段由编译器处理的,它并不是简单的字符串替换,而给原有的数据类型起一个新的名字,将它作为一种新的数据类型。
  3. #define 宏名(形参列表) 字符串,这与函数的调用是不同的,函数调用时要把实参表达式的值求出来再传递给形参,而宏展开中对实参表达式不作计算,直接按照原样替换。
  4. 带参数的宏和函数很相似,但有本质上的区别:宏展开仅仅是字符串的替换,不会对表达式进行计算;宏在编译之前就被处理掉了,
    它没有机会参与编译,也不会占用内存。
  5. 函数是一段可以重复使用的代码,会被编译,会给它分配内存,
    每次调用函数,就是执行这块内存中的代码。
#include <stdio.h>
#include <stdlib.h>

#define SQ(y) y*y
#define PRINT printf("file:%s line:%d\n", __FILE__, __LINE__)//__FILE__,__LINE__这是个啥?别急后面有补充!


#define STR(s) #s
#define JOIN(a,b) a##b

//
void test_define_str()
{
    printf("%s\n", STR(c.biancheng.net));
    printf("%s\n", STR(“c.biancheng.net”));
}
void fejiw()
{
    int a, sq;
    printf("input a number: ");
    scanf("%d", &a);
    sq = SQ(a+1);
    printf("sq=%d\n", sq);
}

#define CON1(a, b) a##e##b
#define CON2(a, b) a##b##00
#define ADD_TO(num, value) num##value

//
void test_con()
{
    printf("%f\n", CON1(8.5, 2));
    printf("%d\n", CON2(12, 34));
    int a = ADD_TO(114, 514);
	printf("%d \n", a);
}
//
#define PFINT printf("hello world!\n");\
			  printf("goodbye world!\n");
#define NUM 1,\
			2,\
			3
//
void test_huanhang()
{
	PFINT
	int x[] = { NUM };//->int x[] = { 1,2,3 };
	printf("%d %d %d \n", x[0], x[1], x[2]);
}

#define MALLOC(num, type) \ 
(type *)malloc(num * sizeof(type)) //这和\是干啥的?详见地四大点\的作用
// 反斜杠后不要有空格 backslash and newline separated by space

void test_malloc()
{
    // 使用
    int *b = MALLOC(10, int); // 类型作为参数
    free(b);
    b = NULL;
    // 预处理器替换之后:
    //(int *)malloc(10 * sizeof(int));
}

//
//这种能够根据不同情况编译不同代码、产生不同目标文件的机制,称为条件编译。
//条件编译是预处理程序的功能,不是编译器的功能。
void test_if_()
{
    #if _WIN32
        system("color 0c");
        printf("http://c.biancheng.net\n");
    #elif __linux__
        printf("\033[22;31mhttp://c.biancheng.net\n\033[22;30m");
    #else
        printf("http://c.biancheng.net\n");
    #endif
}

/*
#if 整型常量表达式1
    程序段1
#elif 整型常量表达式2
    程序段2
#elif 整型常量表达式3
    程序段3
#else
    程序段4
#endif
如常“表达式1”的值为真(非0),就对“程序段1”进行编译,否则就计算“表达式2”,
结果为真的话就对“程序段2”进行编译,为假的话就继续往下匹配,直到遇到值为真的表达式,
或者遇到 #else。这一点和 if else 非常类似。

需要注意的是,#if 命令要求判断条件为“整型常量表达式”,
也就是说,表达式中不能包含变量,而且结果必须是整数;而 if 后面的表达式没有限制,只要符合语法就行。
这是 #if 和 if 的一个重要区别。


#ifdef 的用法
#ifdef 用法的一般格式为:
#ifdef  宏名
    程序段1
#else
    程序段2
#endif

它的意思是,如果当前的宏已被定义过,则对“程序段1”进行编译,否则对“程序段2”进行编译。

也可以省略 #else:
#ifdef  宏名
    程序段
#endif


*/

#define NUMBER 10
//
void test_if()
{
    #if NUMBER == 10 || NUM == 20
        printf("NUM: %d\n", NUMBER);
    #else
        printf("NUM Error\n");
    #endif
}

#define NUM1 10
#define NUM2 20
int test_defined()
{
    #if (defined NUM1 && defined NUM2)
        //代码A
        printf("NUM1: %d, NUM2: %d\n", NUM1, NUM2);
    #else
        //代码B
        printf("Error\n");
    #endif
    return 0;
}

void testerror()
{
#ifndef __cplusplus
#error 当前程序必须以C++方式编译
//#error nihao bianyiqi budui
#endif
}


//
int main()
{
    //test_define_str();
    //test_con();
    //test_huanhang();
    //test_defined();
    testerror();
    return 0;
}

04-06 21:33