Chapter 4 Data Types Variables and Constants
Chapter 4 Data Types Variables and Constants
1. The name can contain letters, digits, and the underscore character (_).
2. The first character of the name must be a letter. The underscore is also a legal first
character, but its use is not recommended at the beginning of a name. an
underscore is often used with special commands, and it’s sometimes hard to read.
3. Case matters (that is, upper- and lowercase letters). C# is case sensitive; thus, the
names count and count refer to two different variables.
4. C# keywords can’t be used as variable names. Recall that a keyword is a word that
is part of the C# language.
5. The following list contains some example of valid and invalid C# variable names:
VARIABLE NAME LEGALITY
Percent Legal
y2x5 Legal
yearly_cost legal
int age = 10
Variable
INITIALIZE VARIABLES
variables are initializes (assigned a value) with an equal sign followed by a
constant expression. The general form of initialization is:
variable_name = value
<data_type><variable_name>=value;
Some examples are:
int d = 3, f = 5; /*initializing d and f.*/
byte z = 22; /*initializes z.*/
double pi = 3.14159; /*declares an approximation of pi*/
char x = ‘x’; /*the variable x has the value ‘x’*/
using System; float pi;
double num3;
namespace VariableDeclaration /*actual initialization*/
pi=3.141;
{ num1 = 12;
num2 = 18;
class program num3 = num1 + num2;
Console.WriteLine(“The value of PI is {0}, pi”);
{
Console.WriteLine(“number 1 = {0}, number 2 = {1},
static void Main(string[]args) number 3 = {2}”, num1, num2, num3);
console.ReadLine();
{ }
}
short num1; }
int num2;
OUTPUT OF THE PROGRAM
The value of PI is 3.14 number 1=12, number 2=18, number 3=30/
_______________________________________________________________
4.3 CONSTANTS
Constants are immutable values which are known at compile time and do
not change for the life of the program. Constant are declared with the conts
modifier. Only the c# built-in types (excluding System.Object) may be
declared as “const”.
INTEGER LITERAL
An integer can be a decimal, octal, or hexadecimal constant. A prefix
specfies the base or radix: 0x or 0X for hexadecimal, 0 for octal, and no prefix
id for decimal. An integer literal can also have a suffix that is a combination of
U and L, for unsigned and long, respectively. The suffix can be uppercase or
lowercase and can be in any order.
Here are some examples of integer literals:
212/*Legal*/
215u/*Legal*/
0xFeel/*Legal*/
078/*Illegal:8 is not an octal digit*/
032UU/*Illegal: cannot repeat a suffix*/
Following are other examples of various types of Integer literals:
85/*decimal*/
0213/*octal*/
04xb/*Legal*/
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.
Here are some examples of floating-point literals:
3.14159/*Legal*/
314159E-L/*Legal*/
210f/*Illegal: no decimal or exponent*/
.e55/*Illegal: missing integer or fraction*/
While representing in decimal form, you must include the decimal point,
the exponent, or both; and while representing using exponential form you
must include the integer part, the fractional part, or both. The signed
exponent is introduced by e or E.
CHARACTER CONSTANT
Character literals are enclosed in single quotes. For example, ‘x’ and
can be stored in 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 (susch as ‘\u02c0’).
There are certain characters in C# when they are presided by a backslash. They
have special meaning and they are used to represent like newline (\n) or tab (\
t). Here, is a list of some of such escape sequence codes:
ESCAPE SEQUENCE MEANING
\\ \character
\’ ‘ character
\” “ character
\? ? Character
\a Alert or bell
\b Backspace
\f Form feed
\n Newline
\r Carriage return
\t Horizontal tab
\v Vertical tab
\ooo Octal number of one to thee Digits
\xhh. . . Hexadecimal number of one or more digits
THANK YOU