Explanation
A constant is a value that doesn't change during the execution of a program. Constants are used to represent fixed values, such as numbers, characters, or strings, and they are essential for making programs more readable and maintainable. C language supports various types of constants, including:
Integer Constants:
-
Integer constants are whole numbers without a decimal point.
-
They can be represented in decimal (base 10), octal (base 8), or hexadecimal (base 16) format.
-
Examples: 42, 0x2A (hexadecimal), 052 (octal).
Floating-Point Constants:
-
Floating-point constants represent real numbers with a decimal point.
-
They can be written in decimal notation with or without an exponent.
-
Examples: 3.14, -0.001, 2.0e-3 (scientific notation).
Character Constants:
-
Character constants are enclosed in single quotes (' ').
-
They represent a single character or an escape sequence.
-
Examples: 'A', '\n' (newline), '\t' (tab).
String Constants:
-
String constants are enclosed in double quotes (" ").
-
They represent sequences of characters.
-
Example: "Hello, World!"
Enumeration Constants:
-
Enumeration constants are user-defined constants created with the enum keyword.
-
They provide a way to create symbolic names for integer values.
-
Example:
enum Day { SUN, MON, TUE, WED, THU, FRI, SAT };
Macro Constants:
-
Macro constants are defined using the ‘#define’preprocessor directive.
-
They are typically used for defining constants that are replaced with their values during preprocessing.
-
Example:
#define PI 3.14159265359
Constant Pointers:
-
Pointers that point to constant values or memory locations.
-
They are declared using the ‘const’ keyword.
-
Example:
const int* ptr; // Pointer to a constant integer.
-
Literal Constants:
-
Literal constants are used to represent special values like NULL or '\0' (null character).
-
NULL is often used to represent a null pointer.
-
'\0' is the string terminator in C.
-
-
Boolean Constants:
-
In C99 and later, _Bool is a data type used for representing Boolean values.
-
Constants 0 and 1 can be used to represent false and true.
-