Operators
>>>Operators
Operator is a symbol that denotes an operation to be performed.
An expression is statement that must be evaluated to some result. Expression consists of operators and operands.
For example:
2+3 is an expression
+ is operator
2, 3 are operands
Operand can be number, variable or expression or combination
Example:
2+3
x+10
x+y
(x+y)*z
based on the number of operands, operators of c language
are 3 types:
1. unary : works with single operand Eg: ++
2. binary : works with two operands Eg: +
3. ternary : works with three operands (?:)
based on the type of operation, operators of c language are:
1. arithmetic operators
2. assignment operator
3. increment/decrement operators
4. relational or comparison operators
5. logical operators
6. Bitwise operators
Arithmetic Operators
used to perform basic arithmetic operations.
List of arithmetic operators:
+ – addition
– – subtraction (hyphen)
* – Multiplication (asterisk)
/ – division (returns quotient after division) (forward slash)
% – Modulus (returns remainder after division)
//Example program
#include<stdio.h>
void main()
{
int res;
res=5+2;
printf(“Result=%d”,res);
res=5-2;
printf(“\nResult=%d”,res);
res=5*2;
printf(“\nResult=%d”,res);
res=5/2;
printf(“\nResult=%d”,res);
res=5%2;
printf(“\nResult=%d”,res);
}
Assignment Operator
The equal to(=) symbol is called as Assignment operator in C Language.
The statement involved assignment operator is known as assignment statement.
syntax of assignment statement:
var=val/var/expr/func();
//Example
#include<stdio.h>
void main()
{
int a,b;
a=12;
b=24;
printf(“a=%d\n”,a);
printf(“b=%d\n”,b);
a=b; //try by replacing with b=a;
printf(“a=%d\n”,a);
printf(“b=%d\n”,b);
}
Exercise)
Write C Program to swap the values of two Variables
//Example program
#include <stdio.h>
void main() {
int x,y;
x=12;
y=24;
printf(“x=%d”,x); //x=12
printf(“\ny=%d”,y); //y=24
_____
_____
____
printf(“\n\nx=%d”,x); //x=24
printf(“\ny=%d”,y); //y=12
}
Short hand Notation or Compound Assignment
It can be used when we have same variable at both sides of assignment statement. It is a combination of assignment operator and other operators.
//Example
void main()
{
int x=10;
printf(“Value of x is=%d”,x);
x+=2; //x=x+2;
printf(“\n\nValue of x is=%d”,x);
int y=20;
x+=y; //x=x+y; or x=y+x;
printf(“\n\nValue of x is=%d”,x);
printf(“\nValue of y is=%d”,y);
y+=x; //y=x+y; or y=y+x;
printf(“\n\nValue of x is=%d”,x);
printf(“\nValue of y is=%d”,y);
}
Type casting/conversion
When we are assigning a value to a variable, the value at right hand side will be promoted or demoted based on the datatype of the variable at left hand side.
type conversion determined by the system is known as implicit type conversion.
Type conversion specified by the developer through code is known as explicit type conversion.
#include<stdio.h>
void main()
{
float x;
x=(float)’a’;
printf(“%f”,x);
}
Exercises:
1.Write C program to print ASCII value of a variable of ‘char’ type where value is user entered.
Hint: ASCII Value of ‘A’ is 65
2. Write C Program to display product of two user entered numbers. first number is integer and second number is float type.
3. write C Program to convert the desired temperature from forienheit(float type) to celcius(float type)
Hint: C = (F − 32)*5/9
>>>Increment/Decrement Operators
++ is known as Increment Operator
— is known as Decrement Operator
++ operator adds 1 to the existing value of variable and stores the result back into same variable.
Example:
x=x+1 can be written as x++
— operator subtracts 1 from the existing value of variable and stores the result back into same variable
Example:
x=x-1 can be written as x–
#include<stdio.h>
//example of increment/decrement operators
void main()
{
int x=10;
printf(“x=%d”,x);
x++; //x=x+1;
printf(“\nx=%d”,x);
x–; //x=x-1;
printf(“\nx=%d”,x);
}
Post/Pre Increment/Decrement
Post Increment -> x++
Pre Increment -> ++x
Post Decrement -> x–
Pre Decrement -> –x
Example 1:
#include <stdio.h>
void main()
{
int a=2;
printf(“a=%d\n”,a++);
printf(“a=%d”,a);
}
#include<stdio.h>
//example of Post/pre increment/decrement operators
void main()
{
int x=10,y;
y=x++ +5;
printf(“x=%d\n”,x);
printf(“y=%d”,y);
}
Relational or comparison operators
these are used to compare two values and returns True or False value. The expression contains relational operator is called as condition.
List of relational operators of C language:
> greater than
>= greater than or equal to
< less than
<= less than or equal to
== equal to
!= not equal to
//Example
#include<stdio.h>
void main()
{
int res;
res= 5>7;
printf(“result = %d”,res);
res= 5>=7;
printf(“\nresult = %d”,res);
res= 5<7;
printf(“\nresult = %d”,res);
res= 5<=7;
printf(“\nresult = %d”,res);
res=5==7;
printf(“\nresult = %d”,res);
res= 5!=7;
printf(“\nresult = %d”,res);
}
Logical or Boolean Operators
These operators works with logical value of their operands and returns True or False. Operands of logical operators can be numbers, variables or conditions.
Logical value of any non-zero value is 1.
Logical Operators of C Lang:
&& – AND operator
|| – OR operator
! – NOT operator
Example :
#include<stdio.h>
void main()
{
int res;
res=(5>7)&&(12<20);
printf(“res=%d”,res);
res=(5>7)||(12<20);
printf(“\nres=%d”,res);
res=!(5>7);
printf(“\nres=%d”,res);
}
Bitwise Operators
works with Binary representation of their operands.
Bitwise Operators of C Lang:
& – AND
| – OR
~ – Compliment (tild)
^ – XOR (carot)
>> – Right Shift
<< – Left Shift
//Example
#include<stdio.h>
void main()
{
int res;
res=4&5;
printf(“result=%d\n”,res);
res=4<<2;
printf(“result=%d\n”,res);
}