C की Operator Precedence
In this page:
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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Operator Associativity
जब same expression में दो operators equal precedence share करते हैं, associativity tie decide करती है: ज़्यादातर C operators left-to-right associate करते हैं, लेकिन कुछ (जैसे assignment) इसकी बजाय right-to-left associate करते हैं।
उदाहरण: Operator Associativity
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Arithmetic Hierarchy
*, /, और % सब same precedence level share करते हैं और + और - से पहले evaluate होते हैं, इसलिए 2 + 3 * 4 पहले multiplication evaluate करता है, 20 की बजाय 14 देते हुए।
उदाहरण: Arithmetic Hierarchy
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Parentheses इस्तेमाल करना
Parentheses हमेशा C के default precedence rules से पहले आते हैं, इसलिए एक expression के किसी हिस्से को () में wrap करना इसे पहले evaluate होने पर force करता है -- memorized rules पर depend करने की बजाय अपने intended order of operations को explicit बनाने का सबसे clear तरीका।
उदाहरण: Using Parentheses
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि
a + b * cleft to right evaluate होता है, जब*+से पहले चलता है। a & b == cलिखना, जहाँ==&से tighter bind करता है।- 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 होता है।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: