C_Tutorial_Day_4
C_Tutorial_Day_4
Arithmetic Operators
The arithmetic operators provide arithmetical operation and can be classified into unary and binary arithmetic
operators. It can operate on any built-in data type.
+ Addition or unary plus - Subtraction or unary minus
* multiplication / division % modulo division
C has no operators for exponentiation. We have to use pow(x,y) function which is available in math.h header
file. It returns xy .
Arithmetic Expression
Arithmetical expression is of three types:
Integer mode arithmetic: variables and constants used in operation are of integer type and will yield an
integer value.
Real mode arithmetic: variables and constants used in operation are of real number and will yield real
value.
Mixed mode arithmetic: variables and constants used in operation are of real type and integer type and
result will yield real type.
Assignment Statement
The result of an arithmetic expression assigned to a variable name using assignment operator ( = ). The va-
riable name on which result of an expression is to store is kept in the left side of assignment operator and
expression is on the right side of assignment operator. Statement is terminated by semicolon.
var_name=expression;
Relational Operators
If we have to take some decision, then we make a compare. This comparison is done by the help of relation-
al operators. It evaluates to 0 if compare results false and 1 if it is true.
Logical Operators
A logical operator is used to compare or evaluate logical and relational expression. There are three logical
operators in C language. They are
&& Logical AND
|| Logical OR
! Logical NOT
for example:
int year,n;
year=1980;
n=(year%4==0 && year%100!=0)||(year%400==0);
What will be the value of n?
Here year%4==0 evaluates to true and year%100!=0 evaluates to true therefore True && True will yield True
result and year%400==0 yield false value and now whole result will be True || false will yield the value True
i.e. 1.
int k,m;
k=0;
m=!k;
now the value of m will be 1 .
Bitwise operator
A bitwise operator operates on each bit of data. These operators are used for testing, complimenting or shift-
ing bits to the right or left. Usually bitwise operators are not useful in case of float and double variable. Oper-
ators are
& bitwise AND | bitwise OR ^ bitwise XOR
<< left shift >> right shift ~ bitwise complement
Example: a 0000 0000 0000 1101
b 0000 0000 0000 0111
int a=13,b=7,c;
c=a & b; a&b 0000 0000 0000 0101
Comma Operator
A set of expression separated by commas is a valid construct in C language. For example
int m,k;
m=(k=3,k+2);
The right hand side consists of two expression separated by comma. The first expression is k=3 and the
second is k+2. The expressions are evaluated from left to right . The first 3 is assigned to k and then k is in-
cremented by 2 so value of k will be now 5 hence the value of m will be evaluated as 5.
Other operators
Dot (.) operator and arrow( -> ) operator used for member selection and used with structure and union. * and
& is pointer and reference operator.
Execise
1. When the const qualifier useful?
2. What do you understand by operator precedence? Explain the associativity of unary operators.
3. Explain the behavior of right shift operator for signed and unsigned number.
4. What is enumeration constant?
5. What is meant by explicit typecasting?
6. Explain the 4 basic data types in C.