Together talk C Chestnut Bar (165th: C language Instance--c constant in language)

Source: Internet
Author: User

Ladies and gentlemen, crossing, the last time we talked about the Const keyword example, this time we say the example is:C language Constants . Gossip Hugh, words return to the positive. Let's talk C chestnuts together!

Crossing, we introduced the Const keyword in the last time, and we all know that const modifiers are constants. Referring to constants, we often use them in our programs, and in this case we look at other constants in the C language.

As we all know, the value of the constant is fixed and cannot be modified. In the C language, there are four ways to implement constants, namely:

    • Literal constants
    • Macro
    • Const
    • Enumeration.

Next we describe the four ways to implement constants.

literal constants : Literal constants are values and strings that we use directly, for example:

int a=3char *str="hello"

The 3 and hello here are literal constants.

macro: A macro is primarily used for substitution, and if the content it replaces is literal, you can also use a macro as a way to implement a constant. For example:

#define SIZE 16; int a[SIZE];

We define the macro size as literal constant 16, which, when used, replaces the macro with 16, and the code in the example defines an array of type int of size 16.

enumeration: The members in the enumeration have specific values, in order to improve the readability of the code, we use the name of the enumeration member, but let us not forget that the enumeration member is a constant of type int, it has a specific value, which is similar to a macro. For example:

    enum    {        MON=1,        TUE,        WED,        THU,        FRI,    };      printf("today is Week: %d \n",FRI);

We can directly output the value of an enumeration member because it is a constant of type int. In the project, you often define some types of errors in the enumeration, and in other functions you can return the members of the enumeration. There are two benefits to doing so:

    • First: We can know the type of error through the member name, improve the readability of the Code;
    • Second: We can judge the function return value by the value of int type, and then determine the type of the error;

Const: We have already introduced it in the previous chapter, and we are not going to introduce it here.

Crossing, we have a summary of how this implements the constants:

    • Literal constants are not recommended, and if you want to use them, you can replace them with a macro or const constant.
    • For some consecutive constants, it is recommended to use enumerations, such as the Monday to Friday in our example.
    • For discontinuous constants, it is recommended to use a macro.
    • The most common use of const is to modify the pointer, which ensures that the value that the pointer refers to is not modified by the pointer in the program.

Everybody crossing, the example of the constant in C language we're going to talk about here. For further examples, listen to tell

Together talk C Chestnut Bar (165th: C language Instance--c constant in language)

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.