C# | literals

IN this post we will learn about C# Literals. The fixed values assigned to a variable are called as Literal. Literal is a value that is used by the variables. Values can be either an integer, float or string, etc. 
// In below example 1010 is a constant/literal.
int x = 1010;

Literals can be of the following types:

  • Integer Literals
  • Floating-point Literals
  • Character Literals
  • String Literals
  • Null Literals
  • Boolean Literals

Integer Literals:Literals of type Integer are called integer literals. It can be an octal, decimal, binary, or hexadecimal constant. Decimal numbers do not require a prefix. Suffixes can also be used with integer literals. U or u for unsigned numbers, l or L for long numbers. By default, all literals are of type int. For integer data types (Byte, Short, Int, Long).

We can specify literals in the ways:

  • Decimal literals (Base 10): In this form, the allowed digits are 0-9.
int x = 101;
  • Octal literals (Base 8): In this form, the allowed digits are 0-7.
// The octal number should be prefix with 0.
int x = 0146;
  • Hexa-decimal literals (Base 16): In this form, the allowed digits are 0-9 and characters are a-f. We can use both uppercase and lowercase characters. As we know that C# is a case-sensitive programming language but here in current case C# is not case-sensitive.
// The hexa-decimal number should be prefix
// with 0X or 0x.
int x = 0X123Face;
  • Binary literals (Base 2): In this form, the allowed digits are only 1’s and 0’s.
// The binary number should be prefix with 0b.
int x = 0b101

Examples:

07778    // invalid: 8 is not an octal digit 
045uu    // invalid: suffix (u) is repeated.
0b105    // invalid: 5 is not a binary digit.
0b101    // valid binary literal
456      // valid decimal literal
02453    // valid octal literal 
0x65d    // valid hexadecimal literal 
12356    // valid int literal 
304U     // valid unsigned int literal 
3078L    // valid long literal 
965UL    // valid unsigned long literal

Program: C#

// C# program to illustrate the use of Integer Literals
using System;
 class CodeConfig{
    // Main method
    public static void Main(String[] args)
    {
        // decimal-form literal
        int a = 1010;
        // octal-form literal
        int b = 0146;
        // Hexa-decimal form literal
        int c = 0xFace;
      
        // binary-form literal
        int x = 0b101;
        
        Console.WriteLine(a);
        Console.WriteLine(b);
        Console.WriteLine(c);
        Console.WriteLine(x);
    }
}
1010
146
64206
5

Floating-point Literals: The literal which has an integer part, a decimal point, a fractional part, and an exponent part is known as the floating-point literal. These can be represented either in decimal form or exponential form.

Examples:

Double d = 3.14145       // Valid
Double d = 312569E-5      // Valid
Double d = 125E             // invalid: Incomplete exponent 
Double d = 784f            // valid
Double d = .e45           // invalid: missing integer or fraction

Program: C#

// C# program to illustrate the use of
// floating-point literals
using System;
class CodeConfig{
    // Main Method
    public static void Main(String[] args)
    {
        // decimal-form literal
        double a = 1010.230;
        // It also acts as decimal literal
        double b = 01234.222;
        Console.WriteLine(a);
        Console.WriteLine(b);
    }
}
Output: 
101.23
1234.222

Note: By default, every floating-point literal is of double type and hence we can’t assign directly to float variable. But we can specify floating-point literal as float type by suffixed with f or F. We can specify explicitly floating-point literal as the double type by suffixed with d or D, of course, this convention is not required.

Character Literals: For character data types we can specify literals in 3 ways:

  • Single quote: We can specify literal to char data type as single character within single quote.
char ch = 'a';
  • Unicode Representation: We can specify char literals in Unicode representation ‘\uxxxx’. Here xxxx represents 4 hexadecimal numbers.
char ch = '\u0061';// Here /u0061 represent a.
  • Escape Sequence: Every escape character can be specified as char literals.
char ch = '\n';
Escape Sequence Meaning
\\ \ character
\’ ‘ character
\? ? character
\” ” character
\b Backspace
\a Alert or Bell
\n New Line
\f Form Feed
\r Carriage Return
\v Vertical Tab
\xhh Hexadecimal number of one or more digits

Example: C#

// C# program to illustrate the use of char literals
using System;
class CodeConfig{
    // Main Method
    public static void Main(String[] args)
    {
        // character literal within single quote
        char ch = 'a';
        // Unicode representation
        char c = '\u0061';
        Console.WriteLine(ch);
        Console.WriteLine(c);
        // Escape character literal
        Console.WriteLine("Hello\n\nWorld\t!");
    }
}
a
Hello
World    !

String Literals: Literals which are enclosed in double quotes(“”) or starts with @”” are known as the String literals.

Examples:

String s1 = "Hello World!";

String s2 = @"Hello World!";

Program: C#

// C#  program to illustrate the use of String literals
using System;
class CodeConfig{
    // Main Method
    public static void Main(String[] args)
    {
        String s = "Hello World!";
        String s2 = @"Hello World!";
        // If we assign without "" then it
        // treats as a variable
        // and causes compiler error
        // String s1 = HelloWorld;
        Console.WriteLine(s);
        Console.WriteLine(s2);
    }
}
Output: 
Hello World!
Hello World!

Boolean Literals: Only two values are allowed for Boolean literals i.e. true and false.

Example:

bool b = true;
bool c = false;

Program: C#

// Below is C# program to illustrate the use
// of boolean literals
using System;
class CodeConfig{
    // Main Method
    public static void Main(String[] args)
    {
        bool b = true;
        bool c = false;
        // these will give compile time error
        // bool d = 0;
        // bool e = 1;
        // Console.WriteLine(d);
        // Console.WriteLine(e);
        Console.WriteLine(b);
        Console.WriteLine(c);
    }
}
Output: 
True
False

In this article we tried to explain C# | literals with Examples. I hope you enjoyed reading this article. For more information you can check out Microsoft learn.

For more information you can visit our C# Section for more such articles.