


Khusboo Tayal
Data types specify the type of data a variable can hold. Each data type has a specific size and range, which defines the type of values it can store.
These are the basic data types provided by C++.
| Data Type | Size (Typical) | Range |
|---|---|---|
| int | 4 bytes | -2,147,483,648 to 2,147,483,647 |
| float | 4 bytes | ±3.4E-38 to ±3.4E+38 |
| double | 8 bytes | ±1.7E-308 to ±1.7E+308 |
| char | 1 byte | -128 to 127 (or 0 to 255) |
| bool | 1 byte | true (1) or false (0) |
| wchar_t | 2-4 bytes | Wide character type (Unicode) |
| void | 0 bytes | No value |
#include <iostream>
using namespace std;
int main() {
int age = 25;
float height = 5.8f;
double weight = 72.5;
char grade = 'A';
bool isStudent = true;
cout << "Age: " << age << endl;
cout << "Height: " << height << " feet" << endl;
cout << "Weight: " << weight << " kg" << endl;
cout << "Grade: " << grade << endl;
cout << "Is Student: " << isStudent << endl;
return 0;
}
Age: 25 Height: 5.8 feet Weight: 72.5 kg Grade: A Is Student: 1
These are data types derived from primitive types.
1. Arrays: Collection of elements of the same type.
int numbers[5] = {1, 2, 3, 4, 5};
2. Pointers: Variables that store the memory address of another variable.
int x = 10; int *ptr = &x;
3. References: An alias for another variable.
int y = 20; int &ref = y;
4. Function Types: Return and parameter types of functions.
int add(int a, int b) {
return a + b;
}These allow you to create your own custom types.
1. Structures (struct)
struct Student {
string name;
int age;
char grade;
};2. Unions (union)
union Data {
int intValue;
float floatValue;
};3. Enumerations (enum)
enum Color {RED, GREEN, BLUE};
4. Classes (class) (Advanced - Covered in OOP Chapter)
class Car {
public:
string brand;
int year;
};Variables are containers for storing data. They are named storage locations in memory.
data_type variable_name;
int age; float height; char grade;
Variables can be assigned a value at the time of declaration.
int age = 25; float height = 5.9; char grade = 'A';
1. Direct Initialization:
int number = 10;
2. Copy Initialization:
int number(10);
3. Uniform Initialization (C++11):
int number{10};
int a = 5, b = 10, c = 15;
The scope defines where a variable can be accessed.
1. Local Variables: Declared inside functions or blocks.
void display() {
int x = 10; // Local variable
}2. Global Variables: Declared outside any function.
int count = 0; // Global variable
void increment() {
count++;
}3. Static Variables: Retain their value between function calls.
void demo() {
static int num = 0;
num++;
cout << num << endl;
}#include <iostream>
using namespace std;
int globalVar = 100; // Global variable
void display() {
int localVar = 50; // Local variable
static int staticVar = 0; // Static variable
staticVar++;
cout << "Local Variable: " << localVar << endl;
cout << "Static Variable: " << staticVar << endl;
cout << "Global Variable: " << globalVar << endl;
}
int main() {
display();
display();
return 0;
}
Local Variable: 50 Static Variable: 1 Global Variable: 100 Local Variable: 50 Static Variable: 2 Global Variable: 100
Constants are variables whose value cannot change during program execution.
1. Using const Keyword:
const float PI = 3.14159;
2. Using #define (Preprocessor Directive):
#define MAX_SIZE 100
Type modifiers allow you to adjust the properties of a data type.
| Modifier | Usage | Example |
|---|---|---|
| signed | Allows negative and positive | signed int x; |
| short | Reduces size (2 bytes) | short int z; |
| long | Increases size (8 bytes) | long int a; |
#include <iostream>
using namespace std;
int main() {
unsigned int u = 4294967295;
signed int s = -2147483648;
short int sh = 32767;
long int l = 9223372036854775807;
cout << "Unsigned Int: " << u << endl;
cout << "Signed Int: " << s << endl;
cout << "Short Int: " << sh << endl;
cout << "Long Int: " << l << endl;
return 0;
}In this tutorial, you learned: