How to define constants for C programs

Mondo Technology Updated on 2024-02-01

1. Overview

There are two main forms of definition of constants in C++ programmingdefine macro definitions and const keyword definitions

For more information about how to declare and use the define macro, see the article: How to use define in C. This article focuses on the use of the const keyword.

The const keyword is an abbreviation of the English word constant, and the Chinese meaning constant is constant. In C++ programming syntax, the const word is used as a modifier, and variables that are decorated by the const keyword are interpreted by the compiler as constants.

2. Use

The use of const keywords is relatively simple, just asPrefixBefore loading the variable declaration, therefore, the syntax format for declaring constants using the const keyword is:const [data_type] name = value, e.g. const int num =12. When declaring constants, in order to distinguish them from variables,It is recommended to capitalize the name of the constant.

The data types that can be decorated with const can be built-in in C++, such as int, double, and string;It can also be a custom data type, mainly a variable declared using a custom class.

In order to meet the requirements of constants, the constants declared by the const keyword are not allowed to be modified after the declaration is completed, so it is used in specific programmingThe const keyword must be assigned a value when declaring constants, and pay attention not to re-assign the value when using it in the future, otherwise the compilation will fail.

The following is an example:

Depending on the position of the declaration, constants are divided into:Local constants and global constants

1. Local constants

For example, a constant declared in a method body can only function in the method body where the declaration is made, and will be invalid if the method body is out.

More generally, local constant declarations are made in curly braces, such as in the if condition judgment block and in the for loop block.

For the use of local constants,Be sure to pay attention to the scope scope, at the same time,It is recommended to declare local constants with the const keyword.

2. Global constants

Typically, in the source file header, i.e. outside all method bodies and classes. The scope of the global constant, in the same source file, is followed by the declaration statementIn different source files, the area after the file introduction statement is.

While the const keyword can also implement the declaration of global constants, from a canonical programming perspectiveIt is recommended to use the define keyword to declare global constants as macros

Since constants are divided into global constants and local constants, no matter what type of constant is declared, you need to pay attention to whether the constant names are conflicting, so as to avoid scope conflicts caused by the duplication of constant names.

In C++ programming, the declaration definition of constants is mainly implemented through the define and const keywords. In this article, we recommend using the define macro declaration to implement the claim definition of global constants, and the const keyword to implement the claim definition of local variables.

In addition, for the data that does not need to be modified, whether it should be declared as a constant, considering the frequency of use of the data, if the data is only used in a certain row, it is not recommended to declare it as a constant, and it can be used directly. Conversely, if the data is used frequently, it is advisable to declare it as a constant. Whether it is declared as a local constant or a global variable is determined according to the scope of useIf the range is small, it will be declared as a local variable, and if the range is large, it will be declared as a global constant.

Related Pages