歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> ##號的用途及Linux C測試代碼

##號的用途及Linux C測試代碼

日期:2017/3/1 10:51:25   编辑:Linux編程

大家都知道##這兩個的意思是什麼吧?見下:

和#一樣 此運算符可以用於宏函數的替換部分.
這個運算符把兩個語言符號組合成單個語言符號.例如: #define MAN(woman) Man##woman 使用: MAN(Super) 展開就是:ManSuper

其實就是連接的作用,例如:a##1 的結果就是a1,這是簡單的用法了,若只是如此的話並莫有什麼意義的,但也不要小瞧它喲 在很多實際運用中還是有大用處的,如宏定義一個打印方法:

/*************************************************
* Function: Test double '#' character connection
* Author : Samson
* Date : 11/23/2011
* Test platform:
* GNU Linux version 2.6.29.4
* gcc version 4.4.0 20090506 (Red Hat 4.4.0-4) (GCC)
* *************************************************/

#include <stdio.h>
#include <stdlib.h>

#define YYG_PRINT(fmt, arg...) printf(fmt, ##arg)

int
main()
{

int m = 20, n = 90, l = 80;
char str[16] = "amstr";
char ch = 'a';
YYG_PRINT("m is %d n is %d l is %d str is %s ch is %c\n", m, n, l, str, ch);
exit(0);

}


run result:

[root@UFO]# ./a.out
m is 20 n is 90 l is 80 str is amstr ch is a

Copyright © Linux教程網 All Rights Reserved