亲宝软件园·资讯

展开

C语言 #与##

清风自在 流水潺潺 人气:0

一、# 运算符

#define STRING(x) #x
printf("%s\n",STRING(Hello World!));

下面通过一个示例感受一下:

test.c:

#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;
}

输出结果如下:

为了直观看预处理期间的变化,下面进行单步编译,输入 gcc -E test.c-o test.i,下面为部分输出结果:

test.i:

# 2 "test.c" 2
 
 
 
int main()
{
 
    printf("%s\n", "Hello world!");
    printf("%s\n", "100");
    printf("%s\n", "while");
    printf("%s\n", "return");
 
    return 0;
}

可以看到宏参数在预处理阶段转换为字符串。

下面再来看一下运算符的妙用,动态打印当前调用的函数名

test.c:

#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;
}

下面为输出结果:

同样,可以进行单步编译,输入 gcc -E test.c-o test.i,看看预处理后的结果,下面为部分输出结果:

test.i:

# 2 "test.c" 2
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;
}

二、## 运算符

#define CONNECT(a, b) a##b
int CONNECT(a,1);   //int a1;
a1 = 2;

下面通过一个示例,看看运算符的基本用法:

test.c:

#include <stdio.h>
 
#define NAME(n) name##n
 
int main()
{    
    int NAME(1);
    int NAME(2);
    
    NAME(1) = 1;
    NAME(2) = 2;
    
    printf("%d\n", NAME(1));
    printf("%d\n", NAME(2));
 
    return 0;
}

输出结果如下:

可以进行单步编译,输入 gcc -E test.c-o test.i,看看预处理后的结果,下面为部分输出结果:

test.i:

# 2 "test.c" 2
int main()
{
 
    int name1;
    int name2;
 
    name1 = 1;
    name2 = 2;
 
    printf("%d\n", name1);
    printf("%d\n", name2);
 
    return 0;
}

再来看一个 ## 运算符的工程应用:

test.c:

#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;
}

输出结果如下:

这么做的好处就是在需要定义很多结构体时,可以尽量少写 struct 和 typedef,提高工作效率,另外使得代码更加美观。

可以进行单步编译,输入 gcc -E test.c-o test.i,看看预处理后的结果,下面为部分输出结果:

test.i:

# 2 "test.c" 2
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;
}

三、小结

加载全部内容

相关教程
猜你喜欢
用户评论