zoukankan      html  css  js  c++  java
  • C利用宏语言(#,##,do…while(0)盛大)

    C利用宏语言(#,##。do…while(0)盛大)


    1.使用宏预先定义__FILE__,__FUNCTION__。__LINE__。

    #include <stdio.h>
    void fun(void)
    {
    	char v1;
    	short v2;
    	int v3;
    	long v4;
    	printf("v1: %x
    ",&v1);
    	printf("v2: %x
    ",&v2);
    	printf("v3: %x
    ",&v3);
    	printf("v4: %x
    ",&v4);	
    	printf("file is :%s,	function is: %s,	line is :%d
    ",__FILE__,__FUNCTION__,__LINE__);
    }
    
    int main()
    {
    	fun();
    	printf("file is :%s,	function is: %s,	line is :%d
    ",__FILE__,__FUNCTION__,__LINE__);
    	return 0;		
    }
    

    2.#宏字符串化

    #include<stdio.h>
    #define dprintf(expr) printf("<main>%s=%d
    ",#expr,expr)
    int main()
    {
       int x=100;
       int y=2;
       
       dprintf(x/y);//#expr相当于x/y   expr是x/y的运算结果  #是字符串化操作
       dprintf(x+y);
       dprintf(x+y+2);
       return 0;
    }
    

    3.##连接字符串

    #include<stdio.h>
    #define test(x) test ##x
    void test1(int a)
    {
       printf("Test 1 integer: %d 
    ",a);
    }
    void test2(char *s)
    {
       printf("Test 2 String: %s 
    ",s);
    }
    int main ()
    {
       test(1)(100); //test(1)等价于test; test##1等价于test1;##连接字符
       test(2)("hello");
       return 0;
    }
    


    4.do...while(0)宏


    #include<stdio.h>
    #define hello(str) do{
    	printf("Hello:%s
    ",str);
    	}while(0) 
    
    int main()
    {
    	int m=0;
    	if(m)
    		{
                     
    		hello("hello true");
    }
    	else
    		hello("hello false");
    		 system("pause");
    	return 0;
    }
    


    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    kettle 3:java调用transformation
    SEO网站优化笔记
    图案设计的素材
    TweenLite 使用详解(译文)
    flv文件修复文件头
    一个拼图素材
    外螺旋矩阵排列
    20160226.CCPP体系详解(0036天)
    tcp三次握手与四次分手
    docker 应用数据的管理之bind mounts
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4747977.html
Copyright © 2011-2022 走看看