栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Day 1--c语言的预处理

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Day 1--c语言的预处理

预处理器是一些指令,指示编译器在实际编译之前所需完成的预处理

#define预处理

#include 
using namespace std;
#define PI 3.14159
//define将后面所有出现的PI 定义为3.14159
int main ()
{
    cout << "Value of PI :" << PI << endl; 
    return 0;
}

#参数宏

#include 
using namespace std;
 
#define MIN(a,b) (a 

#条件编译:有选择对源代码进行编译

#include 
using namespace std;
#define DEBUG
#define MIN(a,b) (((a)<(b)) ? a : b)
int main ()
{
   int i, j;
   i = 100;
   j = 30;
    
//DEBUG在上面已经定义过了,该语句可以执行 
//#ifndef 表示相反
#ifdef DEBUG
   cerr <<"Trace: Inside main function" << endl;
#endif
​
//0表示的地方该语句将不会被执行  
#if 0
   cout << MKSTR(HELLO C++) << endl;
#endif
 
    
    
    
   cout <<"The minimum is " << MIN(i, j) << endl;
​
    
    
#ifdef DEBUG
   cerr <<"Trace: Coming out of main function" << endl;
#endif
    return 0;
}
​

#和##运算符

# 运算符会把 replacement-text 令牌转换为用引号引起来的字符串。

#include 
using namespace std;
 
#define MKSTR( x ) #x
 
int main ()
{
    cout << MKSTR(HELLO C++) << endl;
 
    return 0;
}
​

## 运算符用于连接两个令牌。

#include 
using namespace std;
 
#define concat(a, b) a ## b
int main()
{
   int xy = 100;
   
   cout << concat(x, y);
   return 0;
}
​

#C++中预定义宏

#include 
using namespace std;
 
int main ()
{
    //这会在程序编译时包含当前行号。
    cout << "Value of __LINE__ : " << __LINE__ << endl;
    //这会在程序编译时包含当前文件名。
    cout << "Value of __FILE__ : " << __FILE__ << endl;
    //这会包含一个形式为 month/day/year 的字符串,它表示把源文件转换为目标代码的日期
    cout << "Value of __DATE__ : " << __DATE__ << endl;
    //这会包含一个形式为 hour:minute:second 的字符串,它表示程序被编译的时间。
    cout << "Value of __TIME__ : " << __TIME__ << endl;
 
    return 0;
}
​
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/849926.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号