C# Print Literals
C # Print Literal Values
Literal values are constant values that never change.
They include the following:
String literals
- String literals are used to print alphanumeric words, phrases, or data for presentation.
A string literal is created by enclosing a string in double quotation marks.
For Example:
Console.Write(“number”);
String literals cannot however be used for calculations.
Character Literals
- Character literal, denoted as char which is the short form of character is used to print single alphanumeric characters and is created by enclosing a character in single quotation marks.
For Example:
Console.Write(‘l’);
Note that if you use single quotation marks and input more than a single character, as, in the example below, you will encounter an error because the compiler expects a single character when single quotation marks are used.
For Example:
Console.Write(‘john’);
Output
(2,15): error CS1012: Too many characters in character literal
- Character literals cannot be used for calculations.
Integer Literals
- Integer literals are used to display numeric whole numbers.* They can however not be used to display fractions.* Integer literals are denoted as int when used in coding.* They require no other operator as is the case for string and character literals.
For Example:
Console.Write(123);
Floating literals
- A float-point number is a number that contains decimals e.g. 1.5023.* They are of three types with different levels of precision, namely.
Float Type | Precision |
Float | ~6-9 digits |
Double | ~15-17 digits |
Decimal | 28-29 digits |
- Precision indicates the number of digits on the right-hand side of the decimal point.
Now let us get to know each of the floating literals mentioned above.
Float
To create a float literal, you need to append a letter F after the number.
The letter F is called a literal suffix which tells the compiler to treat the value given as a float.
The letter F can either be in uppercase or lowercase.* Given float has the least precision, it is best to use it when dealing with fixed fractions.
For Example:
Console.Write(0.25f);
Decimal
This is the compiler's default when any decimal value is used, thus, to print a decimal, enter the decimal number without a literal suffix*.*
For Example:
Console.Write(0.256);
Double
- Just like a float, the compiler needs a literal suffix m to treat a value as a decimal.
Double has the highest precision and is thus good for fractions with a higher number of digits after the decimal point.
For Example:
Console.Write(24.6758m);
Boolean Literals.
- If you want to print a value that represents either true or false, then the bool literal is the correct literal to use.* The term bool is the short form of Boolean.
Example in code:
Console.Write(true);
Or
Console.Write(false);
- You will notice that when the term true or false is used without double quotation marks, as is the case when working with string literals*, there is no error encountered, this is because the two words are reserved to be used as* Boolean literals and the compiler understands that.
Recap
Here is a summary of what you need to know about C# print literals.
string
for words, phrases, or any alphanumeric data for presentation, not calculation.char
for a single alphanumeric character.int
for a whole number.decimal
for a number with a fractional component.bool
for a true/false value.