C language preprocessing function-about stringization and symbol pasting

Source: Internet
Author: User

 

In C language development, macro definition is a very useful tool that makes our code easier to understand and maintain. If a constant is used in multiple places and may be modified according to different needs in the future, it would be better to define it. In addition, macro definition has other functions. Understanding it will help us develop C Programs better. Let's take a look at an example:

 

# Define Conn (x, y) x # y

# Define ToString (x) # x

# Define ToChar (x) # @ x

 

In these macro definitions, "#", "#", and "# @" are used in the following functions:

1. Bonding operator # -- connects two macro names. Note that the macro names are connected, rather than the values referred;

For example, int Conn (a, B); defines an int type variable AB, which can be called directly in the future without using the Conn (a, B) form;

Printf (Conn ("AB", "cd"); the output is: abcd

However:

 

# Define M 0

# Define var (x) Var _ # x

...

 

Int var (M); // What is defined here?

 

According to ANSI/iso c, the # operator simply binds two macro names, then int var (M) should be defined as Var_M. According to tests, this is correct. However, in some older C compiling environments, Var_0 may also be defined. For example, in TC 2.0, we found that "Var_0 = 0" can be compiled and passed, "Var_M = 0" has an ERROR.

2. String operators # -- convert macro names into strings

For example, printf (ToString (var1); the output result is var1. Var1 can be a defined variable name or a character combination that never appears.

Similarly, if:

 

# Define STR 0

...

 

Printf (Tostring (STR ));//

 

STR is output in the popular compiling environment, while TC 2.0 outputs 0.

3. Character-driven operations # @ -- convert macro names into characters. Note: Early compilers may not support

For example:

 

Char c;

 

C = ToChar (1); // c = '1'

C = ToChar (a); // c = 'A'

 

If more than one character is provided in the macro (Note: no more than four characters; otherwise, the compiler reports an error), the conversion result is the last character, as shown in figure

 

C = ToChar (abc); // c = 'C'

C = ToChar (abcd); // c = 'D'

C = ToChar (abcde); // ERROR

To sum up, I have summarized its usage.

 

1. Use follows the ansi c Rules, but remember that the compilation and compilation is only a problem that early compilers do not support the C standard;

2. ## operations can be applied to variable definitions. If many variables need to be defined in program development and these variables have the same prefix, ## is especially important, it makes the code more clean and reduces the chance of errors. If you find that you need to modify the variable prefix in batches, you will be glad to have used this powerful tool;

3. # operators can be used to output variable names during debugging. They can be used together with ##, for example, # define CHECK_VAR (x, fmt) printf (# x "=" # fmt "\ n", x), then CHECK_VAR (var1, % d) is equivalent to printf ("var = % d \ n", var1 );

Tips:

1. ansi c stipulates that if the macro definition name appears in quotation marks ('or ""), it will not be replaced, but some early compilers may have different processing methods, for example, # define CTRL (c) ('C' & 37) is extended to ('C' & 37) according to standard CTRL (d ). Obviously, this did not fulfill the author's intention. It happened to work in some compilers, but it should be avoided in actual use.

2. the string connection does not need to be used # so troublesome. In reality, two string constants can be directly written together, for example, printf ("AB" "cd") Outputs abcd. Or when using # macro definition, you can use printf (ToString (str) "\ n"); the output string is followed by a line break, which is not used in the past, later I tried it and found that it was quite easy to use. Of course, puts can also be used to accomplish the same function.

3. You can use an indirect method to convert the value of a macro character constant to # Or #, for example:

# Define ToString (x) # x

# Define Xstr (x) ToString (x)

# Define STR1 STR2

...

 

Printf (Xstr (STR1); // The output result is STR2 rather than STR1.

 

This article is from the blog "xuexiangjing ".

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.