CPP-Day 3

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 22

C++ Introduction – Day 3

Data Type

Before we start learning about C++ Data Types lets
understand some basic concepts in computer
programming.


As you are aware, due to advances in computer science
literally anything can be represented digitally in
computer programs
C++ Introduction – Day 3
 A classic example is PUBG where Persons, Weapons , Buildings etc
are created digitally. These objects represent real life objects virtually.
 Each of these objects behave and imitate properties similar to real life
objects.
 E.g. A gun in PUBG has same color , shape and firing sound as real
life Gun.
 A Car, helicopter has same size, shape , sound and controls as real
life car or helicopter.
 While programming real life objects in C++ we create
Variables/Properties to store and describe information about object
being programmed.
C++ Introduction – Day 3

In spoken languages we use characters and numerals to communicate different
type of information

E.g. We use word (formed by characters ) to write address, poem , names of all
objects living or dead are written in characters

We use numerals to tell distance between two cities ( 1 km), height of person, count
of mangoes in box

Some times we use Yes or No to answer some questions e.g. Are you hungry? Or
Are you mad? Etc.

So depending on the information being communicated we use mix of
character,numerals or Yes/No.

Will you be able to answer following questions. If not then can you explain why?
 A+A=?
 1+A=?
 One + One = ?
C++ Introduction – Day 3
You may be tempted to say that the answer to last question is two. That is
partially true.
Human brain can interpret the word representation of numerals (One → 1) , and
perform addition to arrive at answer.
However computers understand only characters and numerals which are
represented by 0 and 1 internally.
So if one tries to create a character variable, assign a value “one” and writes a
program to perform addition computer will not be able to do that.
Meaning of this is that we have to tell computer while writing program what type
of information will be stored in variable. Based on this compiler will
allow certain operations on those variables.
C++ Introduction – Day 3

Providing the data type while defining variable in


computer programming provides a way to tell
computer what type of information will be
stored in the variable.
Data type defines

The operations that can be done on the data

The meaning of the data

The way values of that type can be stored
C++ Introduction – Day 3

We will learn basic types with examples

As we have seen in C++ Programming data types are declarations for
variables.

Data type declaration of variable determines type of data and size of
data associated with variables

For example

int age = 13;

Here , “age” is a variable of type int. Meaning of this is the variable
“age” can store only integers of 2 or 4 bytes. Size of data (2 or 4 bytes
is automatically defined once we have declared data type is “int”)
C++ Introduction – Day 3

C++ Fundamental Data Types
Data Type Meaning Size (in Bytes)

int Integer 2 or 4

float Floating-point 4

double Double Floating- 8


point

char Character 1
C++ Introduction – Day 3
Data Type Meaning Size (in Bytes)

wchar_t Wide Character 2

bool Boolean 1

void Empty 0
C++ Introduction – Day 3
C++ int

The int keyword is used to indicate integers

Its size is usually 4 bytes. That means it can store – 2147483648 to
2147483647

If you have noticed it stores one less positive number that is because “0”
is included in positive numbers range

For Example
int pincode = 421201;
int distance = 1000;
int salary = 10000;
C++ Introduction – Day 3
C++ float and double

float and double are used to store decimal and exponential
numbers

Size of float is 4 bytes and Size of double is 8 bytes

double is two times more precise than float

For example

float area = 64.74;

double volume = 129.574893;

double distance = 45E12 // 45E12 is equal to 45 x 10 ^ 12 (10 Raise to
12)
C++ Introduction – Day 3

C++ char

Keyword char is used for characters

Its size is 1 byte

Characters in C++ are enclosed in single quotes ‘ ‘

For example

char test = ‘h’;
C++ Introduction – Day 3

C++ wchar_t

Wide character wchar_t is similar to the char data
type, except its size is 2 bytes instead of 1.

It is used to represent characters that require more
memory to represent them than a single char.

For example,

I don’t have example right now will update later
C++ Introduction – Day 3
C++ bool

