Content Strategy Manager
In the C programming language, increment (++) and decrement (--) operators can be used to increase and decrease the numerical values by 1.
- The increment and Decrement operators are some of the most used programming operations for looping, array traversal, pointer arithmetic, and several other applications.
- These operators can also be used to conduct mathematical and logical operations.
- The arithmetic, bitwise, logical, decrement and increments are some examples of operators in the C programming language.
Table of Content |
Key Terms: Increment, Decrement, Prefix, Postfix, Operators, Programming language, Syntax, variable
Increment Operator
[Click Here for Sample Questions]
By using the increment operator variable’s value can be increased by one in an expression. It can be applied to numeric variables that involve character, float, integer, and pointer variables. The increment operator is denoted by ++ (double plus sign).
Read More:
Related GATE Articles | ||
---|---|---|
Variables in C | Storage Classes in C | TCL Full Form |
POP Full Form | Types of Keys in DBMS | Semaphores in Operating System |
Transaction in DBMS | Tokens in C | Void Pointer in C |
Types of Increment Operator
[Click Here for Sample Questions]
The increment operator can be categorized as:
Prefix Increment Operator
The present value of the accessible variable can be increased frequently and is used as the expression in the prefix increment operator. In other words, we can understand that initially the value is raised and then the expression is used internally.
b = ++a
In this instance, the current value of a will be increased by 1 initially. Next, for the expression where it appears, b will be given the updated value.
The syntax that can be used for prefix increment operator are:
#include <studio.in>
int main () { int a1 = 7 : // initially the present value of the a1 is raised by 1 then the value of a1 can be represented. Printf (“%d\n”, ++a1); return 0; }
This syntax will result in 8.
Postfix Increment Operator
The postfix increment operator increases a variable's value by one after allowing it to be used in an expression with its current value. To put it another way, a variable's value is incremented by 1 after being used inside the given phrase.
b = a++;
In this situation, b is first given the present value of a and then increased.
The syntax for the postfix increment operator is:#include <studio.in>
int main () { int a2 = 7 : // initially the present value of the a2 is increased by 1 then the value of a2 can be represented. Printf (“%d\n”, a2++); return 0; }
This syntax will result in 7.
Decrement Operator
[Click Here for Sample Questions]
The variable's value can be declined in the expression of the decrement operator. It can be denoted by – (double minus sign).
Types of Decrement Operator
[Click Here for Sample Questions]
There are two types of decrement operators:
Prefix Decrement Operator
The present value of the accessible variable can be reduced frequently and is used as the expression in the prefix decrement operator. In other words, we can understand that initially the value is decreased and then the expression is used internally.
b = -a;
The present value of an in this instance will be decreased by 1. Next, for the expression that uses it, b will be given the updated value.
The Syntax that can be used for the prefix decrement operator are:
#include <studio.in>
int main () { int a1 = 7 : // initially the present value of the a1 is reduced by 1 then the value of a1 can be represented. Printf (“%d\n”, --a1); return 0; }
This syntax will result in 6.
Postfix Decrement Operator
The postfix decrement operator reduces 1 from a variable's value after allowing it to be used in an expression with its current value. Put more simply, a variable's value is decremented by 1 after being used in the expression that is supplied. To illustrate:
b = a—
In this scenario, initially, the b is allocated the present value of a, and then a is reduced.
The syntax that can be used for the postfix decrement operator are:
#include <studio.in>
int main () { int a2 = 7 : // initially the present value of the a2 is reduced by 1 then the value of a2 can be represented. Printf (“%d\n”, a2--); return 0; }
This syntax will result in 7.
Precedence in Increment and Decrement Operators in C
[Click Here for Sample Questions]
Working with numerous operators in an individual statement requires a knowledge of the precedence and associativity of operators. The binary operators for increment (++) and decrement (--) in C hold significant precedence.
Precedence
Increment and decrement operators: With the possible exception of brackets (()), these operators have a greater precedence than the majority of other operators in C.
- This indicates that the increment and decrement operators are carried out first when evaluating an expression that includes them together with other operators.
- Postfix vs. prefix: Operators with a postfix increment (a++) and decrement (a--) are more common than those with a prefix increment (++a) and decrement (--a).
Associativity
Associativity proceeds from left to right for both increment and decrement operators. This implies that if an expression contains more than one increment or decrement operator, the left-most operator will be used first and proceed rightward.
Difference between Increment and Decrement Operators
[Click Here for Sample Questions]
The primary differences between increment and decrement operators in the C programming language are:
Basis | Increment Operators | Decrement Operators |
---|---|---|
Purpose | The value of the variable rises by 1. | The value of the variable was reduced by 1. |
Prefix operation | Initially, the variable value rises and is then used in the expression. | Initially the variable value declines and is then used in the expression. |
Postfix operation | Before raising the value it is analyzed effectively in the expression. | Before declining the value it is analyzed effectively in the expression. |
Collaborate with the assignment | b = a++ allocates the a’s present value to b, and then raises the a. | b = a—allocates the a’s present value to b and then diminishes the a. |
Common Use Case | Commonly used in loop counters so that the loop variable increases. | Commonly used in loop counters to reduce the end situation. |
Things to Remember
- Value in an expression can be increased by one by using the increment operator variable.
- The increment operator can be categorized as prefix increment operator and postfix increment operator.
- The variable's value is lowered by one when using increment operators.
- Compared to prefix operators, postfix operators are more precedential.
- Operators with a postfix increment (a++) and decrement (a--) are more common than those with a prefix increment (++a) and decrement (--a).
- The postfix increment operator increases a variable's value by one after allowing it to be used in an expression with its current value.
Also Read:
Sample Questions
Ques. Given, int x = 5; x = x++; what is the value of x here? (1 mark)
(a) 6
(b) 9
(c) 3
(d) 2
Ans. (a) 6
Explanation: After providing the present value of x, the post-increment operator x++ increases x. So, in this instance, x becomes 6.
Ques. In the programming language C, what distinguishes printf and scanf functions? (1 mark)
(a) Printf is used to print strings, whereas scanf is used to print characters.
(b) Formatted output is handled by printf, while formatted input is handled by scanf.
(c) For input, use printf, and for output, use scanf.
(d) No difference
Ans. (b) Formatted output is handled by printf, while formatted input is handled by scanf.
Explanation: Because formatted output to the console is handled by printf, while formatted input from the console is handled by scanf.
Ques. What will be the result for the C programming language given below? (2 marks)
#include<stdio.h>
int main()
{
int a, b;
a = 5;
b = x++ / 2;
printf(“%d”, y);
return 0;
}
(a) 2
(b) 3
(c) Compile time error
(d) None of the above
Ans. (a) 2
Explanation: The formula in this case is b = a++ / 2; post-increment, b = 5/2 = 2. Thus, b = 2 and a = 6.
Ques. What will be the result for the C programming language given below? (2 marks)
#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 3
(c) 3 2 3
(d) 3 3 2
Ans. (b) 2 3 3
Explanation: Since y = –x is the initial equation provided, x becomes 3 (x = 3) and y = 3. Since z = x– now, x = 2 and z = 3. At last, z = 3, y = 3, and x = 2.
Ques. In the situation when ++a++ is used in the code, then what will be its result? (1 mark)
(a) Compile error
(b) Declares a variables
(c) Exists the loop
(d) Compile time error
Ans. (d) Compile time error
Explanation: A Compile Time Error will be shown. This is because, in this case, an lvalue is needed as an increment operand.
Ques. Consider the following and what will be its result? (2 marks)
int s = 5, t;
t = s-- + --s - s++;
printf("%d", t);
(a) 2
(b) 4
(c) 9
(d) 3
Ans. (a) 2
Explanation: The function s-- restores the initial value of s (5), s++ increases s after it has been used, and --s decreases s before using it (s becomes 3). Consequently, the formula is 5 + 3 - 4 = 2.
Ques. In C programming language the operator sizef is used for: (1 mark)
(a) Provides the variable or data type's size
(b) Compute the sum of two integers
(c) Identify the square root
(d) None of these
Ans. (a) provides the variable or data type's size
Explanation: To find a variable's or data type's size in bytes, sizef operator can be used.
Ques, Which one is the accurate data type of C programming language? (1 mark)
(a) Float
(b) Chart
(c) Double
(d) Boolean
Ans. (d) Boolean
Explanation: A built-in boolean data type is absent from the C programming language. Generally, boolean values are represented by numbers (0 and 1).
For Latest Updates on Upcoming Board Exams, Click Here: https://t.me/class_10_12_board_updates
Check-Out:
Comments