Data Types and Modifiers
Data Types
- Primitive - Integer, Float, Character, Boolean
- Derived - Function, Array, Pointer, Reference
- User Defined - Class, Structure, Union, Enum
Examples for Primitive Data types:- Integer - 1, 4, 100
- Float - 3.14, 5.22, 4.243432
- Character - @, |, c, f
- Boolean - True, False or (0, 1)
- Integer occupies 4 bytes. Since 1 Byte = 8 bits, integer occupies 32 bits in a memory. Range of unsigned integer is 0 to (2^32)-1. To store negative integer, the left most (most significant bit [MSB]) is used to define the sign of the integer. So, if the left most bit is filled with 1, it means that the integer is negative and if it is 0, then it is a positive integer. Therefore, the range of signed integer is -(2^31) to (2^31)-1.
- Float occupies 4 bytes. We can store up to 7 decimal digits. If we want to store more digits, then we use another data type called Double that occupies 8 bytes. It can store up to 15 decimal digits.
- Char occupies 1 byte. We use ASCII table that is attached below.
- Boolean data type stores boolean values. True is represented by 1 and False is represented by 0. It occupies 1 byte.
Type Modifiers
- signed: It occupies 4 bytes & has a range of -2,147,483,648 to 2,147,483,647
- unsigned: It occupies 4 bytes & has a range of 0 to 4,294,967,295
- long: It occupies 8 bytes & has a range of -9,223,372,036,854,775,808 to -9,223,372,036,854,775,807
- short: It occupies 4 bytes & has a range of -32,768 to 32,767.
Example Program to show the data types:
#include<iostream>using namespace std;
int main(){ int a; //declaration a = 12; //initialization cout<<"size of int"<<sizeof(a)<<endl;
float b; b = 3.14; cout<<"size of float"<<sizeof(b)<<endl;
char c; cout<<"size of char"<<sizeof(c)<<endl;
bool d; cout<<"size of boolean"<<sizeof(d)<<endl;}
Output:size of int4size of float4size of char1size of boolean1
- Integer - 1, 4, 100
- Float - 3.14, 5.22, 4.243432
- Character - @, |, c, f
- Boolean - True, False or (0, 1)
- Integer occupies 4 bytes. Since 1 Byte = 8 bits, integer occupies 32 bits in a memory. Range of unsigned integer is 0 to (2^32)-1. To store negative integer, the left most (most significant bit [MSB]) is used to define the sign of the integer. So, if the left most bit is filled with 1, it means that the integer is negative and if it is 0, then it is a positive integer. Therefore, the range of signed integer is -(2^31) to (2^31)-1.
- Float occupies 4 bytes. We can store up to 7 decimal digits. If we want to store more digits, then we use another data type called Double that occupies 8 bytes. It can store up to 15 decimal digits.
- Char occupies 1 byte. We use ASCII table that is attached below.
- Boolean data type stores boolean values. True is represented by 1 and False is represented by 0. It occupies 1 byte.
Type Modifiers
- signed: It occupies 4 bytes & has a range of -2,147,483,648 to 2,147,483,647
- unsigned: It occupies 4 bytes & has a range of 0 to 4,294,967,295
- long: It occupies 8 bytes & has a range of -9,223,372,036,854,775,808 to -9,223,372,036,854,775,807
- short: It occupies 4 bytes & has a range of -32,768 to 32,767.
#include<iostream>
using namespace std;
int main(){
int a; //declaration
a = 12; //initialization
cout<<"size of int"<<sizeof(a)<<endl;
float b;
b = 3.14;
cout<<"size of float"<<sizeof(b)<<endl;
char c;
cout<<"size of char"<<sizeof(c)<<endl;
bool d;
cout<<"size of boolean"<<sizeof(d)<<endl;
Comments
Post a Comment