📦
Basics

Variables & cin/cout

Input/Output Basics
💡 cin aur cout ek do-tarfa conveyor belt jaisa hai — cout screen ki taraf data bhejta hai (>> ki jagah <<), cin keyboard se data uthata hai (>> operator, jaise "extract from stream").

C++ mein variables C jaise hi hain (int, float, double, char, bool naya hai — true/false ke liye), lekin input/output ke liye cin/cout stream-based approach use karta hai, printf/scanf ki jagah — type-safe hai (format specifiers %d/%f yaad nahi rakhne padte).

cout << value se print hota hai, cin >> variable se input milta hai. Multiple values ko chain kar sakte ho: cout << a << " " << b; — bahut readable lagta hai comparison mein.

#include <iostream>
using namespace std;

int main() {
  int age;
  bool isStudent = true;   // bool naya hai C se — true/false

  cout << "Apni age batao: ";
  cin >> age;

  cout << "Tum " << age << " saal ke ho" << endl;
  cout << "Student ho? " << isStudent << endl;  // 1 (true)
  return 0;
}
📦
cin aur cout ek do-tarfa conveyor belt jaisa hai — cout screen ki taraf data bhejta hai (>> ki jagah <<), cin keyboard se data uthata hai (>> operator, jaise "extract from stream").
1 / 2
⚡ Quick Recap
  • cin >> = input lo (extract from stream)
  • cout << = output do (insert into stream)
  • bool = naya type, true/false ke liye
On this page (2 subtopics)

C++11 se auto keyword compiler ko batata hai "khud type figure out karo" initialization value se — code likhna aasan ho jaata hai, especially complex types (jaise iterators) ke liye, lekin readability ka khayal rakhte hue use karo.

auto x = 10;        // compiler samajhta hai: int
auto y = 3.14;       // double
auto name = "Aarav"; // const char*
auto z = x + y;       // double (int + double = double)

cout << z << endl;
⚠️Common Mistake: auto overuse karne se code kabhi-kabhi padhna mushkil ho jaata hai — jab type clear nahi hai (function return type se), explicit type likhna better hai readability ke liye.

C++ ka std::string (header <string>) C ke char array se bahut behtar hai — dynamic size (khud grow hota hai), built-in operators (+, ==), aur bahut saare helper methods (.length(), .substr(), .find()).

#include <string>
using namespace std;

string name = "Aarav";
name += " Kumar";        // + operator se concatenation!
cout << name << endl;    // "Aarav Kumar"
cout << name.length() << endl; // 11

if (name == "Aarav Kumar") {  // == operator seedha compare karta hai
  cout << "Match!" << endl;
}
💡Tip: C mein strcmp() use karna padta tha compare karne ke liye — C++ mein == operator seedha kaam karta hai kyunki string class ke liye operator overloaded hai.