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

C Increment & Decrement

Increment and decrement add or subtract one, like clicking a tally counter. It matters whether the change happens before or after the value is used.
Syntax
c
++variable;  // prefix: increase, then use value
variable++;  // postfix: use value, then increase
--variable;
variable--;

Prefix Increment

Prefix increment (++x) adds 1 to the variable's value first, and the expression it's part of then uses that already-updated value -- so y = ++x; gives y the value of x after the increase.

Example: 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++) uses the variable's current value in the surrounding expression first, and only applies the +1 increase afterward -- so y = x++; gives y the value x had *before* it was incremented.

Example: 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) subtracts 1 first and the updated value is what gets used in the expression, mirroring prefix increment but decreasing instead of increasing the value.

Example: 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--) uses the current value in the expression first, then decreases the variable afterward -- mirroring postfix increment's timing but subtracting instead of adding.

Example: 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;
}

Mixing in Expressions

Mixing several increment/decrement operators inside one complex expression (like x++ + ++x) leads to undefined or compiler-dependent behavior in C -- it's best practice to keep such operators in their own separate statement.

Example: 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. #}
Common Mistakes
  1. Assuming x++ and ++x are the same inside a larger expression, when postfix returns the old value.
  2. Writing x = x++;, which is undefined behavior in C.
  3. Using the same variable twice with ++ in one expression, such as 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.