Constants and Literals


Constants and Literals

The constants relate to fixed values that cannot be altered during the program's execution. These values are also known as literal values. Constant types like an integer constant, a floating constant, a constant, or a literal string might be basic data types. There are also constants for enumeration.

The constants are treated like regular variables, except that their values cannot be changed after definition.

Integer Literals

A literal integer may be decimal or hexadecimal. The base or radix is specified in the prefix as 0X, and no prefix id for the decimal is present.

An integer literal can additionally have a suffix which is a U and L combination, for both unsigned and long. The suffix can be either uppercase or lowercase.

Floating-point Literals

A literal floating point has an integer, a decimal, a fractional, and an exponent, and they are either decimal or exponential to literal floating points.

You must include the decimal point, exponent, or both while representing the whole, fractional part, or both using an exponential form. e or E is presented to the signed exponent.

Character Constants

In single quotations, the literal of the character is included. For instance, 'x' can be kept in a single char type variable. A character literal may be a simple character (e.g. "x," an escape sequence (e.g., "\t"), or a general character (e.g., "\u02C0").

In the case of a backslash, there are some characters in C#. They have a specific significance and are used as a newline (\n) or tab (\t).

String Literals

In double quotes "" or with @," the string literal or constant is encapsulated. A string includes literal characters: simple, escape, and universal characters.