The world of C language (3)

​​​​​​​Foreword

Hello, C language! (constant)



1. What are constants?

A constant is an unchanging quantity.



The type of constant

1. Literal constants

2. The "variable" value modified by const

3. Constants defined by #define

4. Enumeration constants

3. Detailed explanation of constants

1. Literal constants:

A literal constant is a quantity that we can tell at a glance that it is a constant.

For example: 1 2 3 4 5.

2. The "variable" value modified by const

const is a keyword specified in the C language, representing a constant property.

But the variable modified by it is called a constant variable. Why is there a variable behind the constant?

The variable modified by const becomes an unmodifiable box, and the contents of the box have been locked with the box.

In a program, a const-modified variable is a variable with constant attributes, and its value cannot be changed.

We can use the array to verify this.

#include<stdio.h>
int main()
{    
    const int n=10;
    int a[n] ={1,2,3,4,5,6,7,8,9,10};
    int i=0;
    for(i=0;i<n;i++)
{
        printf("%d",a[i]);//循环输出数组a的值
}

    return 0;
}


 We can see that the compiler reported an error, indicating that the constant modified by const is no longer a constant, but has become a constant variable.

3. Constants defined by #define

#define is a precompiled instruction of the compiler. In the C language, a string is allowed to be defined by an identifier.

And throw away "#", define is a macro definition command.

We can think of define as a simple text replacement, that is, if we see the identifier behind define in the program, then it will be automatically replaced with the string defined by define, and let’s explain it with code.

#include<stdio.h>
int main()
{
    #define A 100
    int F=0;
    F=A;
    printf("%d",F);
    return 0;
}

A here is equivalent to 100 

All the A's we see in this program are equivalent to 100.

4. Enumeration constants (enum)

Speaking of enumeration , it is incremented by 1.

For example: starting from 0, the output is 0 1 2 3 .....

Then we literally look at enum constants like this.

Enumeration constants can define an initial value, and then increment by 1 from the initial value to the value we specify to stop.

Or use code to illustrate:

#include<stdio.h>
int main()
{
    enum NAME
{
    UP,
    DROP
};
printf("%d",UP);

    return 0;
}

From the code, it is clear that each quantity in the enumeration constant has a value, but this value starts from 0 by default, we can also define it ourselves, just change the UP in this program to to UP=3 (the value where you want it to start).

How enum constants are defined:

enum    //类型名称
{
               //取值名称

};//分号别忘记

//比如
enum WEEKDAY
{
    MON,
    TUE,
    WED,
    THU,
    FRI,
    SAT,
    SUN
};

The default type of WEEKDAY here is an integer. It can be seen that the type defined by enum is an integer by default.

Thank you for reading this. I am a college student majoring in computer science. I have just stepped into the world of C language. It is inevitable that there will be mistakes in the blog post. I hope everyone can correct me.

Guess you like

Origin blog.csdn.net/m0_60653728/article/details/121926172