-
Aim: To understand and explore the various data types available in C++ and how they are used to store different types of values.
-
Tools Used - IDE, Basic data types, header files.
-
Theory: In C++, data types define the type and size of data that a variable can hold. Choosing the correct data type is essential for efficient memory usage and accurate program behavior. C++ provides a variety of data types, which are broadly categorized into:
🔹 1. Primary (Built-in) Data Types in C++:
int – A data type used to store integer (whole) numbers. 🔸 Example: int a = 10;
float – Used to store decimal numbers with single precision. 🔸 Example: float pi = 3.14;
double – Stores decimal numbers with double precision (more accurate than float). 🔸 Example: double rate = 5.6789;
char – Used to store a single character, enclosed in single quotes. 🔸 Example: char grade = 'A';
bool – Represents Boolean values, either true or false. 🔸 Example: bool status = true;
void – Indicates no return value, mostly used for functions that don’t return anything. 🔸 Example: void display();
🔹 2. Type Modifiers:
Modifiers alter the size or range of the basic data types:
-signed, unsigned
-short, long
This C++ program demonstrates how to declare different data types—int, float, double, char, string, and bool—and display both their values and memory sizes using the sizeof() function. It helps in understanding the storage requirements of various data types in C++.
ALGORITHM:
1> Start
2> Declare an int variable, take input, and display its value and size
3> Declare a float variable, take input, and display its value and size
4> Declare a double variable, take input, and display its value and size
5> Declare a char variable, take input, and display its value and size
6> Declare a string variable, take input, and display its value and object size
7> Declare two bool variables (true, false) and display value and size
8> End
- Conclusion:
In this experiment, we learned about various data types in C++, such as int, float, char, double, and bool. We understood their sizes, usage, and how they store different types of values.By writing simple programs, we observed how data types help in declaring variables correctly.This experiment gave us a strong foundation for handling data efficiently in C++.