The bool data type has one of two possible values:
true or false.

Booleans are used in conditional statements and
loops (which we will learn in later)

For example,

bool somecondition = false;

bool areyoumad = true;
C++ Introduction – Day 3

C++ void

The void keyword indicates an absence of
data. It means "nothing" or "no value".

We will use void when we learn about
functions and pointers at later point
C++ Introduction – Day 3

Exercise Three

Write a program declaring variables with
different types. Assign them values and
print them.
C++ Introduction – Day 3
C++ Type Modifiers

So far you have learned basic data types in C++. The behavior of
these types can be modified using type modifiers.

The are 4 type modifiers in C++
 signed
 unsigned
 short
 Long


We can modify following data with above listed modifiers
 int
 double
 char
C++ Introduction – Day 3
Data Type Size (in Meaning
Bytes)
signed int 4 Used for integers equivalent to int
unsigned int 4 Can store only positive numbers
short 2 used for small integers (range -32768
to 32767)
unsigned short 2 used for small positive integers (range
0 to 65,535)
long At least 4 Used for large integers equivalent to
long int
unsigned long 4 Used for large positive integers or 0
equivalent to unsigned long int
long long 8 Used for very large integers equivalent
to long long int
C++ Introduction – Day 3
Data Type Size (in Meaning
Bytes)

unsigned long long 8 Used for very large positive integers


equivalent to unsigned long long int

long double 12 Used for large floating-point numbers


e.g.
125632560.234567123456789012345

signed char 1 used for characters (range -127 to


127)

unsigned char 1 used for characters (range 0 to 255)


C++ Introduction – Day 3

Few examples using type modifiers
 long b = 4523232;
 long int c = 2345342;
 long double d = 233434.56343;
 short d = 3434233; // what will be out come and
why
 unsigned int a = 5;
C++ Introduction – Day 3

Sample Program
 // Online IDE - Code Editor, Compiler, Interpreter
 #include<iostream>
 using namespace std;
 int main()
 {
 long b = 4523232;
 cout<< "Current value in Variable b Is : " << b << "\n";
 cout<<"Now assigning new value to variable b :"<<"\n";
 // 123456789101112131415
 b = 999999999999999999999;
 cout<< "Current value in Variable b Is : " << b;
 return 0;
 }
C++ Introduction – Day 3

Type the above program In online IDE

Observe output of variable “b” on last line. Can you write explanation of the behavior?

Try to write program for each variable modifier and find out the maximum positive and negative value it can store;

Type below program in online IDE


#include<iostream>

using namespace std;

int main()

{

char mychar = 'a';

signed mySignedChar = 'a';

unsigned myUnsignedChar = 'a';

cout<< "Current value in Variable mychar Is : " << mychar << "\n";

cout<< "Current value in Variable mySignedChar Is : " << mySignedChar << "\n";

cout<< "Current value in Variable myUnsignedChar Is : " << myUnsignedChar << "\
n";

// Type casting (Converting from one data type to other data type) to int;

cout<< "Current value in Variable mychar after Type casting to int : " << (int)mychar
<< "\n";


cout<< "Reverse Type Casting : " << char(97) << "\n";

return 0;

}
C++ Introduction – Day 3

The case of “char” data type is interesting case

In C++ integer value is stored in the ‘char’ variables instead of character itself.

In above program when we assign ‘a’ to mychar variable internally 97 is stored
in variable mychar. This is ASCII(American Standard Code for Information
Interchange) code of character ‘a’;

Follow the link if you are interested to understand more about ASCII.

Change value of mychar variable from ‘a’ to ‘abc’ and see the output

Change value of mySignedChar to ‘-127’ and see the output.

You are encouraged to play with above data types and learn more thing and
compiler errors that are thrown this helps in building skill to debug the program

Refer the Escape Sequences we learnt in C++ Literals . Write program to print
these sequences to scree. Write down your observations about them.

You might also like