网站后台编辑框变灰了,找回网站后台,网站改中文,专业简章把宏名全部大写#xff0c;函数名不要全部大写。注意宏定义表示数据类型和用 typedef 定义数据说明符的区别。宏定义只是简单的字符串替换#xff0c;由预处理器来处理#xff1b; typedef 是在编译阶段由编译器处理的#xff0c;它并不是简单的字符串替换#xff0c;而给…把宏名全部大写函数名不要全部大写。注意宏定义表示数据类型和用 typedef 定义数据说明符的区别。宏定义只是简单的字符串替换由预处理器来处理 typedef 是在编译阶段由编译器处理的它并不是简单的字符串替换而给原有的数据类型起一个新的名字将它作为一种新的数据类型。#define 宏名(形参列表) 字符串这与函数的调用是不同的函数调用时要把实参表达式的值求出来再传递给形参而宏展开中对实参表达式不作计算直接按照原样替换。带参数的宏和函数很相似但有本质上的区别宏展开仅仅是字符串的替换不会对表达式进行计算宏在编译之前就被处理掉了 它没有机会参与编译也不会占用内存。函数是一段可以重复使用的代码会被编译会给它分配内存 每次调用函数就是执行这块内存中的代码。
#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(a1);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()
{PFINTint 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 spacevoid test_malloc()
{// 使用int *b MALLOC(10, int); // 类型作为参数free(b);b NULL;// 预处理器替换之后//(int *)malloc(10 * sizeof(int));
}//
//这种能够根据不同情况编译不同代码、产生不同目标文件的机制称为条件编译。
//条件编译是预处理程序的功能不是编译器的功能。
void test_if_()
{#if _WIN32system(color 0c);printf(http://c.biancheng.net\n);#elif __linux__printf(\033[22;31mhttp://c.biancheng.net\n\033[22;30m);#elseprintf(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 20printf(NUM: %d\n, NUMBER);#elseprintf(NUM Error\n);#endif
}#define NUM1 10
#define NUM2 20
int test_defined()
{#if (defined NUM1 defined NUM2)//代码Aprintf(NUM1: %d, NUM2: %d\n, NUM1, NUM2);#else//代码Bprintf(Error\n);#endifreturn 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;
}