CHAPTER 4
DATA TYPES, VARIABLES AND CONSTANTS
LEARNING
OBJECTIVES
• To learn the correct
application of data types
and constants
• To identify the difference
between Value Types and
Reference Types.
• To define variables
correctly and properly
• To apply data types,
variables and constant in
creating a program.
OUTLINE:
• 4.1 The Data Types
• 4.2 Variables
• 4.3 Constant
4.1 THE DATA TYPES, VARIABLES AND
CONSTANT
The data types are a value classification of a variable in programming
language. The common example of this are integer, float, double float and
string. The string data type is use to classify letter or text; the integer type is for
the whole number; the float and double float type is for the floating number. It
its very important to know the kind of data type because variable assigned a
number cannot hold text later on in the program. Also, a variable defined as an
integer cannot be assigned a string.
CHARACTERISTICS
Data Types are characterized by:
- Name – for ex. Int;
- Size (How much memory they use) – for ex., 4 bytes;
- Default value – for ex. 0.
TYPES
Basic data types in C# are distributed into the following types:
DATA TYPES NAME KEYWORD
Integer Types Sbyte, byte, short, ushort, int, unit, long,
ulong;
Real floating-point types Float, double;
Real type with decimal precision decimal;
Boolean type bool;
Character type char;
String string;
Object type object;
There are two types of data types the first one is the Primitive Data type (Value
Type) and the second one is the Non-Primitive Data Types (Reference Type)
PRIMITIVE (Built-In Types)
C# primitive are also called value types and predefined in the .NET framework.
Primitive types can be assigned a value directly. The value assigned is stored on the
stack as opposed to the heap.
A stack is used for static memory allocation and Heap for dynamic memory
allocation, both stored in the computer’s RAM. Variables allocated on the stack are
stored directly to the memory and access to this memory is very fast, and its
allocation is dealt with when the program is compiled.
While value types are stored generally in the stack, reference types are stored
in the managed heap. A value type derives form system.ValueType and
contains the data inside its own memory allocation. In other words, variables or
objects or value types have own copy of the data.
NON-PRINITIVE (Reference Type)
The reference types do not contain the actual data stored in a variable, but
they contain a reference to the variables. In other words, they refer to a
memory location. Using multiple variables, the reference types can refer to a
memory location. If the data in the memory location is changed by one of the
variables, the other variable automatically reflects this change in value.
4.2 VARIABLES
This refers to the storage of information that will used (retrieve) inside the
program. It is named as storage location capable of containing a certain type of
data that can be modified during program execution.
Syntax: data_type variable_name;
Identifier Name it is a name used to reference variables, functions, labels and
various other user-defined objects.
RULES IN NAMING IDENTIFIER
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
_2010_tax Legal, but not advised
checking#account Illegal; contains the illegal character #
double Illegal; is a C keyword
9byte Illegal; first character is a digit
You can initialize a variable at the time of definition as:
int age = 10
Data Value stored to
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
Variables can be initialized in their declaration. The initializer consist of an
equal sign followed by a constant expression.
<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