🟩 Chapter 2: Data Types, Variables, and Operators
🔹 1. Tokens in C++
Smallest building blocks:
- Keywords – reserved words (e.g.,Â
int,Âclass,Âif) - Identifiers – names for variables/functions
- Literals – constants (
10,Â'A',Â"Hello") - Operators – symbols that perform operations
- Punctuators –Â
;,Â{},Â[],Â()
🔹 2. Data Types
| Type | Keyword | Example |
|---|---|---|
| Integer | int | 10 |
| Floating | float | 3.14 |
| Double | double | 9.81 |
| Character | char | ‘A’ |
| Boolean | bool | true / false |
| Void | void | — |
🔹 3. Variables
Used to store data temporarily.
Syntax:
int age = 18;
char grade = 'A';
Rules:
- Must start with letter or underscore
- No spaces or special symbols
- Case-sensitive
🔹 4. Constants
Values that cannot be changed:
const float PI = 3.14;
🔹 5. Operators
A. Arithmetic
+, -, *, /, %
B. Relational
==, !=, >, <, >=, <=
C. Logical
&&, ||, !
D. Assignment
=, +=, -=, *=, /=
E. Increment/Decrement
++, --
F. Conditional (Ternary)
(a > b) ? a : b;
G. Bitwise
&, |, ^, ~, <<, >>
🔹 6. Type Casting
Convert data from one type to another.
float a = (float)5 / 2;
🔹 7. Input/Output in C++
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
cin >> age;
cout << "You entered: " << age;
return 0;
}
cin → inputcout → output
