Unit 2
Unit 2
Operators
Arithmetic Operators:
• An arithmetic operator performs mathematical operations such as addition,
subtraction, multiplication, division etc on numerical values.
• Assume that a=10 and b=20, the following are the arithmetic operators:
Operator Description example
Note:
• The modulus(%) operator can be applied to integer operands and cannot
be used for float or double operands.
• All other arithmetic operators can accept a mix of integer and floating
point numbers.
Example program for arithmetic operators:
#include <stdio.h>
int main()
{
int a = 10, b = 4, c;
printf("a is %d and b is %d\n", a, b); // printing a and b
c = a + b; // addition
printf("a+b is %d\n", c);
c = a - b; // subtraction
printf("a-b is %d\n", c);
c = a * b; // multiplication
printf("a*b is %d\n", c);
c = a / b; // division
printf("a/b is %d\n", c);
c= a % b; // modulus
printf("a%b is %d\n", c);
getch();
return 0;
}
Relational Operators:
• A relational operator checks the relationship between two operands.if
relation is true, it returns 1;if the relation is false, it returns 0.
• Relational operators are used in decision making and loops
• Assume that a=10 and b=20, the following are the relational operators.
Operator Description example
#include<stdio.h>
#include<conio.h>
main()
int a=10,b=20,c=30,res;
clrscr();
res=(a>b)||(b>c);
printf("(a>b)||(b>c) is %d\n",res);
res=(a<b)&&(b<c);
printf("(a<b)&&(b<c) is %d\n",res);
res=!((a<b)&&(b<c));
printf("!((a<b)&&(b<c)) is %d\n",res);
res=!((a>b)||(b>c));
printf("!((a<b)||(b<c)) is %d\n",res);
getch();
Assignment Operators:
• An assignment operator is used to assign a value to a variable.
#include<stdio.h>
#include<conio.h>
main()
int a,b;
clrscr();
scanf("%d%d",&a,&b);
b=a;
b+=a;
b-=a;
b*=a;
a<<=2;
getch();
}
Bitwise Operators:
• Bit wise operators are those operator which performs operations at bit
level.
• Assume variable 'A' holds 60 and variable 'B' holds 13, then
A = 0011 1100
B = 0000 1101
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
#include<stdio.h>
#include<conio.h>
main()
int a,b,c;
clrscr();
scanf(“%d%d”,&a,&b);
c=a&b;
c=a|b;
printf(“a | b is %d\n”,c);
c=a>>b;
c=a<<b;
printf(“a << b is %d\n”,c);
c=~a;
printf(“~a is %d\n”,c);
getch();
Miscellaneous operators:
1.unary operators:
Conditional Operator:
• Conditional operator is also known as ternary operator as it takes
three operands.
• It is just like an if-else statements that can be used within
expressions.
• This operator is useful in situations in which there are 2 or more
alternatives for an expression.
Syntax:
exp1 ? exp2 : exp3
• exp1 is evaluated first. If it is true, then exp2 is evaluated and becomes
the result of the expression,otherwise exp3 is evaluated and becomes the
result of the expression.
Ex: large = (a > b) ? a : b
Address operator(&):
• It returns the actual address of the variable.
Indirection operator(*):
• It is a pointer to the variable.
Selection & Making Decisions
Selection & Making Decisions/decision control/conditional/branching
statements:
Conditional Statements in C programming are used to make decisions based
on the conditions.
c supports 2 types of conditional statements.
1.2-way selection
• Simple if(Null else)
• If else
• Nested if
2.multiway selection
• Else if ladder
• Switch
Simple if statements:
• The single if statement in C language is used to execute the code if a
condition is true. It is also called one-way selection statement.
Syntax:
if(condition)
{
// Statements to execute if condition is true
}
• If the condition is evaluated as true, then the block statement is executed,
but if the condition is evaluated as false, then control is passed to the next
Statement following it.
Example program for simple if:
#include<stdio.h>
#include<conio.h>
main()
{
int a=10;
clrscr();
if(a>15)
{
printf(“a is greater than 15\n”);
}
printf(“a is less than 15\n”);
getch();
}
If else statement:
• If the given condition is true, then program control goes inside the if
block and executes the Statement.
• The condition is false, then program control goes inside the else block
and executes the corresponding Statement.
Syntax:
if(expression or condition)
// statement1 ;
}
else
//statement 2;
if(a<b)
{
printf("a is less than b\n");
}
else
{
printf("a is greater than b\n");
}
}
else
{
printf("a is equal to b\n");
}
getch();
}
Else if ladder:
• If else if is used to check multiple conditions.
• If one among the multiple conditions is true then the statement related to
that particular condition is executed and all other conditions are ignored.
• If none of the condition are true then the final else statement is executed.
Syntax:
If(condition1)
{
//statement1;
}
Else if(condition2)
{
//statement2;
}
Else if(condition3)
{
//statement3;
}
…
.
.
Else
{
//statement in else
}
#include<stdio.h>
#include<conio.h>
main()
int day;
clrscr();
printf("enter a day(1-7)");
scanf("%d",&day);
switch(day)
{
case 1:
printf("monday");
break;
case 2:
printf("tuesday");
break;
case 3:
printf("wednesday");
break;
case 4:
printf("thursday");
break;
case 5:
printf("friday");
break;
case 6:
printf("saturday");
break;
case 7:
printf("sunday");
break;
default:
printf("invalid day\n");
}
getch();
1.classifying functions
2.converting functions
1.Classifying functions :
function description
Example program:
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
main()
char c;
clrscr();
printf(“enter a character\n”);
scanf(“%c”,&c);
if(islower(c))
else if(isupper(c))
else
printf(“invalid character\n”);
getch();
}
2.converting functions:
Repetition/iterative statements/loops:
for(initialization;condition;incrementation/decrementation)
{
Statement block;
}
• When the compiler encounter a continue statement then the rest of the
statements in the loop are skipped and the control is unconditionally
transferred to the loop continuation portion of the nearest enclosing loop.
Syntax/general form:
Continue;
#include<stdio.h>
#include<conio.h>
main()
int i;
clrscr();
for(i=1;i<=10;i++)
if(i==5)
continue;
getch();
Goto statement:
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
printf(“statement1\n”);
printf(“statement2\n”);
goto label;
printf(“statement3\n”);
printf(“statement4\n”);
label:
printf(“statement5\n”);
printf(“statement6\n”);
getch();