You are on page 1of 23

Introduction to C++

Language Datatypes

Lecture 5

1
Levels of Programming Languages
There are 3 levels of programming
languages

i) High-Level Language
ii) Middle-Level Language
iii) Low-Level Language

2
Uses of C/C++ Programming Language

Areas where C/C++ language can be used are


 Operating Systems
 Network Drivers
 Communication Packages
 Databases
 Language Interpreters
 Utilities
 Language Compilers
 Spreadsheets
 Text Editors
 etc etc

3
C/C++ Character Set

C/C++ character set comprises of


following characters
 A,B,C,….Z
 a,b,c,…...z

 0,1,2,……9

, . ; : ? ! “ / ‘ \ | ~

( ) [ ] { } < >

+ - # % _ ^ = & *

4
White Space Characters

 The character that produces blank


space when printed is called a white
space character, e.g.
 Spaces
 Tabs
 New Lines

5
Data Transformation
 Programs transform data from one form to another
 Input data  Output data
 Stimulus  Response

 Programming languages store and process data in


various ways depending on the type of the data;
consequently, all data read, processed, or written
by a program must have a data type

 Two distinguishing characteristics of a programming


language are the data types it supports and the
operations on those data types

6
Data Handling
 Various types of data
 E.g. letters, numbers, symbols, etc.
 Data handling requires different data types
 Data Type: type of data that a variable can
hold and the operations that can be
performed on it.
 Logical to ask the computer to multiply a
float by an integer (1.5 x 5)
 Illogical to ask the computer to multiply a
float by a string (1.5 x FUI)
Constants & Variables
 Constants
 1, 56, 1.89, ’a’, ’Z’, ”BSSE”

 Variables
x=5
y = 20

8
What is Data Type
 A data type is a set of values, together with a set of
associated operations on those values.
 Here is the table for different types:-

Date Type Used for Examples

Alphanumeric “hello world”, “Islamabad”,


String
characters “Alice”
Integer Whole numbers 7, 12, 999, -47
Float (floating Number with a decimal
3.15, 9.06, 00.13
point) point
Single alphabet or
Character ‘A’, ‘9’, ‘&’ (uses ASCII)
number
Representing logical
Boolean True, False
values
A Data Type

 A data type is
 A set of values AND
 A set of operations on those values
 A data type is used to
 Identify the type of a variable when the
variable is declared
 Identify the type of the return value of a
function
 Identify the type of a parameter expected
by a function

10
A Data Type (continued)

 When the compiler encounters a declaration for


a variable, it sets up a memory location for it
 An operator used on a variable or variables is
legal only if
 The operator is defined in that programming
language for a variable of that type
 The variable or variables involved with the
operator are of the same or compatible
types

11
C++ Data Types
Primitive Built-in Data Types

Type Keyword
Boolean bool
Character char
Integer int
Floating point float
Double floating point double
Valueless void
Wide character wchar_t
Primitive Built-in Data Types
Many of the above data types can be modify
using these modifiers:-
signed

unsigned

short

long
Variable Types and Memory Used

Typical Size
Type Typical Range
(Byte)
char 1 byte -128 to 127

unsigned char 1 byte 0 to 255

signed char 1 byte -128 to 127


-2147483648 to
int 4 bytes
2147483647
unsigned int 4 bytes 0 to 4294967295
-2147483648 to
signed int 4 bytes
2147483647
Variable Types and Memory Used
Typical Size
Type Typical Range
(Byte)
short int 2 bytes -32768 to 32767

unsigned short int 2 bytes 0 to 65,535

signed short int 2 bytes -32768 to 32767


-
9,223,372,036,854,775,808
long int 8 bytes
to
9,223,372,036,854,775,807
-
9,223,372,036,854,775,808
signed long int 8 bytes
to
9,223,372,036,854,775,807
0 to
unsigned long int 8 bytes 18,446,744,073,709,551,61
5
Variable Types and Memory Used

Typical Size
Type Typical Range
(Byte)
-3.4 x 1038 to +3.4 x 1038
float 4 bytes
(~7 digits)
-1.7 x 10308 to +1.7 x 10308
double 8 bytes
(~15 digits)
wchar_t 2 or 4 bytes 1 wide character
Data Types
 Boolean: for storing true or false values.
Boolean variables are declared using the
keyword bool
 String: generally means an ordered
sequence of characters, enclosing
delimiters are double quotes “some string“.
Declared using keyword string
 The sizes of variables might be different
from those shown in the above table,
depending on the compiler and the
computer you are using.

18
Variable Declaration
When variables are declared of a
particular data type then
the variable becomes the place where the data is
stored, and
data types is the type of value(data) stored by
that variable.
int var;
int is the datatype of the variable,
var is the variable name
; is statement terminator
Identifiers
Identifiers refer to the names of
data types, constants, variables, and
functions
Cannot be keywords

20
Rules for Constructing Identifiers in C++
 Can contain letters, digits and underscores,
capital letters A-Z, lowercase letters a-z,
digits 0-9, and the underscore character _
 Digit can not be the first character.
 First character must be a letter or
underscore
 Can not consist of an underscore alone.
 Usually only the first 32 characters are
significant.

21
Rules for Constructing Identifiers in C++
(contd.)
 May not be same as keyword or function
name etc.
 There can be no embedded blanks (spaces
are not allowed).
 Keywords cannot be used as identifiers
 Identifiers are case sensitive

22
Types of C/C++ Instructions
There are 4 types of C/C++ Instructions.
i) Type declaration Instructions
Variable types and definitions etc.
ii) Input/Output Instructions
Data Input, Data Display, Data
Write etc
iii) Control Instructions
Controls the sequence of execution
of the program instructions.
iv) Arithmetic Instructions
Arithmetic Operations etc
23

You might also like