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

C में Increment और Decrement

Increment और decrement एक जोड़ते या घटाते हैं, एक tally counter click करने जैसा। यह मायने रखता है कि change value इस्तेमाल होने से पहले होता है या बाद में।
Syntax
c
++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

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

Postfix Increment

Postfix increment (x++) पहले आस-पास के expression में variable की current value इस्तेमाल करता है, और +1 increase बाद में ही apply करता है -- इसलिए y = x++; y को वह value देता है जो x के increment होने *से पहले* थी।

उदाहरण: Postfix Increment

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

Prefix Decrement

Prefix decrement (--x) पहले 1 subtract करता है और updated value ही expression में इस्तेमाल होती है, prefix increment की तरह लेकिन value बढ़ाने की बजाय घटाते हुए।

उदाहरण: Prefix Decrement

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

Postfix Decrement

Postfix decrement (x--) पहले expression में current value इस्तेमाल करता है, फिर variable को बाद में घटाता है -- postfix increment की timing जैसा लेकिन जोड़ने की बजाय subtract करते हुए।

उदाहरण: Postfix Decrement

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

Expressions में Mixing

एक complex expression के अंदर कई increment/decrement operators मिलाना (जैसे x++ + ++x) C में undefined या compiler-dependent behavior की ओर ले जाता है -- ऐसे operators को अपने अलग separate statement में रखना best practice है।

उदाहरण: Mixing in Expressions

c
// 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;
}
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. यह मान लेना कि एक बड़े expression के अंदर x++ और ++x same हैं, जब postfix पुरानी value return करता है।
  2. x = x++; लिखना, जो C में undefined behavior है।
  3. एक expression में same variable को ++ के साथ दो बार इस्तेमाल करना, जैसे a[i] = i++;।

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.