C - IntroductionC - Hello World ProgramC - Exercise 1C - Basic structure of a C programC - TokensC - Data TypesC - Type ConversionC - Exercise 2C - Character Input Output OperationsC - Input Output operation using scanf and printf functions

Operators

C - Arithmetic OperatorsC - Relational OperatorsC - Logical OperatorsC - Assignment OperatorsC - Increment Decrement OperatorsC - Bitwise Operators

Precedence and Associativity

C - Precedence and AssociativityC - Exercise 3

Conditions

C - If Else decision making statementsC - Switch Case decision making statements

Loop

C - While LoopC - Do While LoopC - For LoopC - Exercise 4

Array

C - ArraysC - Two Dimensional ArraysC - Multi Dimensional ArraysC - Exercise 5

String

C - StringC - Exercise 6C - String Manipulation

Functions

C - FunctionsC - Functions CategoryC - Function Call - Flow of ControlC - RecursionC - Functions and ArraysC - Functions and Strings

Structures

C - StructuresC - Structures and ArraysC - Passing structure to functionC - Function returning structureC - Structure in Structure

Pointers

C - PointersC - Pointers and VariablesC - Pointers and Variables Memory RepresentationC - Pointers Chaining

Pointers and Arrays

C - Pointers and One Dimensional ArrayC - Pointers and Two Dimensional ArrayC - Array of Pointers

Pointers and Strings

C - Pointers and Strings

Pointers and Functions

C - Pointers and Functions - Call by Value and Call by ReferenceC - Function returning pointer

Pointers and Structures

C - Pointers and StructuresC - Pointers and Array of StructuresC - Passing structure pointer to function

Handling Files

C - File Handling - Getting StartedC - File Handling - Read and Write CharactersC - File Handling - Read and Write IntegersC - File Handling - Read and Write multiple dataC - File Handling - Randomly Access Files

Command Line Arguments

C - Command Line Arguments

Dynamic Memory Allocation

C - Dynamic Memory Allocation - Getting StartedC - Dynamic Memory Allocation - malloc functionC - Dynamic Memory Allocation - calloc functionC - Dynamic Memory Allocation - realloc function

C - Increment Decrement Operators

C Programming

In this tutorial we will learn about increment and decrement operators in C programming language.

Adding 1 and subtracting 1 from a variable is common and if we want to perform this task then we can write the following x = x + 1 and x = x - 1.

Increment Operator

In the following example we are increasing the value of x by 1.

//declare variable
int x;

//assign a value
x = 10;

//increase value by 1
x = x + 1;

The same result can be achived by using the increment operator.

We use the increment operator ++ to increase the value of a variable by 1.

In the following example we are increasing the value of x by 1.

//declare variable
int x;

//assign value
x = 10;

//increase value by 1
x++;

Note! x++ is equivalent to x = x + 1 in the above example.

Decrement Operator

Now, lets say we want to decrease the value of a variable x by 1. Then we can write the following code.

//declare variable
int x;

//assign value
x = 10;

//decrease value by 1
x = x - 1;

We can achieve the same result by using the decrement operator.

We use the decrement operator -- when we want to decrease the value of a variable by 1.

In the following example we are decreasing the value of x by 1.

//declare variable
int x;

//assign value
x = 10;

//decrease value by 1
x--;

Note! x-- is equivalent to x = x - 1 in the above code.

Both ++ and -- are unary operators.

Both x++ and ++x means the same thing when they form statement independently.

Similarly, x-- and --x means the same when they form statement independently.

But they behave differently when they are used in expression on the right hand side of an assignment statement.

Lets explore the difference.

Use value then increase

In the following example we are using the increment operator ++ after the variable x on the right hand side of an assignment statement.

So, value of x will be used first then it will be increased by 1.

#include <stdio.h>
int main(void)
{
  //declare variables
  int x, y;
  
  //assign value to x
  x = 10;
  
  printf("Before x++: Value of x = %d\n", x);
  
  //assign value to y
  y = x++ + 10;
  
  printf("y = %d\n", y);
  
  printf("After x++: Value of x = %d\n", x);
  
  return 0;
}

Output

Before x++: Value of x = 10
y = 20
After x++: Value of x = 11

In the above code the value of x is first used in the assignment statement.

we have, y = x++ + 10;
so, we will first use the value of x i.e., 10 and then increase it by 1

y = 10 + 10;
y = 20;

and now increasing the value of x by 1
so, x = 11

First increase then use value

In the following example we are using the increment operator ++ before the variable x on the right hand side of an assignment statement.

So, value of x will be first increased by 1 and then used.

#include <stdio.h>
int main(void)
{
  //declare variables
  int x, y;
  
  //assign value to x
  x = 10;
  
  printf("Before ++x: Value of x = %d\n", x);
  
  //assign value to y
  y = ++x + 10;
  
  printf("y = %d\n", y);
  
  printf("After ++x: Value of x = %d\n", x);
  
  return 0;
}

Output

Before ++x: Value of x = 10
y = 21
After ++x: Value of x = 11

In the above code the value of x is first increased and then used in the assignment statement.

y = ++x + 10;

since we have ++x so
x is now 10+1 = 11

now using the new value of x

so, y = ++x + 10
y = 11 + 10
y = 21

Similarly, we can have the two scenarios for the decrement operator.

First use then decrease

In the following example we first use the value of x then decrease it by 1.

#include <stdio.h>
int main(void)
{
  //declare variables
  int x, y;
  
  //assign value to x
  x = 10;
  
  printf("Before x--: Value of x = %d\n", x);
  
  //assign value to y
  y = x-- + 10;
  
  printf("y = %d\n", y);
  
  printf("After x--: Value of x = %d\n", x);
  
  return 0;
}

Output

Before x--: Value of x = 10
y = 20
After x--: Value of x = 9

First decrease then use

In the following example we first decrease the value of x then use it.

#include <stdio.h>
int main(void)
{
  //declare variables
  int x, y;
  
  //assign value to x
  x = 10;
  
  printf("Before --x: Value of x = %d\n", x);
  
  //assign value to y
  y = --x + 10;
  
  printf("y = %d\n", y);
  
  printf("After --x: Value of x = %d\n", x);
  
  return 0;
}

Output

Before --x: Value of x = 10
y = 19
After --x: Value of x = 9

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT