A constant is a named value whose value cannot be changed once assigned. It is defined using the const keyword and is used to store fixed values in a program. The fixed value assigned to a constant is called a literal.
- A constant is a named value that remains unchanged throughout the program execution.
- A literal is the actual fixed value written directly in the program, such as 10, 3.14, 'A', or "Hello".
// constant
const int c = 24;
int b = 100; // literal
Output
24 100
In the above code, the variable 'c' is a constant whose value cannot be changed. The value 100 assigned to variable b is a literal, which is directly written in the program.
Constants
A constant is a named value that remains unchanged throughout the program. It must be initialized at the time of declaration using the const keyword. Constants are useful when a value should not be modified during execution.
- Declaration: A constant is declared using the const keyword, followed by its data type and name.
- Fixed Value: Once a value is assigned to a constant, it cannot be changed during program execution.
Syntax
A constant is declared using the const keyword, followed by the data type, constant name, and a fixed value.
const data_type constant_name = value;
using System;
class Program
{
static void Main()
{
// Declaring a constant
const int c = 24;
Console.WriteLine(c);
}
}
Output
24
In the above code, the variable c is a constant whose value cannot be changed. Its value is determined at compile time and embedded directly into the program.
Types of Constants in C#
- Integer Constant: Used to store fixed whole number values. It is declared using
constwith integer type. The value cannot be changed after initialization. - Floating Constant: Used to store fixed decimal values. It can be of type
floatordouble. Float constants require a suffix 'f'. - Character Constant: Used to store a single fixed character. It is enclosed in single quotes (
' '). The value remains unchanged during execution. - String Constant: Used to store fixed text values. It is enclosed in double quotes (
" "). The value cannot be modified after assignment. - Boolean Constant: Used to store logical values such as true or false. It is mainly used in conditional statements. The value remains constant.
using System;
class GFG
{
static void Main()
{
const int a = 10; // Integer
const double b = 3.14; // Double
const float c = 2.5f; // Float
const char d = 'A'; // Character
const string e = "Hello"; // String
const bool f = true; // Boolean
const decimal g = 99.99m; // Decimal
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
Console.WriteLine(d);
Console.WriteLine(e);
Console.WriteLine(f);
Console.WriteLine(g);
}
}
Output
10
3.14
2.5
A
Hello
True
99.99Literals
A literal represents a fixed value written directly in the program. It does not have a name and is used to assign values to variables during initialization. Literals remain unchanged during execution.
- Direct Value: A literal is written directly in the source code, such as 10, 3.14, 'A', or "Hello".
- Types: Literals can represent different data types, such as integer, floating-point, character, string, and Boolean values.
Syntax
Literals do not have a separate declaration syntax. They are fixed values written directly in the source code.
variable_type variable_name = literal_value;
Let's take a look at an example:
using System;
class Program{
static void Main(){
int x = 100;
Console.WriteLine(x);
}
}
Output
100
In the above code, the value 100 assigned to the variable x is a literal. It is a fixed value directly written in the program and used to initialize the variable. This value remains constant during program execution.
Types of Literals in C#
- Integer Literals: Represents whole numbers directly written in the code. It can be decimal, hexadecimal, or binary. By default, it is of type int.
- Floating-point Literals: Represents decimal values written in the code. By default, it is of type double. It can be defined as float using suffix f.
- Character Literals: Represents a single character enclosed in single quotes. It can also be defined using Unicode or escape sequences.
- String Literals: Represents a sequence of characters enclosed in double quotes. It is used to store text directly in the code.
- Boolean Literals: Represents logical values directly in the code. Only two values are allowed: true and false.
- Null Literals: Represents the absence of any value. It is mainly used with reference types.
using System;
class Program
{
static void Main()
{
// Decimal Integer Literal
int number = 500;
// Hexadecimal Integer Literal
int hexNumber = 0xFF;
// Binary Integer Literal
int binaryNumber = 0b1010;
// Long Literal
long population = 8000000L;
// Float Literal
float temperature = 36.5f;
// Double Literal
double distance = 125.75;
// Decimal Literal
decimal salary = 45000.50m;
// Character Literal
char symbol = '#';
// String Literal
string city = "Gurugram";
// Boolean Literal
bool isActive = true;
// Null Literal
string email = "admin@exmple.com";
Console.WriteLine(number);
Console.WriteLine(hexNumber);
Console.WriteLine(binaryNumber);
Console.WriteLine(population);
Console.WriteLine(temperature);
Console.WriteLine(distance);
Console.WriteLine(salary);
Console.WriteLine(symbol);
Console.WriteLine(city);
Console.WriteLine(isActive);
Console.WriteLine(email);
}
}
Output
500
255
10
8000000
36.5
125.75
45000.50
#
Gurugram
True
admin@exmple.comConstants Vs Literals
Constants and literals are often confused as the same, but in C#, they are different entities with distinct meanings.
Features | Constants | Literals |
|---|---|---|
Definition | A constant is a named variable whose value cannot change. | A literal is a fixed value directly written in the code. |
Declaration | Declared using the const keyword. | No declaration required. |
Storage | Stored in memory with a name (identifier). | Used directly without any name. |
Example | const int x = 10; | 10, "Hello", true |