Anda di halaman 1dari 30

Learners Support Publications www.lsp4you.

com
Variables and Data
Types
Learners Support Publications www.lsp4you.com
Objectives of this session
Keywords
Identifiers
Basic Data Types
bool & wchar_t
Built-in Data Types
User-defined Data Types
Derived Data Types
Symbolic Constants
Dynamic Initialization of Variables
Reference Variables

Learners Support Publications www.lsp4you.com
Variables and Data Types
Tokens
The smallest individual units in a program are
known as tokens.
o Keywords
o Identifiers
o Constants
o Strings
o Operators
Learners Support Publications www.lsp4you.com
Keywords
Learners Support Publications www.lsp4you.com
Identifiers
A valid identifier is a sequence of one or more
letters, digits or underscore characters (_).
Neither spaces nor punctuation marks or
symbols can be part of an identifier. Only
letters, digits and single underscore
characters are valid. In addition, variable
identifiers always have to begin with a
letter. They can also begin with an
underline character (_ ).
Learners Support Publications www.lsp4you.com
Identifiers
The name of a variable:
Starts with an underscore _ or a letter, lowercase or
uppercase, such as a letter from a to z or from A to Z.
Examples are Name, gender, _Students, pRice.
Can include letters, underscore, or digits.
Examples are: keyboard, Master, Junction, Player1,
total_grade, _ScoreSide1.
Cannot include special characters such as !, %, ], or $.
Cannot include an empty space.
Cannot be any of the reserved words.
Should not be longer than 32 characters (although
allowed).
continue
Learners Support Publications www.lsp4you.com
Basic Data Types
C++ Data Types
User-defined Type Built-in Type Derived Type
Integral Type Void Floating Type
structure
union
class
enumeration
array
function
pointer
reference
int char float double
Learners Support Publications www.lsp4you.com
Basic Data Types
ANSI C++ added two more data types
bool
wchar_t
continue
Learners Support Publications www.lsp4you.com
Data Type - bool
A variable with bool type can hold a Boolean value true or
false.
Declaration:
bool b1; // declare b1 as bool type
b1 = true; // assign true value to b1
bool b2 = false; // declare and initialize

The default numeric value of
true is 1 and
false is 0.
Learners Support Publications www.lsp4you.com
Data Type wchar_t
The character type wchar_t has been defined to
hold 16-bit wide characters.

wide_character uses two bytes of memory.

wide_character literal in C++
begin with the letter L
Lxy // wide_character literal
Learners Support Publications www.lsp4you.com
Built-in Data Types
int, char, float, double are known as basic or
fundamental data types.

Signed, unsigned, long, short modifier for integer
and character basic data types.

Long modifier for double.

Learners Support Publications www.lsp4you.com
Built-in Data Types
Type void was introduced in ANSI C.

Two normal use of void:
o To specify the return type of a function when it is not
returning any value.
o To indicate an empty argument list to a function.
o eg:- void function-name ( void )
continue
Learners Support Publications www.lsp4you.com
Built-in Data Types
Type void can also used for declaring generic pointer.
A generic pointer can be assigned a pointer value of any
basic data type, but it may not be de-referenced.

void *gp; // gp becomes generic pointer
int *ip; // int pointer
gp = ip; // assign int pointer to void pointer
Assigning any pointer type to a void pointer is allowed in
C & C++.
continue
Learners Support Publications www.lsp4you.com
Built-in Data Types
void *gp; // gp becomes generic pointer
int *ip; // int pointer
ip = gp; // assign void pointer to int pointer
This is allowed in C. But in C++ we need to use a cast
operator to assign a void pointer to other type pointers.
ip = ( int * ) gp; // assign void pointer to int pointer
// using cast operator

*ip = *gp; is illegal
continue
Learners Support Publications www.lsp4you.com
User-Defined Data Types
Structures & Classes:
struct
union

