44 Programming and Data Structures
The below given program illustrates the use of conditional operator.
3.2 Write a program to use the conditional operator with two values.
# include <stdio.h>
# include <conio.h>
main O
{
clrscr();
printf ("Result = %d,2==3 ? 4 : 5 );
I
QUTEUE
Result = 5
Explanation In the above program the condition 2= =3 is false. Hence, 5 is printed.
33 Write a program to use the conditional operator with two statements.
# include <stdio.h>
# include <conio.h>
void m a i n O
{
clrscr();
3>2 ? printf ("Drue ") : printf ("False.");
QLErP.U I;
True.
Explanation In the above program a full statement is used using the conditional operator. The
condition 3>2 is true. Hence, first printf () statement is executed.
3.4 ARITHMETIC OPERATORS
There are two types of arithmetic operators. They are 1) Binary Operator, and 2) Unary Operator
a) Binary operator Table 3.2 shows different arithmetic operators that are used in 'C'. These operators
are commonly used in most of the computer languages. These arithmetic operators are used for
numerical calculations between the two constant values. They are also called as Binary Arithmetic
Operators. The examples are also shown in the Table 3.2 In the program variables are declared
instead of constants.
Operators and Expressions 45
Table 3.2 Arithmetic operators
Arithmetic Operators
Operator Explanation
Examples
+ Addition 2+2=4
-
Subtraction 5-3=2
*
Multiplication
2*5=10
/
Division
10/2=5
%
Modular Division 11%3=2 (Remainder 2)
b) Unary Operators Unary operators are increment operator (++), decrement (- -) and minus
(-). These operators and their descriptions are given in the Table 3.3.
Table 3.3 Unary arithmetic operators
Operator Description or Action
-
Minus
++ Increment
Decrement
&
Address Operator
Size of
Gives the size of operator
a ) M inus ( - ) Unary minus is used to indicate or change the algebraic sign of a value.
For Example
int x= -50;
int y= -x;
Assigns the value of -50 to x and the value of -50 to y through x. The - sign used in this way is
called the unary operator because it takes just one operand. There is no unary plus (+) in C. Even
though, a value assigned with plus sign is valid. For example int x=+50; here + is valid but in practice
this sign should not be attached in C.
b) Increment (++) & Decrement (- -) Operators The C compilers produce very fast,
efficient object codes for increment and decrement operations. This code is better than generated by
using the equivalent assignment statement. So, increment and decrement operators should be used
whenever possible.
The operator++ adds one to its operand. Whereas the operator - - subtracts one from its operand.
For justification x=x+l can be written as x++; and x = x -l; can be written as x - - ;. Both these
operators may either follow or precede the operand. That is, x=x+1; can be represented as x++; or
++x;
If ++ or are used as a suffix to the variables name then the post increased/decreased operations
take place. Consider an example for understanding ++ operator as a suffix to the variable.
46 Programming and Data Structures
x=20;
y=10;
z=x*y++;
In the above equation the current value of y is used for the product. The result is 200, which is
assigned to 'z'. After multiplication, the value of y is increased by one.
If "++" or " are used as a prefix to the variable name then pre increment/ decrement operations
take place. Consider an example for understanding "++" operator as a prefix to the variable.
x=20;
y=10;
z=x*++y;
In the above equation the value of y is increased and then used for multiplication. The result is
220, which is assigned to 1 z '.
The following programs can be executed for verification of increment and decrement operations.
3.4 Write a program to show the effect of increment operator as a suffix.
m a i n O
{
int a,z,x»10,y«20;
clrscr () ;
z*x*y++;
a«x*y;
printf ("n%d %d",z,a);
}
QUTEUIj 2QQ 21Q
Explanation In the above program the equation z=x*y++ gives the result 200 because 'y ' does
not gets increased. After multiplication %y ' increases to 11. The second equation gives result 210.
This can be verified by executing the above program.
3.5 Write a program to show the effect of increment operator as a prefix.
mainO
{
int a,z,x*10,y*20?
clrscr();
z=x*++y;
a=x*y;
printf ("n%d %d",z,a);
}
OUTPUT: 210 210
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.12.50