Basics
Operators in C++
C Jaise, Kuch Extra Ke Saath
💡 C++ ke operators C jaise hi hain (arithmetic, relational, logical) — bas dhang zyada elegant hai kyunki tum khud bhi apne operators define kar sakte ho (operator overloading, aage seekhenge).
Arithmetic (+, -, *, /, %), relational (==, !=, <, >, <=, >=), logical (&&, ||, !), aur assignment (=, +=, -=) sab C jaise hi hain. C++ mein inhe custom types (classes) ke liye bhi redefine kar sakte ho — jaise + operator do "Vector" objects ko add kar sake, sirf numbers nahi.
int a = 10, b = 3;
cout << a + b << endl; // 13
cout << a % b << endl; // 1
cout << (a > b) << endl; // 1 (true)
cout << (a == b) << endl; // 0 (false)
a += 5; // a = 15
cout << a << endl;➗
C++ ke operators C jaise hi hain (arithmetic, relational, logical) — bas dhang zyada elegant hai kyunki tum khud bhi apne operators define kar sakte ho (operator overloading, aage seekhenge).
1 / 2
⚡ झट से Recap
- Sab C operators C++ mein bhi kaam karte hain
- Operator overloading se custom types ke liye bhi redefine kar sakte ho
- bool type ke saath logical operators natural feel karte hain
इस page में (1 subtopics)
Operator precedence (kaunsa operator pehle evaluate hota hai) C jaisa hi hai — *, / pehle +, - se, waghera. sizeof operator bhi same hai, compile-time par size deta hai.
int a = 2 + 3 * 4; // 14, na ki 20 (* pehle)
cout << a << endl;
cout << sizeof(int) << endl; // 4
cout << sizeof(double) << endl; // 8Tip: Confusion avoid karne ke liye parentheses use karna hamesha safe hai, chahe precedence pata bhi ho — (2 + 3) * 4 clearly readable hai.