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

DATA TYPES

CP 111
Recap
What is a variable?

● Anything that stores a value.


● in simple terms is a storage place which has some memory allocated to it

Every variable should get some space in the memory; otherwise how can you use
it for computation?

All variables use data-type during declaration to restrict the type of data to be
stored

Observation: compiler must know the data type for size mapping
What is a Data Type
Data types are means to identify the type of data and associated operation of
handling it

Why have types :-

● Types provide context for operations


○ a+b (what kind of addition?)
● Limit valid set of operations
○ int a = “string” (know error before run time)
● Help the compiler decide how many bits should be reserved
for a variable
Types of Data Type
C++ has three types of data types :-
Primitive data set
These data types are built-in or predefined data types and can be used directly by
the user to declare variables. example: int, char , float, bool etc. Primitive data
types available in C++ are:

● Integer
● Character
● Boolean
● Floating Point
● Double Floating Point
● Valueless or Void
● Wide Character
Derived Data Type
The data-types that are derived from the primitive or built-in datatypes are referred
to as Derived Data Types. These can be of four types namely:

● Function
● Array
● Pointer
● Reference
User Defined Data Type
These data types are defined by user itself. Like, defining a class in C++ or a
structure. C++ provides the following user-defined datatypes:

● Class
● Structure
● Union
● Enumeration
● Typedef defined DataType
primitive data types available in C++
Integer: Keyword used for integer data types is int.

● Integers typically requires 4 bytes of memory space and ranges from


-2147483648 to 2147483647.
● Integers are whole number such as 5,39,-1917,0 etc.
● they have no fractional part.
● Integers can have positive as well as negative value

Write a simple code in c++ that assign value to a integer data type and display it.
primitive data types available in C++
Character: Character data type is used for storing characters.

Keyword used for character data type is char.

Characters typically requires 1 byte of memory space and ranges from -128 to 127
or 0 to 255.

Write a simple code in c++ that assign value to a character data type and display
it.
primitive data types available in C++
Boolean: Boolean data type is used for storing boolean or logical values.

A boolean variable can store either true or false.

Keyword used for boolean data type is bool.

Write a simple code in c++ that assign value to a boolean data type and display it.
primitive data types available in C++
Floating Point: Floating Point data type is used for storing single precision floating
point values or decimal values.

● Keyword used for floating point data type is float.


● Float variables typically requires 4 byte of memory space.
● for example 31.0 is a floating-point number not a integer but simply 31 is a
integer.

Write a simple code in c++ that assign value to a float data type and display it.
primitive data types available in C++
Double Floating Point: Double Floating Point data type is used for storing double
precision floating point values or decimal values.

● Keyword used for double floating point data type is double.


● Double variables typically requires 8 byte of memory space.
● It is used when float is too small or insufficiently precise.

Write a simple code in c++ that assign value to a double data type and display it.
primitive data types available in C++
Void: Void means without any value. void data type represents a valueless entity.
Void data type is used for those function which does not returns a value.

Example void main(){ }

Wide Character: Wide character data type is also a character data type but this
data type has size greater than the normal 8-bit data type.

● Represented by wchar_t. It is generally 2 or 4 bytes long.


● Mostly the wchar_t datatype is used when international
languages like Japanese are used
Data type Modifiers
As the name implies, data type modifiers are used with the built-in data types to
modify the length of data that a particular data type can hold.

The basic data type has modifiers preceding them .we use modifier to alter the
meaning of the base type to fit various situation more precisely.

There are 3 types of modifiers:-

● Integer type modifiers.


● Character type modifiers
● Float type modifiers.
Integer type modifiers
By using different number of bytes to store values , c++ offers 3 types of integers
:short , int and long that can represent up to three different integer sizes.

A short integer is at least 2 bytes .

A int integer is at least as big as short .

A long integer is at least 4 bytes


character type modifiers
The char type can also be signed or unsigned. The unsigned char represent the
range 0 to 255. The signed char represent the range -128 to 127.
Floating-point type modifiers
C++ has three floating-point types : float , double and long double.

● float type occupies 4 byte.


● Double occupies 8 byte .
● Long double occupies 10 byte.
Derived Data Types
From the built in data types other types can be derived called derived data types.

There are 5 types of derived data types :-

● Arrays.
● Functions.
● Pointers.
● References.
● Constant.
Pointers
Pointers are symbolic representation of addresses

Syntax:

datatype *var_name;

int *ptr; //ptr can point to an address which holds int data

The reason we associate data type to a pointer is that it knows how many bytes
the data is stored in.
Pointers
● Define a pointer variable
● Assigning the address of a variable to a pointer using unary operator (&)
which returns the address of that variable.
● Accessing the value stored in the address using unary operator (*) which
returns the value of the variable located at the address specified by its
operand.

