C++ में Operator Precedence
In this page:
result = a + b * c; // * / % run before + -
result = (a + b) * c; // parentheses run first
Arithmetic Operator Precedence
C++ basic math में सिखाए same order-of-operations rules apply करता है: multiplication और division addition और subtraction से पहले evaluate होते हैं, इसलिए 2 + 3 * 4 14 evaluate होता है, 20 नहीं।
Precedence को गलत समझना subtly गलत calculations का एक common source है जो बिना किसी error के compile होती हैं।
उदाहरण: Arithmetic Operator Precedence
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare result and set it to 2 + 3 * 4
int result = 2 + 3 * 4;
// Print to the console with cout
std::cout << result << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Precedence Force करने के लिए Parentheses इस्तेमाल करना
एक expression के किसी हिस्से को parentheses में wrap करना इसे पहले evaluate होने पर force करता है, default precedence rules को पूरी तरह override करते हुए — (2 + 3) * 4 20 evaluate होता है exactly क्योंकि addition अब explicitly group किया गया है।
जब यह doubt हो कि एक expression कैसे evaluate होगा, parentheses add करने की कोई cost नहीं और यह compiler और future readers दोनों के लिए हर ambiguity हटा देता है।
उदाहरण: Using Parentheses to Force Precedence
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare result and set it to (2 + 3) * 4
int result = (2 + 3) * 4;
// Print to the console with cout
std::cout << result << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Relational और Logical Precedence
> और < जैसे relational operators && और || जैसे logical operators से tighter bind करते हैं, यही वजह है कि a > 5 && b < 10 हर comparison के आस-पास explicit parentheses की ज़रूरत के बिना (a > 5) && (b < 10) की तरह parse होता है।
सिंपल conditions के लिए बिना parentheses इस default ordering पर depend करना ठीक है, लेकिन बड़े expressions को clarity के लिए explicit grouping से फायदा होता है।
उदाहरण: Relational and Logical Precedence
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int a = 6, b = 5;
// Declare result and set it to a > 5 && b < 10
bool result = a > 5 && b < 10;
// Print to the console with cout
std::cout << result << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Left-to-Right Associativity
जब दो operators same precedence level share करते हैं, जैसे a - b + c, C++ उन्हें left to right evaluate करता है, इसे (a - b) + c की तरह treat करते हुए।
यह left-to-right rule ज़्यादातर arithmetic और comparison operators follow करते हैं, और यह उस तरीके से match करता है जिस तरह आप naturally expression पढ़ेंगे।
उदाहरण: Left-to-Right Associativity
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int a = 10, b = 3, c = 2;
// Declare result and set it to a - b + c
int result = a - b + c;
// Print to the console with cout
std::cout << result << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Assignment की Right-to-Left Associativity
Assignment left-to-right evaluation का एक exception है — यह right to left associate करता है, जो exactly वह चीज़ है जो a = b = c = 0; जैसे chained assignment को काम करने देती है: c = 0 पहले होता है, फिर वह result b को assign होता है, और आखिर में a को।
यह आपको एक compact line में कई variables को same value initialize करने देता है।
उदाहरण: Right-to-Left Associativity of Assignment
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int a, b, c;
a = b = c = 5;
// Print to the console with cout
std::cout << a << " " << b << " " << c << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि
a + b * cleft to right evaluate होता है, जब multiplication addition से पहले होता है। a & b == cलिखना, जहाँ==&से tighter bind करता है और एक surprising result देता है।- intent clear बनाने के लिए parentheses इस्तेमाल न करना, ताकि readers और compiler operations को उम्मीद से अलग group करें।
- Arithmetic, relational, logical, bitwise, और assignment operators calculations और comparisons perform करते हैं।
- Increment और decrement operators एक value को एक से बदलते हैं, और ternary operator एक short conditional है।
- Operator precedence decide करता है कि expressions किस order में evaluate होते हैं।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: