C Constants
Constants
Constants refer to fixed values that the program may not alter during its execution. These fixed values are also called literals.
Integer Literals
An integer literal can be a decimal, octal, or hexadecimal constant. A prefix specifies the base or radix: 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal.
An integer literal can also have a suffix that is a combination of U and L, for unsigned and long, respectively. The suffix can be uppercase or lowercase and can be in any order.
Floating-point Literals
A floating-point literal has an integer part, a decimal point, a fractional part, and an exponent part. You can represent floating point literals either in decimal form or exponential form.
Character Constants
Character literals are enclosed in single quotes, e.g., 'x' can be stored in a simple variable of char type.
A character literal can be a plain character (e.g., 'x'), an escape sequence (e.g., '\t'), or a universal character (e.g., '\u02C0').
String Literals
String literals or constants are enclosed in double quotes "". A string contains characters that are similar to character literals: plain characters, escape sequences, and universal characters.
List of Constants in C
Constant | Example |
---|---|
Decimal Constant | 10, 20, 450 etc. |
Real or Floating-point Constant | 10.3, 20.2, 450.6 etc. |
Octal Constant | 021, 033, 046 etc. |
Hexadecimal Constant | 0x2a, 0x7b, 0xaa etc. |
Character Constant | 'a', 'b', 'x' etc. |
String Constant | "c", "c program", "c in javatpoint" etc. |
Defining Constants
There are two simple ways in C to define constants −
- Using #define preprocessor.
- Using const keyword.
1.The #define Preprocessor
The #define preprocessor is also used to define constant. Given below is the form to use #define preprocessor to define a constant
#define identifier value
The following example explains it in detail:
2.The const Keyword
The const keyword is used to define constant in C programming. You can use const prefix to declare constants with a specific type as follows −
const type variable = value;
The following example explains it in detail: