Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand C language constant

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to understand the C language constant, I believe that many inexperienced people are at a loss about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

What is a constant?

Literally, a simple explanation is that a constant is called a constant.

What are the constants

Literal constant

Constant variables modified by const

# identifier constants defined by define

Enumerate the constant enum

The characteristics of these four constants, matters needing attention and so on. Literal constant:

A literal constant is a quantity written directly.

There are literal constants such as characters, numbers, strings, etc.

Constant variables modified by 2.const

To get to the point: variables modified by const can no longer be changed and have some common properties. In a nutshell, it's like a constant, but it's essentially a variable.

Const means constant attribute in English.

Const is the keyword of C language.

Variables modified with const cannot be changed, such as:

As shown in the figure, the variable num modified by const cannot be transformed to 20.

It says straight to the point:

Let's explain why being modified by const is not a constant in nature.

We all know that the size of the array is a constant, while the size of the arr array in the following figure is a variable n

Const can modify the originating variable and cannot be directly understood as a constant, as an example is as follows:

Suppose we just think that the variable modified by const becomes constant. If we run the following code, it should be correct and can be compiled successfully.

But it is wrong, which proves that our hypothesis is wrong, so we can't simply think that the variable modified by const is constant, it just has some constant attribute.

So n is a constant, not a constant.

Identifier constant defined by 3.#define

# the identifier constant defined by define is a constant, which is different from the constant modified by const

As follows:

4. Enumeration constant

The keyword of enumeration provided by C language: enum

What is an enumeration constant?

There are some values in our life that can be listed one by one.

For example, there are only two kinds of gender, which can be listed.

Such as week, Monday, Tuesday. On Sunday, you can come out one by one.

The format of the enumeration:

For example, the color enumeration:

Enum Colour

{

Yellow

Blue

While

}

Note that the semicolon after the curly braces cannot be lost

We also need to know that enumerations start at 0 by default.

From the above study, we should know that there is a difference between the constant modified by const and the identifier constant defined by # define

# the identifier constant defined by define is constant, while the constant modified by const is not constant

After reading the above, have you mastered how to understand C language constants? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report