C# Constants and Literals

Introduction

Constants refer to fixed values that the program cannot alter during its execution. These fixed values are also called literals. Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal. There are also enumeration constants as well.

The constants are treated just like regular variables except that their values cannot be modified after their declaration.

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 no prefix is required for decimal numbers.

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.

Here are some examples of integer literals.

Integer Literal Sequence Meaning
212 /* Legal */
215u /* Legal */
0xFeeL /* Legal */
078  /* Illegal: 8 is not an octal digit */
032UU /* Illegal: cannot repeat a suffix (U is repeated)*/

The following are other examples of various types of integer literals:

Integer Literal Sequence Meaning
85 /* decimal literal*/
0213  /* octal literal */
0x4b /* hexadecimal literal */
30 /* int literal */
30u /* unsigned int literal */
30l /* long literal */
30ul /* unsigned long literal */
 
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.

Here are some examples of floating-point literals:

Floating-Point Literals Sequence Meaning
Double f=3.14159 /* Legal */
Double f=314159E-5 /* Legal */
Double f=510E /* Illegal: incomplete exponent */
Double f=210f /* Legal */
Double f=.e55 /* Illegal: missing integer or fraction */

Character Constants

Character literals are enclosed in single quotes, for example, 'x' and can be stored in a simple variable of char type. A character literal can be a plain character (for example, 'x'), an escape sequence (for example, ' \t'), or a universal character (for example, '\u02B0').

There are certain characters in C# that, when preceded by a backslash, will have a special meaning that they are used to represent. For examle newline (\n) and tab (\t). The following is a list of some of the escape sequence codes:

Escape Sequence Meaning
\\ \ character
\' ' character
\" " character
\? ? character
\a Alert or bell
\b Backspace
\f Form feed
\n Newline
\r Carriage return
\t Horizontal tab
\v Vertical tab
\ooo Octal number of one to three digits
\xhhh . . . Hexadecimal number of one or more digits

the following is the example to show a few escape sequence characters:

  1. static void Main(string[] args)  
  2. {  
  3.   Console.WriteLine("Hello\tWorld\n\n\n");  
  4.   Console.ReadLine();  
  5. }  

The output will be:



String Literals

String literals or constants are enclosed in double quotes "" or with @"". A string contains characters that are similar to character literals: plain characters, escape sequences and universal characters.

You can break a long line into multiple lines using string literals and separating the parts using whitespaces.

Here are some examples of string literals. All the three forms are identical strings.

Exa1: "hello, world"
Exa2: "hello, \
world"
Exa3: "hello, " "dear" "world"
Exa4: @"hello world"

Defining Constants

Constants are defined using the const keyword. The following is the syntax for defining a constant:

const <data_type> <constant_name> = value;

The following program shows defining and using a constant in your program:

  1. static void Main(string[] args)  
  2. {  
  3.   const double pi = 3.14159; // constant declaration   
  4.   double r;  
  5.   Console.WriteLine("Enter Radius: ");  
  6.   r = Convert.ToDouble(Console.ReadLine());  
  7.   double areaCircle = pi * r * r;  
  8.   Console.WriteLine("Radius: {0}, Area: {1}", r, areaCircle);  
  9.   Console.ReadLine();  
  10. }  

When the preceding code is compiled and executed, it produces the following result:

Enter Radius:
3
Radius: 3, Area: 28.27431


Recommended Free Ebook
Similar Articles