Types of Constants in C programming language
In the C programming language, constants refer to fixed values that are not expected to change during the execution of a program. There are several types of constants that can be used in C, including:
- Integer constants: These are whole numbers that can be positive, negative, or zero. They can be written in decimal (base 10), octal (base 8), or hexadecimal (base 16) notation. For example: 10, -5, 0xFF
- Floating-point constants: These are numbers with a decimal point, such as 3.14 or -0.01. They can be written in decimal or scientific notation.
- Character constants: These are single characters enclosed in single quotes, such as ‘A’ or ‘$’. They can also be written as an integer value in the ASCII table
- String constants: These are sequences of characters enclosed in double quotes, such as “Hello, World!”. They are stored as arrays of characters in memory.
- Enumeration constants: These are named constants that are defined using the
enum
keyword. For example, we can define a set of constants for the days of the week like this:
enum days {SUN, MON, TUE, WED, THU, FRI, SAT};
- Constant expressions: These are expressions that are evaluated at compile time and replaced with their resulting value. They can be used to define constant values that depend on other constants or expressions. For example:
#define PI 3.14
#define AREA(r) (PI * (r) * (r))
- Symbolic constants: These are named constants that are defined using the
#define
preprocessor directive. They are replaced with their corresponding value at compile time. Symbolic constants can be used to make code more readable and maintainable. For example:
#define MAX_SIZE 100
#define MIN_SIZE 10
int main() {
int size = 50;
if (size > MAX_SIZE) {
size = MAX_SIZE;
} else if (size < MIN_SIZE) {
size = MIN_SIZE;
}
// ...
}
Leave a Comment