0% found this document useful (0 votes)
6 views8 pages

3 - Data Types in C++

Uploaded by

saeedafghan01234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

3 - Data Types in C++

Uploaded by

saeedafghan01234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Data Types

Instructor: Samiullah Ehsas

1
C++ Data Types
While doing programming in any programming language, you need to use
various variables to store various information. Variables are nothing but
reserved memory locations to store values. This means that when you
create a variable you reserve some space in memory.

You may like to store information of various data types like character,
integer, floating point, double floating point, Boolean etc. Based on the
data type of a variable, the operating system allocates memory and decides
what can be stored in the reserved memory.

2
• As its name indicates, a data type represents a type of the data which
you can process using your computer program. It can be numeric,
alphanumeric, decimal, etc.

• A data type is used to Identify the type of a variable when the variable is
declared .

Data type:
Void
Char
int
long
Float(real)
Double
3
pointer
Two Classifications of Data Types

• Built-in data types


• Fundamental data types (int, char, double, float, void, pointer)

• Programmer-defined data types


• Structure
• Union
• Enumeration

4
Fundamental Data Types :
• void – used to denote the type with no values
• int – used to denote an integer type
• char – used to denote a character type
• float, double – used to denote a floating point type
• int *, float *, char * – used to denote a pointer type, which is a memory
address type
Type Typical Bit Width Value range which can be represented by this data type

char 1 Byte -128 to 127 or 0 to 255


int 4 Bytes -2,147,483,648 to 2,147,483,647
short int 2 Bytes -32,768 to 32,767
long int 4 Bytes -2,147,483,647 to 2,147,483,647
float 4 Bytes +/- 3.4e +/- 38 (~ 7digits)
5
double 8 Bytes +/- 1.7e +/- 308 (~ 15 digits)
Following is the example, which will produce correct size of various data types on
your computer.
#include <iostream>
using namespace std;

int main()
{
cout << "Size of char : " << sizeof(char) << endl;
cout << "Size of int : " << sizeof(int) << endl;
cout << "Size of short int : " << sizeof(short int) << endl;
cout << "Size of long int : " << sizeof(long int) << endl;
cout << "Size of float : " << sizeof(float) << endl;
cout << "Size of double : " << sizeof(double) << endl;
cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;
return 0;
} 6
This example uses endl, which inserts a new-line character after every line and <<
operator is being used to pass multiple values out to the screen. We are also using sizeof()
function to get size of various data types.

When the above code is compiled and executed, it produces the following result which
can vary from machine to machine:
Size of char : 1
Size of int : 4
Size of short int : 2
Size of long int : 4
Size of float : 4
Size of double : 8
Size of wchar_t : 4 7
Thanks

You might also like