class
Legal data types in C++.
Like any other basic data type to declare variables.
The class variables are known as objects.
Learners Support Publications www.lsp4you.com
User-Defined Data Types
Enumerated Data Type:
Enumerated data type provides a way for attaching names
to numbers.
enum keyword automatically enumerates a list of words
by assigning them values 0, 1, 2, and so on.

enum shape {circle, square, triangle};
enum colour {red, blue, green, yellow};
enum position {off, on};
continue
Learners Support Publications www.lsp4you.com
User-Defined Data Types
Enumerated Data Type:
enum colour {red, blue, green, yellow};
In C++ the tag names can be used to declare new
variables.
colour background;
In C++ each enumerated data type retains its own
separate type. C++ does not permit an int value to
be automatically converted to an enum value.
continue
Learners Support Publications www.lsp4you.com
User-Defined Data Types
Enumerated Data Type:
colour background = blue; // allowed
colour background = 3; // error in C++
colour background = (colour) 3; // OK

int c = red; // valid
continue
Learners Support Publications www.lsp4you.com
User-Defined Data Types
Enumerated Data Type:
By default, the enumerators are assigned integer
values starting with 0. We can override the default
value by explicitly assigning integer values to the
enumerators.

enum colour { red, blue=4, green =8};
enum colour {red=5, blue, green};
continue
Learners Support Publications www.lsp4you.com
User-Defined Data Types
Enumerated Data Type:
C++ also permits the creation of anonymous
enum (i.e., enum with out tag name).
enum {off, on}; here off 0 and on 1

int switch_1 = off;
int switch_2 = on;
continue
Learners Support Publications www.lsp4you.com
Derived Data Types
Arrays
The application of arrays in C++ is similar to that
in C.
Functions
top-down - structured programming ; to reduce
length of the program ; reusability ; function over-
loading.
Learners Support Publications www.lsp4you.com
Derived Data Types
Pointers
Pointers can be declared and initialized as in C.
int * ip; // int pointer
ip = &x; // address of x assigned to ip
*ip = 10; // 10 assigned to x through indirection
continue
Learners Support Publications www.lsp4you.com
Derived Data Types
Pointers
C++ adds the concept of constant pointer and
pointer to a constant.
char * const ptr1 = GOODS; // constant pointer
int const * ptr2 = &m; // pointer to a constant
continue
Learners Support Publications www.lsp4you.com
Symbolic Constants
Two ways of creating symbolic constant in C++.
Using the qualifier const
Defining a set of integer constants using enum keyword.

Any value declared as const can not be modified by the
program in any way. In C++, we can use const in a
constant expression.
const int size = 10;
char name[size]; // This is illegal in C.
Learners Support Publications www.lsp4you.com
Symbolic Constants
const allows us to create typed constants.
#define - to create constants that have no type
information.

The named constants are just like variables except that
their values can not be changed. C++ requires a const to
be initialized.

A const in C++ defaults, it is local to the file where it is
declared. To make it global the qualifier extern is used.
continue
Learners Support Publications www.lsp4you.com
Symbolic Constants
extern const int total = 100;

enum { X, Y, Z };
This is equivalent to
const int X = 0;
const int Y = 1;
const int Z = 2;

continue
Learners Support Publications www.lsp4you.com
Reference Variables
A reference variable provides an alias for a
previously defined variable.

For eg., if we make the variable sum a reference to
the variable total, then sum and total can be used
interchangeably to represent that variable.
data-type & reference-name = variable-name
float total = 100;
float &sum = total;
Learners Support Publications www.lsp4you.com
Reference Variables
A reference variable must be initialized at the time
of declaration. This establishes the correspondence
between the reference and the data object which it
names.

int x ;
int *p = &x ;
int & m = *p ;
continue
Learners Support Publications www.lsp4you.com
Reference Variables
void f ( int & x )
{
x = x + 10;
}
int main ( )
{
int m = 10;
f (m);
}
continue
When the function call f(m) is
executed,
int & x = m;
Learners Support Publications www.lsp4you.com
Thank You

Anda mungkin juga menyukai