C++ chapter 2

🟩 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

TypeKeywordExample
Integerint10
Floatingfloat3.14
Doubledouble9.81
Characterchar‘A’
Booleanbooltrue / false
Voidvoid—

🔹 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 → input
  • cout → output

Leave a Comment

Your email address will not be published. Required fields are marked *