C constant

Introduction to handling constants in C language

In the previous article, I introducedVariables in C.

In this article, I want to tell you all about C constants.

The declaration of a constant is similar to the declaration of a variable, except that the constant is preceded byconstKeyword, then you always need to specify a value.

like this:

const int age = 37;

This is perfectly valid C language, although constants are usually declared in uppercase as follows:

const int AGE = 37;

This is just a convention, but it can be a great help when reading or writing this bookCProgram because it improves readability. Uppercase names indicate constants, lowercase names indicate variables.

Constant names follow the same variable name rules: they can contain any uppercase or lowercase letters, they can contain numbers and underscore characters, but they cannot start with a number.AGEwithAge10Is a valid variable name,1AGEIt's not.

Another way to define constants is to use the following syntax:

#define AGE 37

In this case, you don’t need to add types, and you don’t need to=The equal sign, and then omit the semicolon at the end.

The C compiler will infer the type based on the specified value at compile time.

Download mine for freeC manual


More clang tutorials: