Pages

C OPERATOR UNIT 3 PART 1


ASSIGNMENT OPERATOR PART 1
Introduction
The assignment operator is used for assigning the value of an expression to a variable. The general format for an assignment operator is var = expression.
You can use other formats such as var += expression, which means var = var + expression.
Program
#include<stdio.h>

main( )
{
  int a,b,c,d;
  printf("ENTER VALUES OF a,b, c, d");
  scanf("%d%d%d",&a,&b,&c);
   a += b*c+d;
  printf("\n a = %d",a);
  }

Input
a = 5, b= 5, c = 7, d = 8.
Output
ENTER VALUES OF a,b, c, d
5
5
7
8
a =  48
Explanation
The assignment operators have the lowest priority and they are evaluated from right to left. The assignment operators are as follows:
=, +=, -=, *=, /=, %=.
Suppose the expression is
a = 5;
a += 5*7+8;
You will get the value 48. It is evaluated by the following steps:
  1. 5*7 = 35.
  2. 35+8 = 43.
  3. a += 43 means a = a + 43 which gives the value 48.
You can assign a value to multiple variables in one statement as:
i = j = k = 10 which gives value 10 to i, j, k.
ARITHMETIC OPERATOR
Introduction
You can process data using arithmetic operators such as +, -, *, \ and the modulus operator %. % indicates the remainder after integer division; % cannot be used for float data type or double data type. If both operands i1 and i2 are integers, the expression i1/i2 provides integer division, even if the target is a floating point variable. The operators have normal precedence rules, as follows:
  1. Unary operators such as −, + are evaluated.
  2. The multiplication (*) and division (/,%) operators are evaluated.
  3. The addition (+) and subtraction (−) operators are evaluated.
  4. The assignment operator is evaluated.
  5. The expressions are evaluated from left to right for unary operators. The assignment is from right to left.
Program

#include<stdio.h>

main( )
{
  int a,b,c,d;
  int sum,sub,mul,rem;
  float div;
   printf("ENTER VALUES OF b, c, d");
    scanf("%d%d%d",&b&c,&d);
  sum = b+c;
  sub = b-c;
  mul = b*c;
  div = b/c;
  rem = b%d;
  a = b/c * d;
  printf("\n sum = %d, sub = %d, mul = %d, div = %f",sum,sub,mul,div);
  printf("\n remainder of division of b & d is %d",rem);
  printf("\n a = %d",a);
  }
Input
b = 10, c = 5, d= 3.
Output
ENTER VALUES OF b, c, d
10
5
3

sum = 15, sub = 5, mul = 50, div = 2.0
remainder of division of b & d is 1
a = 6
Explanation
  1. Suppose you have the expression
  2. a = b/c * d
Here / and * both have the same priority. b/c first is evaluated because the expression is evaluated from left to right.
  1. After evaluating the expression b/c * d, the value is assigned to a because the assignment operator has an order of evaluation from right to left, that is, the right expression is evaluated first.

RELATIONAL OPERATOR
Introduction
Relational operators are used in Boolean conditions or expressions, that is, the expressions that return either true or false. The relational operator returns zero values or nonzero values. The zero value is taken as false while the nonzero value is taken as true.
Program
Th relational operators are as follows:
<, <=, >, >=, ==, !=
The priority of the first four operators is higher than that of the later two operators. These operators are used in relational expressions such as:
7 > 12       // false
20.1 < 20.2     // true
'b' < 'c'   // true
"abb" < "abc"  // true
The strings are compared according to dictionary comparison, so if the first characters are equal, the condition is checked for the second characters. If they are also equal then it is checked for the third character, etc. The relational operators return integer values of either zero or non zero.
Note that the equality operator is == and not =. ‘=’ is an assignment operator.
If you want to compare a and b for equality then you should write a == b, not a = b because a = b means you are assigning the value of b to a, as shown in Table 3.1.
Table 3.1: Comparing the equality operator (= =) with the ‘=’ assignment operator.
Case
a
b
a = b
a == b
1
5
3
a = 3 (true)
false
2
7
0
a = 0 (false)
false
3
0
0
a = 0 (false)
true
In case 1, the value of a = 5 and b = 3. The assignment expression assigns the value of b to a, so a will be 3. The expression returns a true value because 3 is not zero. For the same case a == b does not make any assignment and returns a false value because in the value of a does not equal that of b.
In case 2, the value of a = 7 and b = 0. The assignment expression assigns the value of b to a, so a will be 0. The expression returns a false value of zero. For the same case, a == b does not make any assignment and returns a false value because the value of a does not equal that of b.
In case 3, the values of a and b are both 0. The assignment expression assigns the value of b to a, so a will be 0. The expression returns a false value of zero. For the same case, a == b does not make any assignment and returns a true value because the value of a equals that of b.