How are constants used in the C programming language?
There are several ways to use constants in the C programming language.
- Character constants are characters enclosed in single quotes, for example: ‘a’, ‘b’, ‘c’. They can be assigned directly to a character variable or used for comparison operations.
- String constants are a series of characters enclosed in double quotation marks, for example: “Hello, World!”. They can be assigned to character arrays or pointer variables.
- Integer constants can be represented in decimal, octal, or hexadecimal form, such as 10, 012, 0xA. They can be assigned to integer variables.
- Real number constants can be in decimal or exponential form, such as 3.14 or 1.23e-5, and can be assigned to floating-point variables.
- Enumerating constants: defining constant values within an enum type, for example:
enum colors {RED, BLUE, GREEN};
enum colors myColor = RED;
- Global constant: a constant defined using the #define preprocessing directive, for example:
#define PI 3.14159
float radius = 5.0;
float area = PI * radius * radius;
The above is the common usage of constants in the C language, choose the appropriate constant type for specific requirements.