← Back to C++ Course | Chapter 3: Operators | Lesson 9 of 9

C++ में Operator Precedence

Operator precedence वह order है जो C++ तब follow करता है जब एक line में कई operators हों, school में addition से पहले multiplication करने जैसा। Brackets आपको order खुद decide करने देते हैं।
Syntax
cpp
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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मान लेना कि a + b * c left to right evaluate होता है, जब multiplication addition से पहले होता है।
  2. a & b == c लिखना, जहाँ == & से tighter bind करता है और एक surprising result देता है।
  3. 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 होते हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.