Lecture-4.1-C Operators and Expression
Lecture-4.1-C Operators and Expression
Expression
Operators and
Expressions
Consider the expression A + B * 5 , where,
+, * are operators,
A, B are variables,
5 is constant,
A, B and 5 are called operand, and
A+B*5 is an expression.
Types of C operators
Arithmetic Operators
operators
Assignment operators
Inc/dec operators
Relational operators
Logical operators
Bit wise operators
① Integer arithmetic : Conditional operators
Special operators
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
Arithmetic
Output:
Addition of a, b is : 60
Subtraction of a, b is : 20
Multiplication of a, b is : 800
Division of a, b is : 2
Modulus of a, b is : 0
Arithmetic
Shorthand or
Increment and Decrement Arithmetic operators
Assignment operators
Inc/dec operators
Decrement __
Increment ++
The prefix form of ++ (or --) and the suffix form of ++ (or --) are the
same if they are used in isolation, but they cause different effects
when used in an expression. The following code illustrates this:
int x=2 , y = 5 , z = 0;
x++ ; y++ ;
x=y++ + x++;
y=++y + ++x;
y=++y + x++;
y += ++y;
y += 1 + (++x);
y += 2 + x++;
Arithmetic operators
Assignment operators
Inc/dec operators
Relational
Relational Operators operators
Logical operators
Bit wise operators
Conditional operators
Special operators
Relational operators are used to find the relation between
two variables. i.e. to compare the values of two variables.
Exercise
int i=10, j=20, k=30;
float f=5.5;
char ch=‘A’;
• i<j
• (j+k)>=(i+k)
• i+f <=10
• i+(f <=10)
• ch==65
• ch >= 10*(i+f)
Arithmetic operators
Logical Operators
Assignment operators
Inc/dec operators
Relational operators
Logical operators
These operators are used to perform logical Bit wise operators
Conditional operators
operations on the given expressions. Special operators
For each of the following statements, assign variable names for the
unknowns and rewrite the statements as relational expressions.
1. A customer's age is 65 or more.
2. The temperature is less than 0 degrees and greater than -15
degrees.
3. A person's height is in between 5.8 to 6 feet.
4. The current month is 12 (December).
5. The person's age is 65 or more but less than 100.
6. A number is evenly divided by 4 or 400 but not with 100
7. A person is older than 55 or has been at the company for more
than 25 years.
8. A width of a wall is less than 4 meters but more than 3 meters.
9. An employee's department number is less than 500 but greater
than 1, and they've been at the company more than 25 years.
Arithmetic operators
Example program for Assignment operators
Inc/dec operators
Relational operators
logical operators in C Logical operators
Bit wise operators
Conditional operators
Special operators
Output:
&& Operator : Both conditions are true
|| Operator : Only one condition is true
! Operator : Both conditions are true. But, status is inverted as
false
Try this example program
and explain the results
#include<stdio.h>
int main()
{
int a=5, b=-7, c=0, d;
d = ++a && ++b || ++c;
printf("\n %d %d %d
%d",a,b,c,d);
}
Arithmetic operators
Assignment operators
Output:
Storage size for int data type:4
Storage size for char data type:1
Storage size for float data type:4
Storage size for double data
type:8