HomeAbout Me

C# CONSTANTS AND LITERALS

By Daniel Nguyen
Published in WPF - CSharp
October 18, 2022
1 min read
C# CONSTANTS AND LITERALS

The constants refer to fixed values that the program may not 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 definition.

Integer Literals

Integer literals

212 / Legal /
215u / Legal /
0xFeeL / Legal /
078 / Illegal: 8 is not an octal digit /
032UU / Illegal: cannot repeat a suffix /

Various types of Integer literals

85 / decimal /
0213 / octal /
0x4b / hexadecimal /
30 / int /
30u / unsigned int /
30l / long /
30ul / unsigned long /

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.

Floating-point literals

3.14159 / Legal /
314159E-5L / Legal /
510E / Illegal: incomplete exponent /
210f / Illegal: no decimal or exponent /
.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 (such as ‘x’), an escape sequence (such as ‘\t’), or a universal character (such as ‘\u02C0’).

Here, is a list of some of such escape sequence codes

Escape sequenceMeaning
\\\ character
\’” character
\”” character
\?? character
\aAlert or bell
\bBackspace
\fForm feed
\nNewline
\rCarriage return
\tHorizontal tab
\vVertical tab
\oooOctal number of one to three digits
\xhhHexadecimal number of one or more digits

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.

“hello, dear”
“hello, \
dear”
“hello, ” “d” “ear”
@“hello dear”

Defining Constants

using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
// const <data_type> <constant_name> = value;
const double pi = 3.14159; // constant declaration
double r;
Console.WriteLine("Enter Radius: ");
r = Convert.ToDouble(Console.ReadLine());
double areaCircle = pi * r * r;
Console.WriteLine("Radius: {0}, Area: {1}", r, areaCircle);
Console.ReadLine();
//Enter Radius:
//3
//Radius: 3, Area: 28.27431
}
}
}

Tags

#CSharp

Share

Previous Article
C# OPERATORS
Next Article
C# COLLECTIONS

Table Of Contents

1
Integer Literals
2
Floating-point Literals
3
Character Constants
4
String Literals
5
Defining Constants

Related Posts

C# ARRAYS
January 05, 2024
1 min
© 2025, All Rights Reserved.
Powered By

Quick Links

About Me

Legal Stuff

Social Media