Bcanotesmod 2 C
Bcanotesmod 2 C
UNIT STRUCTURE
2.2 INTRODUCTION
2.3 OPERATORS
Integer Arithmetic
When an arithmetic operation is performed on two whole numbers or
integers then such an operation is called integer arithmetic. It al-
ways gives an integer as the result. Let, x = 5 and y = 2 be two integer
numbers. Then the integer arithmetic leads to the following results:
If we enter 5 for n1 and 2 for n2, then the output of the above program
will be :
The sum is = 7
The difference is = 3
The product is = 10
The division is = 2
The modulus is = 1
Operator Meaning
All these operators fall within the same precedence group, which is
lower than the arithmetic and unary operators. The associativity of
these operators is left to right. There are two equality operators as-
sociated with the relational operators. They are:
== equal to
!= not equal to
For checking equality the double equal sign is used, which is different
from other programming languages. The statement a == b checks
whether a is equal to b or not. If they are equal, the output will be true;
otherwise , it will be false. The statement a = b assigns the value of b
to a. For example, if b = 10, then a is also assigns the value of 10.
>, >=, <, <= have precedence over == and !=. Again, the arithmetic
operators +, -, *, / have precedence over relational and logical
operators. Therefore, in the following statement :
a-4>6
a - 4 will be evaluated first and only then the relation will be checked.
So, there is no need to enclose a - 4 within parenthesis.
A simple relational expression contains only one relational operator
and takes the following form:
where exp1 and exp2 are expressions, which may be simple con-
stants, variables or combination of them. Some examples of rela-
tional expressions and their evaluated values are listed below:
4.5 <= 12 TRUE
-5 > 0 FALSE
10 < 8 + 5 TRUE
5 == 2 FALSE
6! = 2 TRUE
The logical AND and Logical OR operators are used when we want
to test more than one condition and make decisions.
a > b && x = = 8
The expression to the left is a > b and that on the right is x == 8.
The whole expression is true only if both expressions are true i.e., if
a is greater than b and x is equal to 8.
Logical OR (||)
The logical OR is used to combine two expressions and the condi-
tion evaluates to true if any one of the two expressions is true.
For example:
a < m || a < n
The expression evaluates to true if any one of the expressions a<m
and a<n is true or if both of them are true. It evaluates to true if a is
less than either m or n and when a is less than both m and n.
a=a%b is same as a %= b
Sum is = 35.00
Average is = 7.00
EXERCISE
LET US KNOW
Unary Operators
The operator that acts upon a single operand to produce a new
value is called unary operator. Unary operators usually pre-
cede their single operands, though some unary operators are
written after their operands. Unary minus operation is distinctly
different from the arithmetic operator which denotes subtraction
(-). The subtraction operator requires two separate operands. All
unary operators are of equal precedence and have right-to-left
associativity. Following are some examples of the use of unary
minus operation:
-145 // unary minus is followed by an integer constant
-0.5 //unary minus is followed by an floting-point constant
-a //unary minus is followed by a variable ‘a’
-5 *(a + b) //unary minus is followed by an arithmetic expresion
i is incremented by 1.
• Decrement operator - -
Program5: Program for finding the larger value of two given values
using conditional operator
#include<stdio.h>
void main()
{ int i,j, large;
printf (“Enter 2 integers : ”); //ask the user to input 2 numbers
scanf(“%d %d”,&i, &j);
large = i > j ? i : j; //evaluation using conditional operator
printf(“The largest of two numbers is %d \n”, large);
}
void main( )
{ unsigned int a = 60; // a= 60 = 0011 1100
unsigned int b = 13; // b= 13 = 0000 1101
unsigned int c = 0;
c = a & b; //c= 12 = 0000 1100
printf(“%d”,c);
}
The output will be 12
void main( )
{ unsigned int a = 60; // 60 = 0011 1100
unsigned int b = 13; // 13 = 0000 1101
unsigned int c = 0;
c = a | b; // 61 = 0011 1101
}
Bitwise exclusive OR (^) :
The bitwise exclusive OR(XOR) operator performs logical operations
on a bit-by-bit level using the following truth table:
The bitwise exclusive OR(^) operator sets a bit in the resulting value’s
bit position if either operand (but not both) has a bit set (i.e.,1)at the
position. Bitwise exclusive OR(^) operation can be understood with
the following example:
void main( )
{ unsigned int a = 60; // 60 = 0011 1100
unsigned int b = 13; // 13 = 0000 1101
unsigned int c = 0;
c = a ^ b; // 49 = 0011 0001
}
The bitwise complement operator (~) reverses each bit in the oper-
and.
unsigned int Z = 5;
and Z in binary is 00000000 00000101 when 16 bits are used to
store integer values.
a%b=10
a*=b=231
1
0
2 bytes
The increment operator ++ works when used in an expression. In the
statement c = ++a – b; new value a = 16 is used thus giving value 6
to C. That is a is incremented by 1 before using in expression
However in the statement d = b++ + a; the old value b = 10 is used in
the expression. Here b is incremented after it is used in the expres
sion.
EXERCISE
2.5 EXPRESSIONS
(type) expression
For example,
(float)25 // Gives the float 25.0
(int)5.8 // Gives the int 5
(string)5.8 // Gives the string "5.8"
(float)"4.3" // Gives the float 4.3
Let us consider the case where we want to divide two integers a/b,
where the result must be an integer. However, we may want to force
the output to be a float type in order to keep the fraction part of the
Arithmetic Operator
Logical Operator
Operator Description Operator Description
* multiplication ! NOT
/ division && AND
% modulo division || OR
+ addition
- subtraction
Assignment Operator
The Assignment Operator(=) evaluates an expression on the right of
the expression and substitutes it to the value or variable on the left of
the expression. For example: sum = n1+n2 ;
value of n1 and n2 are added and the result is assigned to the vari-
able sum.
Increment and Decrement
Comma Operator
We can use the comma operator(,) available in c language, to build a
compound expression by putting several expressions inside a set of
parentheses. The expressions are evaluated from left to right and the
final value is evaluated last.
sizeof Operator
The sizeof operator returns the physical size in bytes of the data
item for which it is applied. It can be used with any type of data item
except bit fields. The general form is: s = sizeof (item );
3. (a) 55 (b)150
99 57
(c) ASCII value of 'A' is 65
Size of s1 and s2 in bytes: 1 4
area= b * h / 2;
printf(“\nArea of the triangle is %f”, area);
}
7. Value of n is: 20
Size of n is: 2
and a cylinder. Assume, the dimensions are integers. Use type casting
whereever necessary.
11. If a,b,c,d and e are declared using the statement
int a, b, c, d;
What value is assigned to the variablea in the following statement
a = b > c ? c > d ? 12 : d > e ? 13 : 14 : 15 in each of the following
cases:
(i) b = 5; c = 15; d = e = 8;
(ii) b = 15; c = 10; d = e = 8;
(iii) b = 15; c = 10; d = e = 20;
(iv) b = c = 9; d = 20; e = 19;
*****