Unari operator:C Programming Language - Part 2.4
Unari operator
Unary Operators: In C programming, all operators who work on a variable give new value to their Unary Operators. The most used Unary Operators are - (minus sign). To show that a number is negative or negative, we want to put + or - sign before it. Although no mark-up means that the number is positive.
1
2
3
4
5
6
7
#include <stdio.h>
Int main ()
{
Int x = -3;
Printf ("% d", x);
Return 0;
}
Where x is the value of -3;
The main two Unary operators are the Increment operator (++) and Decrement operator (- -).
Increment operator: Increment operator is displayed with a ++ symbol. That is, ++ is called increment operator. It sits on a variable and increases its value to 1. It can sit before the variable and sit next. For example, think x is a variable whose value is 5. ++ will be value of x 6 So X ++ value and 6
1
2
3
4
5
6
7
8
#include <stdio.h>
Int main ()
{
Int x = 6;
X ++;
Printf ("% d", x);
Return 0;
}
On top of our x 6, we used the increment operator (++). And finally we print it. We got the output 7.
Now if we print x before ++, then we get the same value.
1
2
3
4
5
6
7
8
#include <stdio.h>
Int main ()
{
Int x = 6;
++ x;
Printf ("% d", x);
Return 0;
}
But there is a little difference between x ++ and ++ x.
X ++ means that the value of x will be executed first and then its value increases 1. And ++ x means before that its value will increase and then be executed. If you still have difficulty understanding it, there is no problem. See the following program
1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
Int main ()
{
Int x = 3;
Int y = 6;
Printf ("% d \ n", x ++);
Printf ("% d \ n", ++ y);
Return 0;
}
If we run the above program, then we get the output 3 and 7. The increment operator was after the variable, our printf function printed the value of x before it, then its value increased one. So we're getting output 3.
But in the case of y earlier one has increased the value of y, and then printed its value. So we got output 7
Here I have taken two variables, and then I printed them. One more time ago ++ was used.
Decrement operator: - - [Minus Minus] is called Decrement operator. It sits on a variable and reduces its value to 1. It can sit before the variable and sit next. For example, think x is a variable whose value is 5. The value of -x will be 4. Similarly, the X-value will be 4.
1
2
3
4
5
6
7
8
9
#include <stdio.h>
Int main ()
{
Int x = 6;
X--;
Printf ("% d", x);
Return 0;
}
In the above program we set the value of x 6 and then applied it to the Decrement operator. After that its quality has decreased one. We printed the value of x later and got its value 5.
No comments