#define macro definition in C language command usage _c language

Source: Internet
Author: User

#define
The command #define defines an identifier and a string. Each time the identifier is encountered in the source program, it is substituted with a defined string. The ANSI standard defines an identifier as a macro name and replaces the process with a macro substitution. The general form of the command is:

#define Identifier string

Attention:
1. The statement does not have a semicolon. There can be any space between the identifier and the string, which, once started, ends only with a new row.
2. Once the macro name is defined, it becomes part of the definition of another macro name.
3. A macro substitution is simply a text string instead of a macro identifier, provided that the macro identifier must be recognized independently or not replaced. For example:
#define XYZ This is a TES
Use macro printf ("xyz");//This section does not print "This is a test" and prints "xyz". Because the preprocessor recognizes "XYZ"
4. If the string is longer than one line, you can continue the line with a backslash ' \ ' at the end of the line.
#defineLONG_STRING "This is a very long\
String, is used as a example "
The 5.C language program generally uses uppercase letters to define identifiers.
6. One of the great benefits of replacing real functions with macro substitution is that macro substitution increases the speed of the code because there is no overhead for function calls. But the increase in speed also has a price: due to repeated coding and increase the length of the program.

Considerations for macro Definitions
attempting to define annotation symbols using macros is not possible, such as the following code:

#define BSC//
#define BMC/*
#define EMC */
BSC my single-line comment
BMC my multi-line comment EMC

Because annotations are processed prior to preprocessing directives, a bunch of errors naturally occur when you expand these macro definitions.
Macro-Definition expressions must not be stingy with parentheses. This doesn't have to be explained.
It is best not to have spaces in macro names when a macro is defined. The following definitions can cause a lot of trouble:

  #define SUM (x) ((x) * (x))

Once you use the #undef undo macro, the following code cannot use the macro unless you define it again. In addition, if you define it directly without #undef, later definitions will override the previous definition. The C value in the following code is 4:

  #include <stdio.h>
 
  #define x 3
  #define y x*2
  #undef x
  #define x 2
 
  int c = y;
 
  int main (int argc, char** argv) {
    printf ("%d", c);
    return 0;
  }

Macros are only expanded when used, otherwise there will be no compilation errors even if there is a problem with the definition. If you comment out the second #define in the code above and assign the value of C to 0, you undo the X definition and do not make an error, because Y is not used and will not unfold.

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.