C में Increment और Decrement
In this page:
++variable; // prefix: increase, then use value
variable++; // postfix: use value, then increase
--variable;
variable--;
Prefix Increment
Prefix increment (++x) पहले variable की value में 1 जोड़ता है, और जिस expression का यह हिस्सा है वह फिर उस already-updated value इस्तेमाल करता है -- इसलिए y = ++x; y को increase के बाद x की value देता है।
उदाहरण: Prefix Increment
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare x and set it to 5
int x = 5;
// Declare y and set it to ++x
int y = ++x;
// 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
Postfix Increment
Postfix increment (x++) पहले आस-पास के expression में variable की current value इस्तेमाल करता है, और +1 increase बाद में ही apply करता है -- इसलिए y = x++; y को वह value देता है जो x के increment होने *से पहले* थी।
उदाहरण: Postfix Increment
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare x and set it to 5
int x = 5;
// Declare y and set it to x++
int y = x++;
// 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
Prefix Decrement
Prefix decrement (--x) पहले 1 subtract करता है और updated value ही expression में इस्तेमाल होती है, prefix increment की तरह लेकिन value बढ़ाने की बजाय घटाते हुए।
उदाहरण: Prefix Decrement
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare x and set it to 5
int x = 5;
// Declare y and set it to --x
int y = --x;
// 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
Postfix Decrement
Postfix decrement (x--) पहले expression में current value इस्तेमाल करता है, फिर variable को बाद में घटाता है -- postfix increment की timing जैसा लेकिन जोड़ने की बजाय subtract करते हुए।
उदाहरण: Postfix Decrement
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare x and set it to 5
int x = 5;
// Declare y and set it to x--
int y = x--;
// 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
Expressions में Mixing
एक complex expression के अंदर कई increment/decrement operators मिलाना (जैसे x++ + ++x) C में undefined या compiler-dependent behavior की ओर ले जाता है -- ऐसे operators को अपने अलग separate statement में रखना best practice है।
उदाहरण: Mixing in Expressions
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare x and set it to 5
int x = 5;
x++;
++x;
// Print formatted text to the screen
printf("%d", x);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि एक बड़े expression के अंदर
x++और++xsame हैं, जब postfix पुरानी value return करता है। x = x++;लिखना, जो C में undefined behavior है।- एक expression में same variable को
++के साथ दो बार इस्तेमाल करना, जैसेa[i] = i++;।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: