Loading, please wait...

A to Z Full Forms and Acronyms

What are constant and literal. Explain different types of constants available in C#.

Jun 24, 2021 Constant, Literal, 5418 Views
In this article you will learn about constant and literal

Define constant and literals.

Explain different types of constant available in C#

Literal/Constant

Constant is a quality that does not change. Constant in C# refer to fixed values that do not change during the execution of a program.

In C#, it can be declared as follows:

access-modifiers const data-type constant-name= value;

for example public const int A=10;

Types of Constant in C#

The following types of constants are supported by C#

1. Boolean

2. Numeric

3. character

4. String

5. null

1. Boolean:- Boolean types represent a true or false type value. The value true and false are the two possible Boolean constant values. Numeric values such as 0 and 1 can not be treated as false and true respectively. The boolean type does not accept any numeric value as its value.

2. Numeric:- Numeric constant may be an integral type or real type:-

a) Integral Constant: An integral constant refers to a sequence of digits without a decimal point. An integral constant is one of the following types: byte, byte, short, int, uint, long, long, and char. Comma and blank space cannot be included within integral constants. An integral constant may be preceded by a plus sign if desired, or a minus sign. If a sign does not appear, the integral value will be assumed to be positive. Integral values should not go beyond the maximum and minimum values that can be represented by the specified number of bits used by the system for each integral data type representation. An integral constant may end with l or L representing long integer, u or U representing an unsigned integer.

The integral constant may be represented using decimal digits or hexadecimal digits.

i) Decimal integral Constant: A decimal integral constant is formed using the digits 0 through 9. a decimal value represents a number system using Base 10.

The following are valid decimal integral constant:

35 -454 435926U

on the other hand, the following are not valid decimal integral constant:

54.4 Contain a decimal point.

34,45 Contain a comma.

56 56 blank spaces are not allowed within an integer constant.

ii) Hexadecimal integral Constant: A hexadecimal integral constant is formed from the hexadecimal number system 0 through 9 and A through F (either uppercase or lowercase), leading with 0x and 0X. The character A through F represent the values 10 through 15.

The following are valid hexadecimal integral constant:-

0x6 0x6ac -0XABCD

On the other hand, the following are not valid hexadecimal integral constant:-

55 not leading with 0X or 0X.

0Xabh Only A through F and 0 through 9 are allowed.

0X.6af Decimal point is not allowed.

b) Real (floating-point) Constant: Any number with a decimal point is called a real constant or a floating-point constant. It can be written in exponential form also. An exponent is written as an e of E followed by a positive or negative integer when a floating-point constant contains an exponent, its value is obtained by multiplying the value preceding the exponent by 10 to the power of the integer given in the exponent. The value preceding the exponent is known as mantissa. For example, 3.5e6(3.5 is the mantissa part and e6 is the exponent part) is equivalent to 3.5*106.

Rules for real constant are:

  • The mantissa is either a real number expressed in decimal notation or an integer.

  • An exponent is always an integer number with an optional plus or negative sign.

  • A real constant may start with a decimal point(.) but can not end with it. Pone or more digits must follow the decimal point.

  • The mantissa part and the exponent part should be separated by letter ‘e’.

  • special character except for e, E, + and – are not allowed.

  • Floating constant should not exceed the specified limits of the values of the system used.

 

c) Decimal Constant: C# provides a special data type called decimal. It denotes the 128-bits data type. Compared to floating-point types, The decimal type has a greater precision and a smaller range which makes it suitable for financial and monetary calculations. The decimal numbers can range from 1.0*10-18 to 7.9*1028 with 28 to 29 digits precision. It provides exact precision compared to float or double type avoiding round-off errors that may occur in currency calculations to specify a number to be of decimal type, one must append the character M or m to the value such as 3456.25. If one omits M, the value will be treated as double. Decimal constant may also use exponent nation as

15E22M, 15.24E11M etc.

3. Character Constant: A character written within single quotes is called a character constant. The character may be the letter, number, or special character.

Rules for character constant:

  • A character from the character set of C# can be enclosed in single quotes.

  • An escape sequence can be enclosed in single quotes.

  • More than one character is not allowed in a character constant. An escape sequence consists of more than one character to represent a single character. Hence, it is treated as a single character.

  • At least one character is written in a single quote.

 

4. String Constant:C# supports two types of the string constant:

a) The regular string constant: A regular string constant is a sequence of zero or more character enclosed within double quotes such as “India”, “Ramsingh”, “L42” or the empty string “". the quotes are not the part of the string.

b) The verbatim string constant: A verbatim string constant is a pre-fixed with the @ character. Any escape sequence in such a string denotes itself as such and not processed. The only exception to this is two consecutive double quotes (“ ”). if “” occurs in between the verbatim string constant, it displays a single character “. the character “” is known as a quotes escape sequence in the context of the verbatim string constant.

Valid verbatim string constants Display

@ “welcome to\t C#” welcome to \t C#

@ “welcome to\t C#” welcome to \n C#

@ “welcome to “ ”\t C# “”  welcome to “C#”

Invalid Verbatim String constants

“Welcome to \t C#” It is a regular string constant.

5. Null Constant: The null keyword is a constant used to indicate that a variable does not hold a known value. It is used only with a reference type and not with a value type.



A to Z Full Forms and Acronyms