在 C 語言中還有 # 和 ## 這種操做符的存在,只是不常常見,那麼咱們今天就來說下它們的用法。ide
A、 # 運算符用於在預處理期將宏參數轉換爲字符串,它的轉換做用是在預處理期完成的,所以只在宏定義中有效。編譯器不知道 # 的轉換做用,用法:#define STRING(x) #x。下來咱們以代碼爲例進行分析,代碼以下函數
#include <stdio.h> #define STRING(x) #x int main() { printf("%s\n", STRING(Hello world!)); printf("%s\n", STRING(100)); printf("%s\n", STRING(while)); printf("%s\n", STRING(return)); return 0; }
咱們看到第 7-10 行想要打印字符串,那麼咱們這樣定義能夠嗎?咱們編譯下看看結果學習
咱們看到已經打印出了字符串。那麼咱們來單步編譯下,看看通過預處理器處理過的文件變成什麼樣了,生成的代碼以下,仍是註釋掉頭文件ui
test.i 代碼
spa
# 1 "test.c" # 1 "<built-in>" # 1 "<command-line>" # 1 "/usr/include/stdc-predef.h" 1 3 4 # 1 "<command-line>" 2 # 1 "test.c" int main() { printf("%s\n", "Hello world!"); printf("%s\n", "100"); printf("%s\n", "while"); printf("%s\n", "return"); return 0; }
咱們看到宏定義裏面的字符已經變成相應的字符串了,證實咱們那樣定義仍是靠譜的。下來咱們再來看看一個示例代碼,看看 # 運算符的妙用,代碼以下3d
#include <stdio.h> #define CALL(f, p) (printf("Call function %s\n", #f), f(p)) int square(int n) { return n * n; } int func(int x) { return x; } int main() { int result = 0; result = CALL(square, 4); printf("result = %d\n", result); result = CALL(func, 10); printf("result = %d\n", result); return 0; }
咱們在這個代碼中想要打印出它調用的函數名,可是函數名是咱們隨機指定的。這樣的功能是 C 語言寫的函數實現不出來的,那麼咱們看看 # 運算符可以實現這樣的功能嗎?編譯看看結果blog
結果已經很明顯了。咱們再來看看通過單步編譯後的文件圖片
test.i 代碼
字符串
# 1 "test.c" # 1 "<built-in>" # 1 "<command-line>" # 1 "/usr/include/stdc-predef.h" 1 3 4 # 1 "<command-line>" 2 # 1 "test.c" int square(int n) { return n * n; } int func(int x) { return x; } int main() { int result = 0; result = (printf("Call function %s\n", "square"), square(4)); printf("result = %d\n", result); result = (printf("Call function %s\n", "func"), func(10)); printf("result = %d\n", result); return 0; }
咱們能夠看出確實如咱們所想的那樣。編譯器
B、 ## 運算符用於在預處理期粘連兩個標識符,它的做用是在預處理期完成的,所以只在宏定義中有效;編譯器不知道 ## 的鏈接做用,用法:#define CONNET(a, b) a##b。下面咱們仍是以代碼進行說明,代碼以下
#include <stdio.h> #define NAME(n) name##n int main() { int NAME(1); // name1 int NAME(2); // name2 NAME(1) = 1; // name1 = 1 NAME(2) = 2; // name2 = 2 printf("NAME(1) = %d\n", NAME(1)); printf("NAME(2) = %d\n", NAME(2)); return 0; }
咱們看到是想實現註釋的那樣,那麼可否實現呢?看看編譯結果
咱們再來看看單步編譯的代碼,更加明顯
test.i 代碼
# 1 "test.c" # 1 "<built-in>" # 1 "<command-line>" # 1 "/usr/include/stdc-predef.h" 1 3 4 # 1 "<command-line>" 2 # 1 "test.c" int main() { int name1; int name2; name1 = 1; name2 = 2; printf("NAME(1) = %d\n", name1); printf("NAME(2) = %d\n", name2); return 0; }
就不說了,結果已經很明顯了。下來咱們再來看看一個頗有意思的代碼,用 ## 運算符實現結構體的定義,頗有意思
#include <stdio.h> #define STRUCT(type) typedef struct _tag_##type type;\ struct _tag_##type STRUCT(Student) { char* name; int id; }; int main() { Student s1; Student s2; s1.name = "s1"; s1.id = 0; s2.name = "s2"; s2.id = 1; printf("s1.name = %s\n", s1.name); printf("s1.id = %d\n", s1.id); printf("s2.name = %s\n", s2.name); printf("s2.id = %d\n", s2.id); return 0; }
什麼都不說,先看編譯結果可否知足需求
已經完美實現,再來看看通過單步編譯後的代碼
test.i 代碼
# 1 "test.c" # 1 "<built-in>" # 1 "<command-line>" # 1 "/usr/include/stdc-predef.h" 1 3 4 # 1 "<command-line>" 2 # 1 "test.c" typedef struct _tag_Student Student; struct _tag_Student { char* name; int id; }; int main() { Student s1; Student s2; s1.name = "s1"; s1.id = 0; s2.name = "s2"; s2.id = 1; printf("s1.name = %s\n", s1.name); printf("s1.id = %d\n", s1.id); printf("s2.name = %s\n", s2.name); printf("s2.id = %d\n", s2.id); return 0; }
看上面的代碼,啥也不想說了。已經很明顯了,咱們已經看到了 ## 運算符的強大之處。若是咱們的 # 和 ## 運算符運用的好的話,那麼代碼質量就會很是高。經過對 # 和 ## 運算符的學習,總結以下:一、# 運算符用於在預處理期將宏參數轉換爲字符串;二、## 運算符用於在預處理期粘連兩個標識符;三、編譯器不知道 # 和 ## 運算符的存在,# 和 ## 運算符只在宏定義中有效。
歡迎你們一塊兒來學習 C 語言,能夠加我QQ:243343083。