Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

Computer Programming

Principles
Data Types in C++
Programming
26th March, 2024

Dr. Ramesh Kumar


Associate Professor
Computer System Engineering Department,
DUET Karachi 1
Program Important Characters

2
Data Types Introduction
• It is a classification of specific types of data by a
—Certain value
—Certain types of Mathematical
—Logical Operations
• Depends on the command of the programming
language such as letters, lowercase, uppercase,
numbers, punctuation, and many more.
• Data types are Essential for good programming:
— Determine the type, size, and behaviour of variables
— Enable proper data manipulation

3
C++ Data Types
• Three categories of C++ Data

Data Types

User
Primitive Derived
defined

4
C++ Data Types
• Primitive Data Type • User-defined Data Type
—Integer —Class
—Character —Structure
—Boolean —Union
—Floating Point —Enumeration
• Derived Data Type —Typedef defined
—Function Datatype
—Array
—Pointer
—Reference
5
C++ Primitive Data Types
• Computers are designed to process Data (users,
devices)
• Data is represented as a sequence of bits in memory
• The sequence of bits may represent
— An instruction,
— A numeric value,
— A character,
— A portion of an image,
— A portion of Voice/Video data

6
C++ Primitive Data Types
• Three categories of simple data

—Integral: integers (numbers without a decimal)

—Floating-point: decimal numbers

—Char type: String, char

—Boolean: True and false

7
C++ Primitive Data Types

Primitive types

integral floating

char short int long bool float double long double

unsigned
Primitive Data Types in C++
• Integral Type
— Represent whole numbers and their negatives
— Declared as int, short, or long
• Character Type
— Represent single characters
— Declared as char
• Boolean Type
— Has two values true/false
— Declared as bool
• Floating Types
— Represent real numbers with a decimal point
— Declared as float, or double
— Scientific notation where e (or E) stand for “times 10 to
the ” (.55-e6)
Samples of C++ Data Values
int sample values
4578 -4578 0

bool values
true false

float sample values


95.274 95.0 .265

char sample values


‘B’ ‘d’ ‘4’ ‘?’ ‘*’
11
int Data Type
• Examples:
-6728
0
78
• Positive integers do not have to have a + sign in
front of them
• No commas are used within an integer
• Commas are used for separating items in a list

12
bool Data Type
• bool type

— Has two values, true and false

— Manipulate logical (Boolean) expressions

• true and false are called logical values

• bool, true, and false are reserved words

13
char Data Type

• The smallest integral data type


• Used for characters: letters, digits, and special
symbols
• Each character is enclosed in single quotes
• Some of the values belonging to char data type
are: 'A', 'a', '0', '*', '+', '$', '&'
• A blank space is a character and is written ' ',
with a space left between the single quotes

14
Floating-Point Data Types
• C++ uses scientific notation to represent real
numbers (floating-point notation)

15
Floating-Point Data Types
(continued)
• float: represents any real number
—Range: -3.4E+38 to 3.4E+38
• Memory allocated for the float type is 4 bytes
• double: represents any real number
—Range: -1.7E+308 to 1.7E+308
• Memory allocated for double type is 8 bytes
• On most newer compilers, data types double
and long double are same

16
C++ Data Type String
• A string is a sequence of characters
enclosed in double quotes

• string sample values


“Hello” “Year 2000” “1234”

• The empty string (null string) contains no


displayed characters and is written as “”
C++ Data Type String (cont.)
• string is not a built-in (standard) type
—it is a programmer-defined data type
—it is provided in the C++ standard library
• Need to include the following two lines:
#include <string>
using namespace std;
• string operations include
—comparing 2 string values
—searching a string for a particular character
—joining one string to another (concatenation)
—etc...
A Literal

• A literal is a fixed, explicit value that is known at


compile time
– Can be used to initialize variables
– Can be used to initialize constants
– Can be used in expressions
Generally bad programming style
• It may be int, char, bool, etc.
– 5, 5.0, -3, 'a', “C++”, “Programming!”, '\n'
Special Literals

• Boolean
 true
 false
• Pointer
– nullptr ← preferred literal
–0
– NULL (must #include cstdlib)
Variables

• A variable is a logically named, typed, structured


piece of storage
• Name allows us to refer to the stored structure
• Type allows us to know structure
• Variables can be assigned new values
• Program can manipulate them!!
Variables

• Definition: allocates space for storage


• Declaration: specifies name and type
– So variable can be referenced here
– … and defined elsewhere
• Type var_name, var_name, …;
• All vars have type given at start
• Good practice: one per line of code!
Variable Definition

int sum = 0, value, // all type int


total = 0; /* sum and total
initialized to 0 */
Sales_item item; /* type Sales_item
initialized to
default value */
std::string name(“Dr. Newman”);
/* string is a type
from std library
variable length
character sequence */
Initialization
• Good idea: ALWAYS INITIALIZE!!!!
• Initialization – object gets value at time of
definition (when created)
– May be any expression that can be evaluated at time of
creation

– Name becomes visible immediately

– Hence can be used in subsequent initializations in same


line!
Variable Initialization

int i = 0, j = 2*i; /* j init uses value


of i immediately */

int k = sizeof(double); /* value is


a function that can
be evaluated when k
is defined */
“List” Initialization
int i = 0; /* i initialized with
literal value */

int i = {0}; /* i initialized with


literal value, but
restricted */
int i(1)=0; /* same here */
double pi = 3.14; /* floating pt */
Constants
• Naming your constants
—Literal constants are "OK", but provide
little meaning
• e.g., seeing 24 , tells nothing about what it represents
• Use named constants instead
—Meaningful name to represent data

const int NUMBER_OF_STUDENTS = 24;

• Called a "declared constant" or "named constant"


• Now use it’s name wherever needed in program
Arithmetic Precision
• Precision of Calculations
—VERY important consideration!
• Expressions in C++ might not evaluate as
you’d "expect"!
—"Highest-order operand" determines type of
arithmetic "precision" performed
• Examples:
—17 / 5 evaluates to 3 in C++!
• Both operands are integers
• Integer division is performed!
—17.0 / 5 equals 3.4 in C++!
• Highest-order operand is "double type"
• Double "precision" division is performed!
Type Casting
• Two types
—Implicit—also called "Automatic"
• Done FOR you, automatically
17 / 5.5
This expression causes an "implicit type cast" to take place,
casting the 17  17.0
—Explicit type conversion
• Programmer specifies conversion with cast operator
(double)17 / 5.5
Same expression as above, using explicit cast
(double)myInt / myDouble
More typical use; cast operator on variable

You might also like