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 byconst
Keyword, 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.AGE
withAge10
Is a valid variable name,1AGE
It'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:
- Introduction to C programming language
- C variables and types
- C constant
- C operator
- C conditional
- How to use loops in C
- Introduction to C arrays
- How to determine the length of an array in C
- Introduction to C strings
- How to find the length of a string in C
- C pointer introduction
- Iterate over the array in C
- Boolean in C
- Introduction to C functions
- How to use NULL in C
- Basic I/O concepts in C language
- Double quotes and single quotes in C
- How to return a string from a C function
- How to solve the implicit declaration library function warning in C language
- How to check character value in C
- How to use `printf()` to print percent characters in C language
- C conversion specifiers and modifiers
- How to access command line parameters in C
- Range of variables in C
- Can functions be nested in C?
- Static variables in C
- C global variables
- Typedef keyword in C language
- C enumeration type
- C structure
- C header file
- C preprocessor