int * ip; // int pointer

ip = &x; // address of x assigned to ip

*ip = 10; // 10 assigned to x through indirection


Pointers
Pointers
Example of pointer in C++
Void main() {

int var = 20; // declare pointer variable


int *ptr; // note that data type of ptr and var must be same
ptr = &var; // assign the address of a variable to a pointer

cout<<"Value at ptr = "<<ptr<<endl;

cout<<"Value at var = "var<<endl;

cout<<"Value at *ptr = "<<*ptr<<endl;

}
Pointers
Function
A function is a block of code or program-segment that is defined to perform a
specific well-defined task.

A function is generally defined to save the user from writing the same lines of
code again and again for the same input.

All the lines of code are put together inside a single function and this can be called
anywhere required. main() is a default function that is defined in every program of
C++.

Syntax

FunctionType FunctionName(parameters)
Why do we need functions?
Functions help us in reducing code redundancy. If functionality is performed at
multiple places in software, then rather than writing the same code, again and
again, we create a function and call it everywhere. This also helps in maintenance
as we have to change at one place if we make future changes to the functionality.

Functions make code modular. Consider a big file having many lines of codes. It
becomes really simple to read and use the code if the code is divided into
functions.
#include <iostream>
using namespace std;

int max(int x, int y)


{
if (x > y)
return x;
else
return y;
}

int main() {
int a = 10, b = 20;
int m = max(a, b); // Calling above function to find max of 'a' and 'b'
cout << "m is " << m;
return 0;
}
How to use a function
There are two main approaches of using a function in your program.

Approach I

● Declare your function


● Followed by the main function (in the main the function will be used)
● Define the function after the main

Approach II

● Declare and Define the function at the same time


● Followed by the main function (in the main the function will be used)
How to declare a function
A function declaration tells the compiler about the number of parameters function
takes, data-types of parameters and return type of function.

Putting parameter names in function declaration is optional in the function


declaration, but it is necessary to put them in the definition

It is always recommended to declare a function before it is used

Below are an example of function declarations. (parameter names are not there in
below declarations)

// A function that takes two integers as parameters and returns an integer

int max(int, int);


How to declare a function
// A function that takes a char and an int as parameters and returns an integer

int fun(char, int);

Exercise;

Write a function declaration where by the function is called display, has one
parameter is a string and returns no value. (3 minutes).

Write A function that takes a int pointer and an int variable as parameters and
returns an pointer of type int (discuss with your neighbour)
How to define a function
How to call a function
A function can be called or used in the main function or in another function.

You use the same format as the declaration but this time you pass actual values.

Parameter Passing to functions

The parameters passed to function are called actual parameters. For example, in
the above program 10 and 20 are actual parameters.

The parameters received by function are called formal parameters. For example,
in the above program x and y are formal parameters.
There are two most popular ways to pass parameters.
Pass by Value: In this parameter passing
method, values of actual parameters are
copied to function’s formal parameters and
the two types of parameters are stored in
different memory locations.
So any changes made
inside functions are not reflected in
actual parameters of caller.

What is the output of X?


There are two most popular ways to pass parameters.
Pass by Reference Both actual and
formal parameters refer to same locations,
so any changes made inside the function
are actually reflected in actual parameters
of caller.

What is the output of X?


Arrays
An array is a collection of items stored at continuous memory locations. The idea
of array is to represent many instances in one variable.

hey are used to store similar type of elements as in the data type must be the
same for all elements
Why do we need arrays?

We can use normal variables (v1, v2, v3, ..) when we have a small number of
objects, but if we want to store a large number of instances, it becomes difficult to
manage them with normal variables. The idea of an array is to represent many
instances in one variable.
Array declarations
There are various ways in which we can declare an array. It can be done

● Array declaration by specifying size


● Array declaration by initializing elements
● Array declaration by specifying size and initializing elements
Array declaration by specifying size

// Array declaration by specifying size

int arr1[10];

// With recent C/C++ versions, we can also

// declare an array of user specified size

int n = 10;

int arr2[n];
Array declaration by initializing elements

// Array declaration by initializing elements

int arr[] = { 10, 20, 30, 40 }

// Compiler creates an array of size 4.


Array declaration by specifying size and initializing elements

// Array declaration by specifying size and initializing

// elements

int arr[6] = { 10, 20, 30, 40 }

// Compiler creates an array of size 6, initializes first

// 4 elements as specified by user and rest two elements as 0.

// above is same as "int arr[] = {10, 20, 30, 40, 0, 0}"


Constant
A constant is a data item whose data value can never change during the program
run.

Const int pi= 3.14;


Question?
Quiz.
5 Minutes

You might also like