Analysis of 25 × and × operators

Operator operator

  • #Operator to convert macro parameters to strings during preprocessing
  • #The conversion of is completed in the preprocessing period, so it is only valid in the macro definition
  • Compiler does not know the conversion function of ා

usage

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

Example analysis: basic usage of ා operator

#include <stdio.h>

#define STRING(x) #x

int main()
{
    printf("%s\n", STRING(Hello word));
    printf("%s\n", STRING(100));
    printf("%s\n", STRING(while));
    printf("%s\n", STRING(return));
}
Output:
Hello word
100
while
return

Test_2.i

printf("%s\n", "Hello word");
printf("%s\n", "100");
printf("%s\n", "while");
printf("%s\n", "return");

Example analysis: the clever use of ා operator

Test_2.c

#include <stdio.h>

#define CALL(f, p) (printf("Call funtion %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;
}
Output:
Call funtion square
result = 16
Call funtion func
result = 10

Test_2.i

 result = (printf("Call funtion %s\n", "square"), square(4));

 result = (printf("Call funtion %s\n", "func"), func(10));

Operator operator

  • ##Operator to bond two identifiers during preprocessing
  • ##The connection function of is completed in the preprocessing period, so it is only valid in the macro definition
  • Compiler does not know the connection function of ා

usage

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

Example analysis: basic usage of the ා operator

Test_3.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));
}
Output:
1
2

Test_3.i

int main()
{
    int name1;
    int name2;

    name1 = 1;
    name2 = 2;

    printf("%d\n", name1);
    printf("%d\n", name2);
}

Case analysis: engineering application of operators

Test_3.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 = 1;
    
    s2.name = "s2";
    s2.id = 2;

    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;
}
Output:
s1.name = s1
s1.id = 1
s2.name = s2
s2.id = 2

Test_4.i

typedef struct _tag_Student Student; struct _tag_Student
{
 char* name;
 int id;
};

Summary

  • #Operator to convert macro parameters to strings during preprocessing
  • ##Operator to glue two identifiers during preprocessing
  • Compiler does not know the existence of ා and ා - operators
  • #And ා operators are only valid in macro definitions

The above contents refer to the series courses of Ditai Software Institute, please protect the original!

Tags: C

Posted on Tue, 03 Dec 2019 20:42:53 -0500 by bsfischer