The decrement (–) and increment (++) operators are special types of operators used in programming languages to decrement and increment the value of the given variable by 1 (one), respectively.
In this article, we will dig deeper into Increment and Decrement Operators in C according to the GATE Syllabus for CSE (Computer Science Engineering). Read ahead to know more.
Table of Contents
- Types Of Increment And Increment In C
- Increment Operators
- Decrement Operators
- Precedence In Increment And Decrement Operators In C
- Practice Problems On Increment And Decrement Operators In C
- Faqs
Types of Increment and Decrement Operators in C
Following types of increment and decrement operators are found in the C language:
- Prefix Increment operator
- Prefix Decrement operator
- Postfix Increment operator
- Postfix Decrement operator
Both the operators vary a lot in their fundamental working. However, we can only use these with variables, and not with expressions or constants. Read more about the difference between Increment and Decrement Operators.
Increment Operators
We use increment operators in C to increment the given value of a variable by 1.
For instance,
int a = 1, b = 1;
++b; // valid
++3; // invalid – increment operator is operating on constant value
++(a+b); // invalid – increment operator is operating on expression
Prefix Increment Operators
The prefix increment operator increments the current value of the available variable immediately, and only then this value is used in an expression. In simpler words, the value of a variable first gets incremented and then used inside any expression in the case of an increment operation.
b = ++a;
In this case, the current value of a will be incremented first by 1. Then the new value will be assigned to b for the expression in which it is used.
Thus, Syntax for Prefix Increment Operator:
++variable;
For example,
#include<stdio.h>
#include<conio.h>
void main()
{
int p,q;
i=10;
p=++q;
printf(“p: %d”,p);
printf(“q: %d”,q);
getch();
}
The output here would be:
p: 11
q: 11
Postfix Increment Operators
The postfix increment operator allows the usage of the current value of a variable in an expression and then increments its value by 1. In simpler words, the value of a variable is first used inside the provided expression, and then its value gets incremented by 1.
For example:
b = a++;
In this case, the current value of a is first assigned to b, and after that, a is incremented.
Thus, Syntax for Postfix Increment Operator:
variable++;
For example,
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
b=10;
a=b++;
printf(“a: %d”,a);
printf(“b: %d”,b);
getch();
}
The output here would be:
a: 10
b: 11
Syntax of Increment Operators
// PREFIX
++x
// POSTFIX
x++
where x is a variable
Example of Increment Operators
#include <stdio.h>
int main() {
int x = 5, y = 5;
// x is displayed
// Then, x is increased to 6.
printf(“%d post-increment n”, x++);
// y is increased to 6
// Then, it is displayed.
printf(“%d pre-incrementn”, ++y);
return 0;
}
The output here would be:
5 post-increment
6 pre-increment
Decrement Operators
We use decrement operators in C to decrement the given value of a variable by 1.
For instance,
int a = 2, b = 1;
–b; // valid
–5; // invalid – decrement operator is operating on constant value
–(a-b); // invalid – decrement operator is operating on expression
Prefix Decrement Operators
The prefix decrement operator decrements the current value of the available variable immediately, and only then this value is used in an expression. In simpler words, the value of a variable first gets decremented and then used inside any expression in the case of a decrement operation.
b = –a;
In this case, the current value of a will be decremented first by 1. Then the new value will be assigned to b for the expression in which it is used.
Thus, Syntax for Prefix Decrement Operator:
–variable;
For example,
#include<stdio.h>
#include<conio.h>
void main()
{
int p,q;
i=10;
p=–q;
printf(“p: %d”,p);
printf(“q: %d”,q);
getch();
}
The output here would be:
p: 9
q: 9
Postfix Decrement Operators
The postfix decrement operator allows the usage of the current value of a variable in an expression and then decrements its value by 1. In simpler words, the value of a variable is first used inside the provided expression, and then its value gets decremented by 1. For example:
b = a–;
In this case, the current value of a is first assigned to b, and after that, a is decremented.
Thus, Syntax for Postfix Decrement Operator:
variable–;
For example,
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
b=10;
a=b–;
printf(“a: %d”,a);
printf(“b: %d”,b);
getch();
}
The output here would be:
a: 10
b: 9
Syntax of Increment Operators
// PREFIX
–x
// POSTFIX
x–
where x is a variable
Example of Decrement Operators
#include <stdio.h>
int main() {
int x = 5, y = 5;
// x is displayed
// Then, x is decreased to 4.
printf(“%d post-decrement n”, x–);
// y is decreased to 4
// Then, it is displayed.
printf(“%d pre-decrement”, –y);
return 0;
}
The output here would be:
5 post-decrement
4 pre-decrement
Precedence in Increment and Decrement Operators in C
Both of these operators have very high precedence (parentheses are an exception). Also, the precedence of postfix decrement/ increment operators is higher than that of prefix decrement/ increment operators.
Here is a table that discusses the precedence associativity of these operators:
Description | Operators | Associativity |
postfix decrement operator | — | left to right |
postfix increment operator | ++ | left to right |
parentheses | () | left to right |
prefix increment operator | ++ | right to left |
prefix decrement operator | — | right to left |
unary plus | + | right to left |
unary minus | – | right to left |
Multiplication | * | left to right |
Division | / | left to right |
Modulus | % | left to right |
Addition | + | left to right |
Subtraction | – | left to right |
Assignment Operator | = | right to left |
Compound Assignment Operator | =, +=, -=, *=, /=, %= | right to left |
Practice Problems on Increment and Decrement Operators in C
1. Choose the correct output for the C program mentioned below:
#include<stdio.h>
int main()
{
int a, b;
a = 5;
b = x++ / 2;
printf(“%d”, y);
return 0;
}
A. 3
B. 2
C. Compile-time error
D. None of these
Answer – B. 2
The expression given here is:- b=a++ / 2; a++ thus post-increment, b=5/2=2. Hence, a=6 and b=2.
2. Choose the correct output for the C program mentioned below:
#include<stdio.h>
int main()
{
int x=4,y,z;
y = –x;
z = x–;
printf(“%d %d %d”,x,y,z);
return 0;
}
A. 3 2 2
B. 2 3 2
C. 3 3 2
D. 2 3 3
Answer – D. 2 3 3
The first expression given here is y=–x; thus, x becomes 3 (x=3) and y=3. Now, z=x–; so, z=3 and x=2. Finally, x=2, y=3, and z=3.
FAQs
What would be the output if we use the ++a++ in a code?
We will receive a Compile time error. It is because an lvalue is required here as an increment operand.
What would be the output if we use the ++(a*b+1) in a code?
We will receive a Compile time error. It is because an lvalue is required in this case as well in the form of an increment operand. Also, the operand of decrement and increment operators must not be an expression or a constant. It should rather be a variable.
Can we use prefix and postfix operators in the same code with both – increment and decrement operators?
Yes, we can. Refer to the aforementioned examples to understand how they work when combined together
Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility Criteria, GATE 2023, GATE Admit Card, GATE Syllabus for CSE (Computer Science Engineering), GATE CSE Notes, GATE CSE Question Paper, and more.
Also Explore,
Comments