The fixed values are called as Literal. Literal is a value which is used by the variables. Values can be either an integer, float or string etc.
// Here 100 is a constant/literal. int x = 100;
Literals can be of following types:
- Integer Literals
- Floating-point Literals
- Character Literals
- String Literals
- Null Literals
- Boolean Literals
Integer Literals: A literal of integer type is know as the integer literal. It can be octal, decimal or hexadecimal constant. No prefix is required for the decimal numbers. A suffix can also be used with the integer literals like U or u are used for unsigned numbers while l or L are used for long numbers. By default, every literal is of int type. For Integral data types (byte, short, int, long), we can specify literals in 3 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 c# is not case-sensitive.
// The hexa-decimal number should be prefix // with 0X or 0x. int x = 0X123Face;
07778 // invalid: 8 is not an octal digit 045uu // invalid: suffix (u) is repeated 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
101 145 64206
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.
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
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 specify 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
a a Hello Geeks !
String Literals: Literals which are enclosed in double quotes(“”) or starts with @”” are known as the String literals.
String s1 = "Hello Geeks!"; String s2 = @"Hello Geeks!";
Hello Geeks! Hello Geeks!
Boolean Literals: Only two values are allowed for Boolean literals i.e. true and false.
bool b = true; bool c = false
- C# Coding Standards
- while Loop in C#
- Type System Unification in C# .NET
- Garbage Collection in C# | .NET Framework
- Difference between Boxing and Unboxing in C#
- C# | Version History
- Interesting Facts about C#
- C++ vs C#
- Introduction to .NET Framework
- Introduction to Visual Studio
- Common Language Runtime (CLR) in C#
- Main Method in C#
- Introduction to C# Windows Forms Applications
- C# | Default Constructor
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.