← 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
c
result = a + b * c;    // * / % run before + -
result = (a + b) * c;  // parentheses run first

Precedence क्या है?

Operator precedence वह fixed order है जो C यह decide करने के लिए इस्तेमाल करता है कि एक mixed expression में कौन सा operator पहले evaluate होगा, algebra के "add करने से पहले multiply करो" rule जैसी same logic follow करते हुए जो आप हाथ से apply करते।

उदाहरण: What is Precedence?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare result and set it to 2 + 3 * 4
	int result = 2 + 3 * 4;
	// Print formatted text to the screen
	printf("%d", result);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Operator Associativity

जब same expression में दो operators equal precedence share करते हैं, associativity tie decide करती है: ज़्यादातर C operators left-to-right associate करते हैं, लेकिन कुछ (जैसे assignment) इसकी बजाय right-to-left associate करते हैं।

उदाहरण: Operator Associativity

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int x, y;
	x = y = 10;
	// Print formatted text to the screen
	printf("%d %d", x, y);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Arithmetic Hierarchy

*, /, और % सब same precedence level share करते हैं और + और - से पहले evaluate होते हैं, इसलिए 2 + 3 * 4 पहले multiplication evaluate करता है, 20 की बजाय 14 देते हुए।

उदाहरण: Arithmetic Hierarchy

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare result and set it to 2 + 3 * 4
	int result = 2 + 3 * 4;
	// Print formatted text to the screen
	printf("%d", result);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Parentheses इस्तेमाल करना

Parentheses हमेशा C के default precedence rules से पहले आते हैं, इसलिए एक expression के किसी हिस्से को () में wrap करना इसे पहले evaluate होने पर force करता है -- memorized rules पर depend करने की बजाय अपने intended order of operations को explicit बनाने का सबसे clear तरीका।

उदाहरण: Using Parentheses

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare result and set it to (2 + 3) * 4
	int result = (2 + 3) * 4;
	// Print formatted text to the screen
	printf("%d", result);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Relational और Logical Priority

Arithmetic operators relational operators से tighter bind करते हैं, जो बदले में logical operators से tighter bind करते हैं, इसलिए a + b > c && d उस order में addition, फिर comparison, फिर logical AND evaluate करता है।

उदाहरण: Relational and Logical Priority

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int a = 2, b = 3, c = 4, d = 1;
	// Declare result and set it to a + b > c && d
	int result = a + b > c && d;
	// Print formatted text to the screen
	printf("%d", result);
	// 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 होता है, जब * + से पहले चलता है।
  2. a & b == c लिखना, जहाँ == & से tighter bind करता है।
  3. intended order को clear बनाने के लिए parentheses इस्तेमाल न करना।
चैप्टर सारांश
  • Operators values पर काम करते हैं: arithmetic, relational, logical, bitwise, और assignment operators हर एक अलग purpose serve करते हैं।
  • Increment, decrement, और ternary operator values बदलने या दो results में से चुनने के compact तरीके देते हैं।
  • Operator precedence decide करता है कि एक mixed expression किस 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.