Increment Operator and Decrement Operator in C Language with Examples इन्क्रीमेंट ऑपरेटर (++) एवं डेक्रेमेंट ऑपरेटर (--)
Increment and Decrement operators are unary operators in C language. The Increment operator (++) increases the value of a variable by 1, whereas the Decrement operator (--) decreases the value of a variable by 1.
C भाषा में Increment तथा Decrement Operators, Unary Operators होते हैं। Increment Operator (++) किसी Variable के मान में 1 की वृद्धि करता है, जबकि Decrement Operator (--) किसी Variable के मान में 1 की कमी करता है।
There are two types of Increment and Decrement operators:
Pre-Increment and Pre-Decrement
Post-Increment and Post-Decrement
इन Operators के दो प्रकार होते हैं:
Pre-Increment तथा Pre-Decrement
Post-Increment तथा Post-Decrement
1. Increment Operator (++)
The Increment operator (++) increases the value of a variable by 1.
Increment Operator (++) किसी Variable के मान में 1 की वृद्धि करता है।
There are two types of Increment operators:
A. Pre-Increment (++a)
In Pre-Increment, the value of the variable is increased by 1 first, and then the updated value is used in the expression.
Pre-Increment में सबसे पहले Variable के मान में 1 की वृद्धि की जाती है तथा उसके बाद Updated Value का उपयोग Expression में किया जाता है।
Example:
#include <stdio.h>int main(void){int a = 10, b;b = ++a;printf("Value of a = %d\n", a);printf("Value of b = %d\n", b);return 0;}
Output:
Value of a = 11Value of b = 11
Explanation (व्याख्या):
Initially,
a = 10.++afirst increases the value ofato 11.The updated value 11 is assigned to
b.प्रारंभ में
a = 10है।++aसबसे पहलेaका मान 11 कर देता है।Updated Value 11 को Variable
bमें Assign किया जाता है।
B. Post-Increment (a++)
In Post-Increment, the current value of the variable is used in the expression first, and then the variable is increased by 1.
Post-Increment में सबसे पहले Variable के वर्तमान मान का उपयोग Expression में किया जाता है तथा उसके बाद Variable के मान में 1 की वृद्धि होती है।
Example:
#include <stdio.h>int main(void){int a = 10, b;b = a++;printf("Value of a = %d\n", a);printf("Value of b = %d\n", b);return 0;}
Output:
Value of a = 11Value of b = 10
Explanation (व्याख्या):
Initially,
a = 10.a++first provides the current value 10 to the expression.The value 10 is assigned to
b.Afterward, the value of
abecomes 11.प्रारंभ में
a = 10है।a++सबसे पहले Expression को वर्तमान मान 10 प्रदान करता है।मान 10 को Variable
bमें Assign किया जाता है।इसके बाद
aका मान 11 हो जाता है।
2. Decrement Operator (--)
The Decrement operator (--) decreases the value of a variable by 1.
Decrement Operator (--) किसी Variable के मान में 1 की कमी करता है।
There are two types of Decrement operators:
A. Pre-Decrement (--a)
In Pre-Decrement, the value of the variable is decreased by 1 first, and then the updated value is used in the expression.
Pre-Decrement में सबसे पहले Variable के मान में 1 की कमी की जाती है तथा उसके बाद Updated Value का उपयोग Expression में किया जाता है।
Example:
#include <stdio.h>int main(void){int a = 10, b;b = --a;printf("Value of a = %d\n", a);printf("Value of b = %d\n", b);return 0;}
Output:
Value of a = 9Value of b = 9
Explanation (व्याख्या):
Initially,
a = 10.--afirst decreases the value ofato 9.The updated value 9 is assigned to
b.प्रारंभ में
a = 10है।--aसबसे पहलेaका मान 9 कर देता है।Updated Value 9 को Variable
bमें Assign किया जाता है।
B. Post-Decrement (a--)
In Post-Decrement, the current value of the variable is used in the expression first, and then the variable is decreased by 1.
Post-Decrement में सबसे पहले Variable के वर्तमान मान का उपयोग Expression में किया जाता है तथा उसके बाद Variable के मान में 1 की कमी होती है।
Example:
#include <stdio.h>int main(void){int a = 10, b;b = a--;printf("Value of a = %d\n", a);printf("Value of b = %d\n", b);return 0;}
Output:
Value of a = 9Value of b = 10
Explanation (व्याख्या):
Initially,
a = 10.a--first provides the current value 10 to the expression.The value 10 is assigned to
b.Afterward, the value of
abecomes 9.प्रारंभ में
a = 10है।a--सबसे पहले Expression को वर्तमान मान 10 प्रदान करता है।मान 10 को Variable
bमें Assign किया जाता है।इसके बाद
aका मान 9 हो जाता है।
Difference Between Pre-Increment and Post-Increment
(Pre-Increment तथा Post-Increment में अंतर)
Pre-Increment (++a) | Post-Increment (a++) |
|---|---|
| First, the variable is incremented. | First, the current value is used. |
| Updated value is used in the expression. | Original value is used in the expression. |
| पहले Variable का मान बढ़ाया जाता है। | पहले वर्तमान मान का उपयोग होता है। |
| Expression uses the updated value. | Expression uses the original value. |
Difference Between Pre-Decrement and Post-Decrement
(Pre-Decrement तथा Post-Decrement में अंतर)
Pre-Decrement (--a) | Post-Decrement (a--) |
|---|---|
| First, the variable is decremented. | First, the current value is used. |
| Updated value is used in the expression. | Original value is used in the expression. |
| पहले Variable का मान घटाया जाता है। | पहले वर्तमान मान का उपयोग होता है। |
| Expression uses the updated value. | Expression uses the original value. |
Combined Example of Increment and Decrement Operators
The following program demonstrates all four operators.
निम्न प्रोग्राम में सभी चार Increment तथा Decrement Operators को प्रदर्शित किया गया है।
#include <stdio.h>int main(void){int a, b;a = 10;b = ++a;printf("Pre-Increment: a = %d, b = %d\n", a, b);a = 10;b = a++;printf("Post-Increment: a = %d, b = %d\n", a, b);a = 10;b = --a;printf("Pre-Decrement: a = %d, b = %d\n", a, b);a = 10;b = a--;printf("Post-Decrement: a = %d, b = %d\n", a, b);return 0;}
Output:
Pre-Increment: a = 11, b = 11Post-Increment: a = 11, b = 10Pre-Decrement: a = 9, b = 9Post-Decrement: a = 9, b = 10
Important Points About Increment and Decrement Operators
(महत्वपूर्ण बिंदु)
Increment (
++) and Decrement (--) are unary operators because they operate on one variable.Increment (
++) तथा Decrement (--) Unary Operators हैं क्योंकि ये एक Variable पर कार्य करते हैं।Pre-Increment and Pre-Decrement modify the variable before its updated value is used in the expression.
Pre-Increment तथा Pre-Decrement में Variable के मान को बदलने के बाद Updated Value का उपयोग Expression में किया जाता है।
Post-Increment and Post-Decrement use the current value in the expression before the variable is updated.
Post-Increment तथा Post-Decrement में Variable का मान बदलने से पहले वर्तमान Value का उपयोग Expression में किया जाता है।
Increment and Decrement operators can be used with suitable modifiable variables, but they cannot be applied to constants or expressions that are not modifiable.
Increment तथा Decrement Operators का उपयोग उपयुक्त Modifiable Variables के साथ किया जा सकता है, लेकिन Constants या ऐसे Expressions के साथ नहीं किया जा सकता जिनमें परिवर्तन संभव न हो।
Avoid modifying the same variable more than once in a single expression without proper sequencing. Such expressions can produce undefined behavior.
उचित Sequencing के बिना एक ही Expression में किसी Variable को एक से अधिक बार Modify करने से बचना चाहिए। ऐसे Expressions Undefined Behavior उत्पन्न कर सकते हैं।
Increment and Decrement operators are frequently used in C programming, especially in loops, counters, and expressions. Understanding the difference between Pre-Increment, Post-Increment, Pre-Decrement, and Post-Decrement is important for writing correct programs and solving output-based questions.
C Programming में Increment तथा Decrement Operators का उपयोग मुख्य रूप से Loops, Counters तथा Expressions में किया जाता है। Pre-Increment, Post-Increment, Pre-Decrement तथा Post-Decrement के बीच अंतर समझना सही Programs लिखने तथा Output-Based Questions हल करने के लिए महत्वपूर्ण है।
Comments
Post a Comment