C includes a class of operators that act upon a single operand to produce a new value such operators are known as unary operators. Unary operators usually precede their single oper- ands , though some unary operators are written after their operands.
(i) UNARY MINUS
Unary minus is a unary operator where a minus sign precedes a numerical constant, a vari- able or an expression. In C, all numeric constants are positive. Thus a negative number is actually an expression consisting of the unary minus operator followed by a positive number.
e.g. - 743,
-0 X 7FFF,
-0.2, -5E -8,
-root 1,
-(x +y) -3 * (x+y)
(ii) INCREMENT OPERATOR AND DECREMENT OPERATOR
The increment operator (++) causes its operand to be increased by one where as the decre- ment operator causes its operand to be decreased by one. The operand used with each of these operator must be a single variable.
e.g. Suppose i is an integer variable that has been assigned a value of 5. the expression ++i, causes the value of i to be increased by one, Whereas the decrement operator causes the value to be decreased by 1 so, now the new variable of i will be 4
i ++ means i = i + 1
-- i means i = i-1
The increment and decrement operators can each be utilized in two different ways, depend-
ing an whether the operator is written before or after the operand. If the operator precedes the operand (e.g. ++ i) then the operand will be altered in value before it is utilized for its intended purpose within the program. If however, the operator follows the operand (e.g i ++) then the value of the operand will be altered after it is utilized
e.g. printf (“ i = %d\n”, i);
printf (“ i = % d\n”, ++i);
printf (“ i = %d\n”, i);
There printf statements will generate following three lines of out put:-
i = 1
i = 2
i =2
Now let us take the second case:-
printf (“ i = %d\n”, i);
printf (“ i = %d\n”, i++);
printf ( i = %d\n”, i )
The statement will generate the following three lines of output i = 1
i = 1
i = 2
(i) UNARY MINUS
Unary minus is a unary operator where a minus sign precedes a numerical constant, a vari- able or an expression. In C, all numeric constants are positive. Thus a negative number is actually an expression consisting of the unary minus operator followed by a positive number.
e.g. - 743,
-0 X 7FFF,
-0.2, -5E -8,
-root 1,
-(x +y) -3 * (x+y)
(ii) INCREMENT OPERATOR AND DECREMENT OPERATOR
The increment operator (++) causes its operand to be increased by one where as the decre- ment operator causes its operand to be decreased by one. The operand used with each of these operator must be a single variable.
e.g. Suppose i is an integer variable that has been assigned a value of 5. the expression ++i, causes the value of i to be increased by one, Whereas the decrement operator causes the value to be decreased by 1 so, now the new variable of i will be 4
i ++ means i = i + 1
-- i means i = i-1
The increment and decrement operators can each be utilized in two different ways, depend-
ing an whether the operator is written before or after the operand. If the operator precedes the operand (e.g. ++ i) then the operand will be altered in value before it is utilized for its intended purpose within the program. If however, the operator follows the operand (e.g i ++) then the value of the operand will be altered after it is utilized
e.g. printf (“ i = %d\n”, i);
printf (“ i = % d\n”, ++i);
printf (“ i = %d\n”, i);
There printf statements will generate following three lines of out put:-
i = 1
i = 2
i =2
Now let us take the second case:-
printf (“ i = %d\n”, i);
printf (“ i = %d\n”, i++);
printf ( i = %d\n”, i )
The statement will generate the following three lines of output i = 1
i = 1
i = 2