Basic Data Type and Variable in C++
{tocify} $title={Table of Contents}
Variable:-
In the C++ 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.
Data type:-
A data type determines the type
and the operations that can be performed on the program, In a C++ to declare a type of variable use a various data types
like character, wide 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.
So, we can say Data types define the type of data a variable can
hold.
Data Type in C++:-
1.
Primary(Built-in) Data Types:
These data types
are 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:
Type
Boolean
Character
Integer
Floating point
Double floating point
Valueless
Wide character
|
Keywords
Bool
Char
int
float
double
void
wchar_t
|
§
Integer
: Int is keyword of an integer, it is use to declare a integer in a C++ with the help of it’s keyword. Integers typically requires 4 bytes of memory space and ranges from -2147483648 to 2147483647.
How to write a integer in C++ it’s given blew:-
int a = 10;
§ Character:
To declare a character in C++ use Char. Char is a keyword of character. Character data
type is used for storing characters. Characters typically requires 1 byte of
memory space and ranges from -128 to 127 or 0 to 255.
How to write a character in C++ it’s given blew:-
char ch = 'I';
§ Boolean:
Bool is keyword of Boolean. Boolean data type is used for storing
boolean or logical values. A boolean variable can store either true or false.
How to write a boolean in C++ it’s given blew:-
bool b = true;
§ Floating Point:
Floating Point data type is used for storing single precision
floating point values or decimal values. Float variables typically requires 4
byte of memory space.
How to write a float in C++ it’s given blew:-
float num = 12.789;
§ 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.
How to write a double in C++
it’s given blew:-
double num = 23.987;
§ void: Void means
without any value. void datatype represents a valueless entity. Void data type
is used for those function which does not returns a value.
§ Wide character:- Wide character
data type is also a character data type but this data type has size greater
than the normal 8-bit datatype. Represented by wchar_t.
It is generally 2 or 4 bytes long.
Datatype Modifiers: As the name
implies, datatype modifiers are used with the built-in data types to modify the
length of data that a particular data type can hold. Data type modifiers
available in C++ are:
§
Signed
§
Unsigned
§
Short
§ Long
Below table summarizes the modified
size and range of built-in datatypes when combined with the type modifiers:-
DATA TYPE |
SIZE(IN BYTES) |
RANGE |
Short int
|
2
|
-32,768
to 32,767
|
Unsigned int
|
2
|
0 to 65,535
|
Unsigned int
|
4
|
0 to
4,294,967,295
|
Int
|
4
|
-2,147,483,648 to 2,147,483,647
|
Long int
|
4
|
-2,147,483,648
to 2,147,483,647
|
Unsigned
long int
|
4
|
0 to 4,294,967,295
|
Long long
int
|
8
|
-(2^63)
to (2^63)-1
|
Unsigned long
long int
|
8
|
0 to 18,446,744,073,709,551,615
|
Signed char
|
1
|
-128 to
127
|
Unsigned char
|
1
|
0 to 255
|
Float
|
4
|
|
Double
|
8
|
|
Long double
|
12
|
|
Wchar_t
|
2 or 4
|
1 wide character
|
We can display the size of all the data types by using
the size_of() function and passing the keyword of the datatype as argument to
this function as shown below:
// C++ program to sizes of data types#include<iostream>using namespace std;void main(){ cout << "Size of char :
" << sizeof(char) << " byte" << endl; cout << "Size of int : " << sizeof(int) << " bytes" << endl; cout << "Size of short int :
" << sizeof(short int) << " bytes" << endl; cout << "Size of long int :
" << sizeof(long int) << " bytes" << endl; cout << "Size of signed long
int : " << sizeof(signed long int) << " bytes" << endl; cout << "Size of unsigned
long int : " << sizeof(unsigned long int) << " bytes" << endl; cout << "Size of float :
" << sizeof(float) << " bytes" <<endl; cout << "Size of double :
" << sizeof(double) << " bytes" << endl; cout << "Size of wchar_t :
" << sizeof(wchar_t) << " bytes" <<endl; getch ();} |
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() operator 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 −
Output:-
Size of char : 1 byte
Size of int : 4 bytes
Size of short int : 2 bytes
Size of long int : 8 bytes
Size of signed long int : 8 bytes
Size of unsigned long int : 8 bytes
Size of float : 4 bytes
Size of double : 8 bytes
Size of wchar_t : 4 bytes
|
2.
User Defined Data Types:
I.
Structure
II.
Union
III. Class
IV. Enumeration
3.
Derived Data Types:
I.
Array
II.
Function
III. Pointer
IV. Reference
📘 IT Tech Language
☁️ Cloud Computing - What is Cloud Computing – Simple Guide
- History and Evolution of Cloud Computing
- Cloud Computing Service Models (IaaS)
- What is IaaS and Why It’s Important
- Platform as a Service (PaaS) – Cloud Magic
- Software as a Service (SaaS) – Enjoy Software Effortlessly
- Function as a Service (FaaS) – Serverless Explained
- Cloud Deployment Models Explained
🧩 Algorithm - Why We Learn Algorithm – Importance
- The Importance of Algorithms
- Characteristics of a Good Algorithm
- Algorithm Design Techniques – Brute Force
- Dynamic Programming – History & Key Ideas
- Understanding Dynamic Programming
- Optimal Substructure Explained
- Overlapping Subproblems in DP
- Dynamic Programming Tools
🤖 Artificial Intelligence (AI) - Artificial intelligence and its type
- Policy, Ethics and AI Governance
- How ChatGPT Actually Works
- Introduction to NLP and Its Importance
- Text Cleaning and Preprocessing
- Tokenization, Stemming & Lemmatization
- Understanding TF-IDF and Word2Vec
- Sentiment Analysis with NLTK
📊 Data Analyst - Why is Data Analysis Important?
- 7 Steps in Data Analysis
- Why Is Data Analysis Important?
- How Companies Can Use Customer Data and Analytics to Improve Market Segmentation
- Does Data Analytics Require Programming?
- Tools and Software for Data Analysis
- What Is the Process of Collecting Import Data?
- Data Exploration
- Drawing Insights from Data Analysis
- Applications of Data Analysis
- Types of Data Analysis
- Data Collection Methods
- Data Cleaning & Preprocessing
- Data Visualization Techniques
- Overview of Data Science Tools
- Regression Analysis Explained
- The Role of a Data Analyst
- Time Series Analysis
- Descriptive Analysis
- Diagnostic Analysis
- Predictive Analysis
- Pescriptive Analysis
- Structured Data in Data Analysis
- Semi-Structured Data & Data Types
- Can Nextool Assist with Data Analysis and Reporting?
- What Kind of Questions Are Asked in a Data Analyst Interview?
- Why Do We Use Tools Like Power BI and Tableau for Data Analysis?
- The Power of Data Analysis in Decision Making: Real-World Insights and Strategic Impact for Businesses
📊 Data Science - The History and Evolution of Data Science
- The Importance of Data in Science
- Why Need Data Science?
- Scope of Data Science
- How to Present Yourself as a Data Scientist?
- Why Do We Use Tools Like Power BI and Tableau
- Data Exploration: A Simple Guide to Understanding Your Data
- What Is the Process of Collecting Import Data?
- Understanding Data Types
- Overview of Data Science Tools and Techniques
- Statistical Concepts in Data Science
- Descriptive Statistics in Data Science
- Data Visualization Techniques in Data Science
- Data Cleaning and Preprocessing in Data Science
🧠 Machine Learning (ML) - How Machine Learning Powers Everyday Life
- Introduction to TensorFlow
- Introduction to NLP
- Text Cleaning and Preprocessing
- Sentiment Analysis with NLTK
- Understanding TF-IDF and Word2Vec
- Tokenization and Lemmatization
🗄️ SQL
💠 C++ Programming - Introduction of C++
- Brief History of C++ || History of C++
- Characteristics of C++
- Features of C++ || Why we use C++ || Concept of C++
- Interesting Facts About C++ || Top 10 Interesting Facts About C++
- Difference Between OOP and POP || Difference Between C and C++
- C++ Program Structure
- Tokens in C++
- Keywords in C++
- Constants in C++
- Basic Data Types and Variables in C++
- Modifiers in C++
- Comments in C++
- Input Output Operator in C++ || How to take user input in C++
- Taking User Input in C++ || User input in C++
- First Program in C++ || How to write Hello World in C++ || Writing First Program in C++
- How to Add Two Numbers in C++
- What are Control Structures in C++ || Understanding Control Structures in C++
- What are Functions and Recursion in C++ || How to Define and Call Functions
- Function Parameters and Return Types in C++ || Function Parameters || Function Return Types
- Function Overloading in C++ || What is Function Overloading
- Concept of OOP || What is OOP || Object-Oriented Programming Language
- Class in C++ || What is Class || What is Object || How to use Class and Object
- Object in C++ || How to Define Object in C++
- Polymorphism in C++ || What is Polymorphism || Types of Polymorphism
- Compile Time Polymorphism in C++
- Operator Overloading in C++ || What is Operator Overloading
- Python vs C++ || Difference Between Python and C++ || C++ vs Python
🐍 Python - Why Python is Best for Data
- Dynamic Programming in Python
- Difference Between Python and C
- Mojo vs Python – Key Differences
- Sentiment Analysis in Python
🌐 Web Development
🚀 Tech to Know & Technology
- The History and Evolution of Data Science
- The Importance of Data in Science
- Why Need Data Science?
- Scope of Data Science
- How to Present Yourself as a Data Scientist?
- Why Do We Use Tools Like Power BI and Tableau
- Data Exploration: A Simple Guide to Understanding Your Data
- What Is the Process of Collecting Import Data?
- Understanding Data Types
- Overview of Data Science Tools and Techniques
- Statistical Concepts in Data Science
- Descriptive Statistics in Data Science
- Data Visualization Techniques in Data Science
- Data Cleaning and Preprocessing in Data Science
Tags
C plus plus

