Differences between enumerations and macros detailed parsing of _c languages

Source: Internet
Author: User

There are several main differences between enumerated constants and macros:

1. an enumerated constant is one of the entities, but the macro is not an entity;

2. an enumerated constant is a constant, but a macro is not a constant;

3. an enumerated constant has a type, but the macro has no type, and the enumeration variable has the same scope, value, and other properties as the normal variable, but the macro is not, and the macro is not part of the language, it is a preprocessing substitution. Enum types are used primarily for restrictive input, for example, a parameter of a function accepts only a finite number of values in a type, and other values are not accepted, which can be a good solution to this problem. You can use enumerations to use enumerations as much as possible, otherwise you won't be able to see the value at the time of debugging.

4. define a variable with a macro if you define a variable that depends on who is in front, if the macro is in front of the variable will produce a compile error, and this error is difficult to find, if the macro hidden deep. If you define a variable that is more frightening in the first place, there is no error directly, but the macro definition is silently replaced by the custom variable. With enumeration definitions, no matter how the sequence of relationships you define will result in duplicate-defined errors. From the example above, enumerations are much more useful than macros. A macro also has an attribute that has no scope, and the scope here is that macros can be used by code that is defined later in the macro. Macros can be repeated to define this may cause the value of the macro to be modified. Therefore, it is recommended that you do not use macros to define the variables for shaping, using enumerations or Const. It will also be useful to the const or enumeration, the world has always been so tangled, enumerations can only represent an integer, the const may modify any type. In the case of plastic surgery, if you want to define several relational values, use an enumeration, otherwise, use Const.

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.