Data Types in C++
In C++, data types define the kind of data a variable can store. They are broadly categorized as:
- Primitive (Fundamental) Data Types:
int,float,double,char,bool,void - Derived Data Types: Arrays, Pointers, References, Functions
- User-defined Data Types:
struct,class,enum,typedef
Most commonly used primitive types: integer, boolean, character, float, and double. These values are stored in memory locations and assigned to variables with specific names.
Integer
The int data type stores whole numbers (positive or negative) without decimal places.
Example values: -5, -3, 0, 2, 5
Declaration: int age = 25;
Float
The float data type stores numbers with decimal places or in exponential notation.
Example values: -3.43, -2.2, 0.2, 5.9
Declaration: float temperature = 36.6;
Double
The double data type stores decimal numbers like float but with double precision (8 bytes).
Precision: about 15–16 decimal digits.
Example value: 3.14159265358979
Declaration: double pi = 3.14159265358979;
Character
The char data type stores a single character (letter, number, or symbol).
Occupies 1 byte and is enclosed in single quotes.
Example values: 'A', 'z', '3', ' '
Declaration: char grade = 'A';
Boolean
The bool data type can have two values: true (1) or false (0).
Example values: true, false
Declaration: bool isPassed = true;
Escape Sequences
An escape sequence is a combination of a backslash (\) and a special character.
Example: \n (new line), \t (tab), \\ (backslash)
Example usage:
cout << "Hello\nWorld";
Arithmetic Operators
Arithmetic operators perform mathematical calculations:
Addition: +
Subtraction: -
Multiplication: *
Division: /
Modulus (remainder): %
Example usage:
int result = 9 % 4; // result is 1