increment and decrement Operator in c
Increment and Decrement Operators
The increment operator will increment the operand by one value similarly decrements operator decrement the operand by 1 value. This operator will modify the operand so we need to pass only variables constants are not allowed
--
++
These two-operator are unary operator
These operators are divided into two category
- Prefix Increment/decrement-operator is written before the operand.
(++num, --num)
- Postfix Increment/decrement - operator is written after the operand
(num++,num--)
Pre increment/ decrement operator:
in pre-increment first increment then assign it.
Example
#include<stdio.h>
int main()
{
int i=10;
printf("i=%d\n",i);
++i;
//--i;
//++10; error
printf("i=%d\n",i);
}
Post increment and decrement:
Post increment rule is first assigned and then increment or decrement of the operand.
Example:
#include<stdio.h>
void main()
{
//int i=10;
volatile int i=10;
printf("i=%d\n",i);
printf("%d %d %d\n",i++,i++,i++,i++);
}
Output:
using volatile: 14,13,12,11
not using voltaile: 14,14,14,14
in case of pre increment no increment case the latest value will assign gets update
Top Resources
Arithmetic operator in c
Assignment operator in c programming
Relational operator in c programming
Logical Operator in c programming
Bitwise operator in c programming
sizeof operator in c programming
Understanding the Comma Operator in C Programming
Dot Operator in c programming
Using && Logical AND Operator in C Programming
Further Reading:
Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!