Pages

Showing posts with label ASSIGNMENT OPERATOR. Show all posts
Showing posts with label ASSIGNMENT OPERATOR. Show all posts

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.


C OPERATOR UNIT 3 PART 2


COMMA OPERATOR
Introduction
You can combine multiple expressions in a single expression using the comma operator.
Program
#include<stdio.h>

main()
{
  int i,j,k;
  k = (i = 4, j = 5);
  printf("k = %d",k);
 }
Input

i = 4,j = 5.
Output
k = 5.
Explanation
For example, you can write: k = (i = 4, j = 5)
Here the expression is evaluated from left to right, that is, i = 4 is evaluated first then j = 5 is evaluated. The value of the rightmost expression is specified as output, thus k will get the value 5.

BITWISE OPERATOR
Introduction
Bitwise operators interpret operands as strings of bits. Bit operations are performed on this data to get the bit strings. These bit strings are then interpreted according to data type. There are six bit operators: bitwise AND(&), bitwise OR(|), bitwise XOR(^), bitwise complement(~), left shift(<<), and right shift(>>).
Program
# include<stdio.h>

main()
{
  char c1,c2,c3;
  printf("ENTER VAULES OF c1 and c2");
  scanf("%c,%c",&c1,&c2);
  c3 = c1 & c2;
  printf("\n Bitwise AND i.e. c1 & c2 = %c",c3);
  c3 = c1 | c2;
  printf("\n Bitwise OR i.e. c1 | c2 = %c",c3);
  c3 = c1 ^ c2;
  printf("\n Bitwise XOR i.e. c1 ^ c2 = %c",c3);
  c3 = ~c1;
  printf("\n ones complement of c1 = %c",c3);
  c3 = c1<<2;
  printf("\n left shift by 2 bits c1 << 2 = %c",c3);
  c3 = c1>>2;
  printf("\n right shift by 2 bits c1 >> 2 = %c",c3);
}
Input
c1 = 4;
c2 = 6;
Output
ENTER VALUES OF c1 and c2
4
6

Bitwise AND i.e. c1 & c2 = 4
Bitwise OR i.e. c1 | c2 = 6
Bitwise XOR i.e. c1 ^ c2 = 2
ones compliment of c1 = -4
left shift by 2 bits c1 << 2 = 16
right shift by 2 bits c1 >> 2 = 1
Explanation
  1. Suppose you write
  2. char c1, c2, c3;
  3. c1 = 4;
  4. c2 = 6;
The binary values are
c1 = 0000 0100
c2 = 0000 0110
  1. Suppose you write
  2. c3 = c1 & c2;
The value of c3 is interpreted as follows:
  0000 0100
& 0000 0110
 ----------.
  0000 0100
Each bit of c1 is compared with the corresponding bit of c2. If both bits are 1 then the corresponding bit is set as 1, otherwise it is set as 0. Thus the value of c3 is 4.
  1. c3 = c1 | c2
The value of c3 is interpreted as follows:
  0000 0100
| 0000 0110
 -----------.
  0000 0110
Each bit of c1 is compared with the corresponding bit of c2. If any of the bits are 1 then the corresponding bit is set as 1; otherwise it is set as 0. Thus the value of c3 is 6.
  1. c3 = c1 ^ c2
The value of c3 is interpreted as follows:
  0000 0100
^ 0000 0110
 ----------.
  0000 0010
Each bit of c1 is compared with the corresponding bit of c2. If only one bit is 1, the corresponding bit is set to 1; otherwise it is set to 0. Thus you will get the value of c3 as 2 because in the second position for c1, the bit is 0 and for c2, the bit is 1. So only one bit is set.
  1. c3 = ~ c1
The value of c3 is interpreted as follows:
~  0000 0100
   ----------.
   1111 1011
Each bit of c1 is complemented; for 1 the complement is 0. Thus you will get the value of c3 as −4, because the leftmost bit is set as 1.
  1. c3 = c1 << 2;
This is a left-shift operation. The bits are shifted left by two places. c1 indicates the operand that should be an expression returning a whole number. 2 indicates a shift that should not be negative, and its value must be less than the number of bits allocated to that data type.
It is evaluated as follows:
c1 is 0000 0100
It is shifted 2 bits to the left to produce
0001 00**
While shifting, the high-order (left) bits are discarded. Since a vacuum is created on the right side, it is filled with 0s to get 0001 0000. Thus the value is 16.
  1. c3 = c1 >> 2;
This is a right shift operation. The bits are shifted right by two places. c1 indicates the operand that should be an expression returning a whole number. 2 indicates a shift that should not be negative, and its value must be less than the number of bits allocated to that data type.
It is evaluated as follows:
c1 is 0000 0100
It is shifted 2 bits to the right to produce
**00 0001
While shifting, the low-order (right) bits are discarded. The asterisks are replaced using one of the following strategies:
Logical shift: In this case, the high-order bits are filled with 0s, thus you get 0000 0001.
Arithmetic shift: In this case the high-order bits are filled with the original sign bits, so if the sign bit is 1, then all bits are filled with 1s; otherwise, they are filled with 0s.
For unsigned data types, logical shift is used, whereas for signed data types arithmetic shift is used.
In these examples, the char data type which is signed is used. In number 4, the sign bit is 0, so you will get the bit pattern 0000 0001 (decimal 1).
OPERATOR PRECEDENCE
Introduction
Since C has various types of operators, it also sets precedence rules so that the value of expressions that involve multiple operators should be deterministic.
Program
The precedence of operators is given in Table 3.3.

Table 3.3: Operator precedence rules
Operators
Order of evaluation
Remarks
[] ( ) ->
Left to right
Array subscript, function call
− + sizeof( ) ! ++ −−


& * ~ (cast)
Right to left
Unary
* / %
Left to right
Binary Multiplicative
+ -
Left to right
Binary Additive
>> <<
Left to right
Shift operators
< <= > >=
Left to right
Relational operators
== !=
Left to right
Equality operators
&
Left to right
Bitwise And operator
^
Left to right
Bitwise Xor operator
|
Left to right
Bitwise Or operator
&&
Left to right
Logical And operator
||
Left to right
Logical Or operator
?:
Left to right
Conditional operator
= += -= *= /= %=


&= -= |= <<= >>=
Right to left
Assignment
,
Right to left
Comma
Point to Remember
The operators are evaluated according to the precedence as shown in Table 3.3