zoukankan      html  css  js  c++  java
  • #define用法和应用介绍

    #include<iostream>
    #define to_string(s) #s  
    #define com(x,y) x##y
    #define ad(x) ( ((x)>0)? -(x):(x))
    #define PI 3.14
    /*
    	预处理命令:#define用法和应用 
    		#define x y    //把文件中的x都替换成y,直接替换 
    	应用:
    		1.用于宏定义(把字符串替换成常量) eg:#define PI 3.14
    		2.用于函数替换(表达式多用圆括号,为了区分)   格式 #define 函数名(不带类型的参数列表) 表达式 
    			eg:求最大值函数
    			#define max(x,y) ( (x>y)?(x):(y) )
    			。。。 
    			cout<<max(12,1)<<endl;  //把max(12,1)替换成#define定义的函数 
    			用于替换文件中的max(x,y)函数,返回值是后面的语句
    		3.#define 和"#" 或者"##"连用  只能和#define连用 
    			1.#的作用是:使#后面的首个参数返回一个带引号的字符串
    			2.##的作用是:连接##前后的内容
    			eg:#define to_string(s) #s
    			调用 cout<<to_string(hello world)<<endl;   //to_string的回结果是"hello world" 
    			eg:#define ad(x,y) x ## y
    			调用 cout<<ad(12,3)<<endl;  //ad(12,3)返回的结果是 123  不改变类型就是简单的连接 
    */
    using namespace std;
    int main()
    {
    	cout<<to_string(hello world)<<endl;
    	cout<<sizeof(com(19,2))<<endl;
    	cout<<ad(-12)<<endl;
    	cout<<PI*PI<<endl;
    	return 0;
    }
    

      

  • 相关阅读:
    2020/10/29
    2020/10/24
    2020/10/28
    2020/10/31周报
    linux shell 中判断字符串为空的正确方法
    20201107 千锤百炼软工人
    20201103 千锤百炼软工人
    20201109 千锤百炼软工人
    20201111 千锤百炼软工人
    20201105 千锤百炼软工人
  • 原文地址:https://www.cnblogs.com/nanfengnan/p/14432071.html
Copyright © 2011-2022 走看看