Define vs const VS enum

Source: Internet
Author: User

Used to C, used to define;const,enum what the basic will not be used, now there is time to tidy up a bit.

/***********************************************************************************************************/

Define: Macro definition, in the precompiled phase, the compiler simply replaces the macro. Like what:

#define SUNDAY 7

In the pre-compilation phase, wherever the text appears Sunday, the compiler will replace it with 7来. The benefits are obvious: increase the readability of the program.

/************************************************************************************************************** /

Const: The secondary keyword is more complex and can be understood as "lock" the variable, and the const-modified variable becomes a read-only variable. The main difference between cosnt and define is that const is used to modify variables, and since they are variables, the compiler needs to type check them. This will be more secure.

The const is easy to confuse, not easy to distinguish who is locked. For example:

......................................................................................................................... ......................................................................

|     const char *p = "hello,world!"; The const is to lock the *p, namely: *p (Hello,world) is already a constant and cannot be modified. |

|   char Const *P = "hello,world!"; The const is to lock the *p, namely: *p (Hello,world) is already a constant and cannot be modified. |

|   /* If there is: p[0] = ' a '; You will get an error: Error:assignment of read-only location *p * * |

......................................................................................................................... ......................................................................

|                                                                                                                                                     Const (char*) p = q; |

|    char* const p = q; /* Lock p at this time P is not modified */|

| /* If there is: p = Q1;                                                                               Then: error:assignment of read-only variable ' p ' */ |

......................................................................................................................... ....................................................................

/************************************************************************************************************** ******/

Enum:

To create an enumeration type:

Method One:

typedef enum {

SUNDAY,

MONDAY,

.......

.......

.......

Satursday} week;

Week My_week = SUNDAY;

Method Two:

    

enum {

SUNDAY,

MONDAY,

.......

.......

.......

Satursday} week;

Enum Week my_week = SUNDAY;

Fyi:enum is equivalent to defining a lot of macros at once.

/************************************************************************************************************** *****/

Define vs const VS enum

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.