Arithmetic Operators
Arithmetic Operators
All the basic arithmetic operations can be carried out in C. All the operators have almost the same meaning as in other
languages. Both unary and binary operations are available in C language. Unary operations operate on a singe operand,
therefore the number 5 when operated by unary – will have the value –5.
Arithmetic Operators
Operator Meaning
* Multiplication
/ Division
% Modulus Operator
x + y
x - y
-x + y
a * b + c
-a * b
etc.,
here a, b, c, x, y are known as operands. The modulus operator is a special operator in C language which evaluates
the remainder of the operands after division.
Example
.
#include //include header file stdio.h
void main() //tell the compiler the start of the
program
{
int numb1, num2, sum, sub, mul, div, mod;
//declaration of variables
scanf (“%d %d”, &num1, &num2); //inputs the
operands
Integer Arithmetic
When an arithmetic operation is performed on two whole numbers or integers than such an operation is called as integer
arithmetic. It always gives an integer as the result. Let x = 27 and y = 5 be 2 integer numbers. Then the
integer operation leads to the following results.
x + y = 32
x – y = 22
x * y = 115
x % y = 2
x / y = 5
x + y = 18.0
x – y = 10.0
x * y = 56.0
x / y = 3.50
2. Relational Operators
Often it is required to compare the relationship between operands and bring out a decision and program accordingly. This
is when the relational operator come into picture. C supports the following relational operators.
Operato Meaning
r
== is equal to
!= is not equal to
It is required to compare the marks of 2 students, salary of 2 persons, we can compare them using relational operators.
A simple relational expression contains only one relational operator and takes the following form.
Relational expressions are used in decision making statements of C language such as if, while and forstatements to
decide the course of action of a running program.
3. Logical Operators
C has the following logical operators, they compare or evaluate logical and relational expressions.
Operato Meaning
r
|| Logical OR
! Logical NOT
Example
The expression to the left is a > b and that on the right is x == 10 the whole expression is true only if both expressions
are true i.e., if a is greater than b and x is equal to 10.
Logical OR (||)
The logical OR is used to combine 2 expressions or the condition evaluates to true if any one of the 2 expressions is true.
Example
a < m || a < n
The expression evaluates to true if any one of them 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.
For example
! (x >= y) the NOT expression evaluates to true only if the value of x is neither greater than or equal to y
4. Assignment Operators
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.
Example
x = a + b
Here the value of a + b is evaluated and substituted to the variable x.
Here var is a variable, exp is an expression and oper is a C binary arithmetic operator. The operatoroper = is known as
shorthand assignment operator
Example
x + = 1 is same as x = x + 1
a=a+1 a += 1
a=a–1 a -= 1
a = a * (n+1) a *= (n+1)
a = a / (n+1) a /= (n+1)
a=a%b a %= b
.
#define N 100 //creates a variable N with constant
value 100
#define A 2 //creates a variable A with constant
value 2
++variable name and variable name++ mean the same thing when they form statements independently, they behave
differently when they are used in expression on the right hand side of an assignment statement.
m = 5;
y = ++m; (prefix)
m = 5;
y = m++; (post fix)
Then the value of y will be 5 and that of m will be 6. A prefix operator first adds 1 to the operand and then the result is
assigned to the variable on the left. On the other hand, a postfix operator first assigns the value to the variable on the left
and then increments the operand.
exp1 is evaluated first. If the expression is true then exp2 is evaluated & its value becomes the value of the expression.
If exp1 is false, exp3 is evaluated and its value becomes the value of the expression. Note that only one of the expression
is evaluated.
For example
a = 10;
b = 15;
x = (a > b) ? a : b
Here x will be assigned to the value of b. The condition follows that the expression is false therefore b is assigned to x.
.
/* Example : to find the maximum value using
conditional operator)
#include
void main() //start of the program
{
int i,j,larger; //declaration of variables
printf (“Input 2 integers : ”); //ask the user to
input 2 numbers
scanf(“%d %d”,&i, &j); //take the number from
standard input and store it
larger = i > j ? i : j; //evaluation using
ternary operator
printf(“The largest of two numbers is %d \n”,
larger); // print the largest number
} // end of the program
.
Output
7. Bitwise Operators
C has a distinction of supporting special operators known as bitwise operators for manipulation data at bit level. A
bitwise operator operates on each bit of data. Those operators are used for testing, complementing or shifting bits to the
right on left. Bitwise operators may not be applied to a float or double.
Operato Meaning
r
| Bitwise OR
^ Bitwise Exclusive
8. Special Operators
C supports some special operators of interest such as comma operator, size of operator, pointer operators (& and *) and
member selection operators (. and ->). The size of and the comma operators are discussed here. The remaining
operators are discussed in forth coming chapters.
First assigns 10 to x and 5 to y and finally assigns 15 to value. Since comma has the lowest precedence in operators the
parenthesis is necessary. Some examples of comma operator are
In for loops:
In while loops
t = x, x = y, y = t;
Example
m = sizeof (sum);
n = sizeof (long int);
k = sizeof (235L);
The size of operator is normally used to determine the lengths of arrays and structures when their sizes are not known to
the programmer. It is also used to allocate memory space dynamically to variables during the execution of the program.
Example program that employs different kinds of operators. The results of their evaluation are also shown in comparision
.
main() //start of program
{
int a, b, c, d; //declaration of variables
a = 15; b = 10; c = ++a-b; //assign values to
variables
printf (“a = %d, b = %d, c = %d\n”, a,b,c); //print
the values
d=b++ + a;
printf (“a = %d, b = %d, d = %d\n, a,b,d);
printf (“a / b = %d\n, a / b);
printf (“a %% b = %d\n, a % b);
printf (“a *= b = %d\n, a *= b);
printf (“%d\n, (c > d) ? 1 : 0 );
printf (“%d\n, (c < d) ? 1 : 0 );
}
.
Notice the way 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 expression.
We can print the character % by placing it immediately after another % character in the control string. This is illustrated
by thestatement.