Constants are fixed values that do not change during the execution of a program. C supports several types of constants, including integer constants, floating-point constants, character constants, string constants, enumeration constants, and symbolic constants.
Integer constants are whole numbers without a fractional part. They can be specified in decimal, octal, or hexadecimal form:
Type | Example | Description |
---|---|---|
Decimal | 42 |
Base 10 (e.g., 42 ) |
Octal | 052 |
Base 8 (e.g., 052 represents decimal 42 ) |
Hexadecimal | 0x2A |
Base 16 (e.g., 0x2A represents decimal 42 ) |
Floating-point constants represent real numbers and include a decimal point or an exponent. Examples:
Type | Example | Description |
---|---|---|
Decimal | 3.14 |
Standard decimal notation |
Exponential | 2.5e3 |
Represents 2.5 × 103 (i.e., 2500 ) |
Character constants represent single characters enclosed in single quotes. Examples:
String constants are sequences of characters enclosed in double quotes. Examples:
Enumeration constants are a set of named integer constants. They are defined using the enum
keyword. For example:
In this example, RED
, GREEN
, and BLUE
are enumeration constants that correspond to the values 0
, 1
, and 2
respectively.
Symbolic constants are defined using the #define
preprocessor directive. They are used to give a name to a constant value. For example:
In this example, PI
and MAX_LENGTH
are symbolic constants.
Here are some examples of how these constants can be used in a C program: