A little discussion about Macros in C Language

Source: Internet
Author: User

Preface:

If you are familiar with C language, you should be familiar with macros. macros are often used in C language programming. The main purpose of using macros is to facilitate programming for programmers, in addition, the program efficiency can be improved to a certain extent. The macro definition command in C language is # define. The following are some small details about using macros.

1. Use in macros ()

In the macro, try to use () as much as possible. Do not think it doesn't matter or think that using or not using it has no impact. Please remember to use () as much as possible in the macro so as to ensure the correctness of your macro as much as possible.

For example:

# Define CAL (x, y) x + y

CAL (2, 4) × CAL (2, 5)

The programmer's intention may be to calculate (2 + 4) × (2 + 5), but this macro is like this: 2 + 4 × 2 + 5, it is totally different from the expected result.

A reasonable definition should be # define CAL (x, y) (x) + (y) to avoid unnecessary ambiguity.

2. Repeated macro definitions

Do not duplicate definition macros as much as possible, which will lead to poor readability and error-prone and difficult maintenance of other programs. Duplicate macro definitions are subject to the last definition.

Example:

# Define PI 3.14

# Define PI 10.48

In the program, the PI value should be 10.48, because this is the final definition.

3. Dangerous macro definition

Macro definitions are dangerous when used improperly. Random definitions can confuse the thinking of programmers, for example:

# Define int char

# Define break continue

Such macro definition is quite dangerous, and will make the program quite messy, difficult to read, and difficult to maintain.

4. # And # in the macro ##

# In a macro can convert characters into strings, while # can splice macro parameters.

For example:

# Define SUM (a, B) printf ("a add B is % d \ n", (a) + (B )));

In the program, if this macro is used as follows: SUM (1, 2), the output is a add B is 3.

Note that a and B in the quotation marks are treated as plain text rather than symbols that can be replaced. If you want a and B to be replaced, you can define the macro as follows:

# Define SUM (a, B) printf ("# a add # B is % d \ n", (a) + (B )));

In the program, if this macro is used as follows: SUM (1, 2), the output is 1 add 2 is 3.

Like the # operator, the # operator can be used to replace a macro. This symbol can be used to splice two symbols into one. Example:

# Define NAME (x, y) x # y

If this macro is used in the program as follows: NAME (Steve, Johnson), the macro will be expanded as follows: SteveJohnson

5. Pay attention to the use of semicolons for macro definition.

Pay attention to the use of semicolons in macro definition. The use of semicolons in macro definition may cause some inexplicable errors.

For example:

# Define SUM (a, B) (a) + (B ));

This macro is used in the program as follows: SUM (a1, b1) + SUM (a2, b2)

In this way, the macro scale becomes (a1) + (b1); + (a2) + (b2) so that the program will go wrong.

6. An interesting macro test question:


# Include <stdlib. h>
# Include <string. h>
# Include <stdio. h>
 
# Define num 10
# Define f (x) # x
# Define g (x) f (x)
 
Int main ()
{
Printf ("f (num) = % s g (num) = % s \ n", f (num), g (num ));
}
This is an interesting small question about macros. You can think about the output result.
Run the gcc-E marco. I marco. c> a.txt command to save the prepared result to the.txt file. The Preprocessing Result of the main function is:

Int main ()
{
Printf ("f (num) = % s g (num) = % s \ n", "num", "10 ");
}

We can see that f (x) has the # symbol, so num is not replaced with 10, and g (x) should be replaced with f (x) first ), at the same time, replace num with 10 and change it to #10. Therefore, the result is num and 10.

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.