In C, constants are values that remain fixed and cannot be altered during the execution of a program. They are used to represent fixed values such as numbers, characters, and strings. Constants provide a way to store data that does not change throughout the program's execution.
There are several types of constants in C:
1. Integer Constants: Integer constants represent whole numbers without a fractional part. They can be specified in decimal, octal, or hexadecimal format. For example:
Decimal: 42
Octal: 052 (prefix 0 indicates octal)
Hexadecimal: 0x2A (prefix 0x indicates hexadecimal)
Rules for constructing integer constants
An integer constant in C is a number that does not have a decimal point. It can be either positive or negative. The allowable range for integer constants is -32768 to 32767.
The rules for constructing integer constants in C are as follows:
The constant must have at least one digit.
The constant must not have a decimal point.
The constant can be either positive or negative.
The constant cannot contain any commas or blanks.
For example, the following are all valid integer constants in C:
123
-456
0
The following are all invalid integer constants in C:
123.45
123,456
-123 456
Integer constants can be used in a variety of ways in C programs. They can be used to initialize variables, to perform calculations, and to compare value.
int my_number = 123;
int result = my_number * 10;
if (my_number == 123)
{
// Do something
}
2. Floating-Point Constants: Floating-point constants represent real numbers with a fractional part. They are typically written with a decimal point.For example:
Decimal notation: 3.14
Scientific notation: 6.02e23 (6.02 multiplied by 10 raised to the power of 23)
Rules for constructing floating-point constants
Floating-point constants in C are numbers that have a decimal point. They can be either positive or negative. The allowable range for floating-point constants is -3.402823466e+38 to 3.402823466e+38.
The rules for constructing floating-point constants in C are as follows:
- The constant must have at least one digit.
- The constant must have a decimal point.
- The constant can be either positive or negative.
- The constant cannot contain any commas or blanks.
For example, the following are all valid floating-point constants in C:
123.45
-456.78
0.0
The following are all invalid floating-point constants in C:
123
-456
123,456
Floating-point constants can be used in a variety of ways in C programs. They can be used to initialize variables, to perform calculations, and to compare values.
Here are some examples of how floating-point constants can be used in C programs:
float my_number = 123.45;
float result = my_number * 10;
if (my_number == 123.45) {
// Do something
}
In addition to the basic rules for constructing floating-point constants, there are a few other things to keep in mind:
- Floating-point constants can be suffixed with the letters f or F to indicate that they should be stored as single-precision floating-point numbers.
- Floating-point constants can be suffixed with the letters l or L to indicate that they should be stored as long double floating-point numbers.
- Floating-point constants can be suffixed with the letters e or E to indicate that they should be stored as exponential floating-point numbers.
For example, the following are all valid floating-point constants in C:
123.45f
-456.78l
0.0e-10
The following are all invalid floating-point constants in C:
123.45fF
-456.78lE
0.0e-10f
Note
It is important to note that the range of values that can be represented by floating-point constants is much smaller than the range of values that can be represented by integer constants. This is because floating-point numbers are stored in binary, while integer numbers are stored in decimal. As a result, floating-point numbers are subject to rounding errors.
3. Character Constants: Character constants represent individual characters enclosed in single quotes (''). For example:
'A'
'5'
'\n' (newline character)
The following are the rules for character constants:
- A character constant can be any single character from the execution character set.
- A character constant can be preceded by a backslash (\) to introduce an escape sequence.
- An escape sequence is a sequence of characters that represent a single character.
- The value of a character constant is the numerical value of the character that is represented by the constant.
- The type of a character constant is int.
The following are the escape sequences that are supported by C:
Symbol | Explaination |
\a |
Bell |
\b |
Backspace |
\f |
Form feed |
\n |
Newline |
\r |
Carriage return |
\t |
Tab |
\v |
Vertical tab |
\0 |
Null character |
\xhh |
Hex character, where hh is a two-digit hexadecimal number |
\uhhhh |
Unicode character, where hhhh is a four-digit hexadecimal number |
Here are some examples of character constants:
'A'
'\n'
'\t'
'\0'
'\x41'
'\u0041'
The first example, 'A', represents the character A.
The second example, '\n', represents the newline character.
The third example, '\t', represents the tab character.
The fourth example, '\0', represents the null character.
The fifth example, '\x41', represents the character A in hexadecimal.
The sixth example, '\u0041', represents the character A in Unicode.
4. String Constants: String constants represent sequences of characters enclosed in double quotes (""). They are treated as arrays of characters in C. For example:
"Hello, World!"
"OpenAI"
The following are the rules for string constants:
- A string constant can be any sequence of characters from the execution character set.
- A string constant can be preceded by a backslash () to introduce an escape sequence.
- An escape sequence is a sequence of characters that represent a single character.
- A string constant can be enclosed in double quotes (" ").
- The value of a string constant is a pointer to the first character in the string.
- The type of a string constant is char *.
5. Symbolic Constants: Symbolic constants are user-defined constants that are given meaningful names using the #define preprocessor directive. They are typically used to assign names to fixed values, making the code more readable and maintainable.
The syntax for a #define directive is as follows:
#define identifier value
identifier - is the name of the symbolic constant
value - is the constant value.
For example:
#define PI 3.14159
#define MAX_VALUE 100
The following are the rules for symbolic constants:
- The identifier must be an identifier that follows the rules for identifiers in C.
- The value can be any constant value that is valid in C.
- The #define directive must be placed before the first use of the symbolic constant in the program.
- The #define directive can be used to define multiple symbolic constants on a single line.
- The #define directive can be used to define symbolic constants that are macros.
6. Enumerated Constants: Enumerated constants, or enums, are user-defined types that allow you to define a set of named constants. Each constant in an enum is assigned an integer value, starting from 0 by default. Enumerated constants provide a way to create named groups of related values. For example:
enum Color { RED, GREEN, BLUE };
The syntax for an enum declaration is as follows:
enum identifier {
constant-1,
constant-2,
...
constant-n
};
where identifier is the name of the enumerated type, constant-1, constant-2, ..., constant-n are the names of the enumerated constants, and n is the number of enumerated constants.
The following are the rules for enumerated constants:
- The identifier must be an identifier that follows the rules for identifiers in C.
- The constant-1, constant-2, ..., constant-n must be identifiers that follow the rules for identifiers in C.
- The constant-1, constant-2, ..., constant-n must be unique.
- The enum declaration must be placed before the first use of the enumerated constants in the program.
Here are some more examples of enumerated constants:
enum Color {
RED,
GREEN,
BLUE
};
enum Day {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY
};
The first example defines an enumerated type named Color with three enumerated constants: RED, GREEN, and BLUE. The second example defines an enumerated type named Day with seven enumerated constants: MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, and SUNDAY.
Enumerated constants can be used in a variety of ways in C. For example, they can be used to make code more readable, to make code more portable, and to make code more efficient.
Note
- Constants in C are typically declared and defined at the beginning of a program or within a specific scope, depending on their usage.
- Once defined, their values cannot be changed during program execution, providing a way to ensure data integrity and facilitate code maintenance.