0% found this document useful (0 votes)
7 views32 pages

Unit 2 Lecture 4 LPU CAP

Unit 2 Lecture 4 LPU CAP

Uploaded by

Divyanshu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views32 pages

Unit 2 Lecture 4 LPU CAP

Unit 2 Lecture 4 LPU CAP

Uploaded by

Divyanshu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Programming Methodologies

Unit 2 Lec_5
We will discuss
• What is an Operator?
• Types of Operator
• Type Conversion
Operators in C Language

• C language supports a rich set of built-in operators.

• An operator is a symbol that tells the compiler to perform


certain mathematical or logical manipulations.

• Operators are used in program to manipulate data and


variables.
Operators in C Language

C operators can be classified into following types,


• Arithmetic operators
• Relation operators
• Logical operators
• Bitwise operators
• Assignment operators
• Conditional operators
• Special operators
Arithmetic operators

• C supports all the basic arithmetic operators.


#include <stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}
#include <stdio.h>
int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;
printf("++a = %d \n", ++a);
printf("--b = %d \n", --b);
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);
return 0;
}
Relation operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;
printf("%d == %d is %d \n", a, b, a == b);
printf("%d == %d is %d \n", a, c, a == c);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d < %d is %d \n", a, c, a < c);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d != %d is %d \n", a, c, a != c);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d >= %d is %d \n", a, c, a >= c);
printf("%d <= %d is %d \n", a, b, a <= b);
printf("%d <= %d is %d \n", a, c, a <= c);
return 0;
}
Logical operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;
result = (a == b) && (c > b);
printf("(a == b) && (c > b) is %d \n", result);
result = (a == b) && (c < b);
printf("(a == b) && (c < b) is %d \n", result);
result = (a == b) || (c < b);
printf("(a == b) || (c < b) is %d \n", result);
result = (a != b) || (c < b);
printf("(a != b) || (c < b) is %d \n", result);
result = !(a != b);
printf("!(a == b) is %d \n", result);
result = !(a == b);
printf("!(a == b) is %d \n", result);
return 0;
}
Bitwise operators

• Bitwise operators perform manipulations of data at bit level.


• These operators also perform shifting of bits from right to left.
Bitwise operators are not applied to float or double.
Bitwise operators
Bitwise AND Operator &
The output of bitwise AND is 1 if the corresponding bits
of two operands is 1. If either bit of an operand is 0, the
result of corresponding bit is evaluated to 0.
In C Programming, the bitwise AND operator is denoted
by &.
Bitwise OR Operator |

#include <stdio.h>
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
int main() { Bitwise OR Operation of 12 and
25
int a = 12, b = 25; 00001100 |
printf("Output = %d", a | b); 00011001 ________
00011101 = 29 (In decimal)

return 0;
}
Bitwise XOR
The result of bitwise XOR operator is 1 if the corresponding bits of two
operands are opposite. It is denoted by ^.
Bitwise XOR (exclusive OR) Operator ^

#include <stdio.h>
int main() 2 = 00001100 (In Binary)
25 = 00011001
{ int (In Binary) Bitwise XOR Operation of 12 and 25
a = 12, b = 25; 00001100 ^
00011001 ________
printf("Output = %d", a ^ b); 00010101 = 21 (In decimal)
return 0;
}
Shift Operators in C programming
Practical 2 :

G:1 10-oct-2022 12-2


G:2 11-oct-2022 12-2

It will be upto operators


• Assume variable 'A' holds 60 and variable 'B' holds 13
Assignment Operator
Conditional operator

• It is also known as ternary operator and used to evaluate conditional


expression.
epr1 ? expr2 : expr3

• If epr1 Condition is true ? Then value expr2 : Otherwise value expr3

printf("%d", a>b? a : b);


Special operator
C Programming Expression

1. In programming, an expression is any legal combination of symbols


that represents a value.
2. C Programming Provides its own rules of Expression, whether it is
legal expression or illegal expression. For example, in the C language
x+5 is a legal expression.
3. Every expression consists of at least one operand and can have one
or more operators.
4. Operands are values and Operators are symbols that represent
particular actions.
Type Conversion
• Type casting is a way to convert a variable from one data type to
another data type. For example, if you want to store a 'long' value
into a simple integer then you can type cast 'long' to 'int'. You can
convert the values from one type to another explicitly using the cast
operator as follows −

(type_name) expression
Example

main()
{
int sum = 17, count = 5;
double mean;
mean = (double) sum / count;
printf("Value of mean : %f\n",
mean );
}
Implicit Type Conversion
• When the type conversion is performed automatically by the compiler
without programmers intervention, such type of conversion is known
as implicit type conversion or type promotion. Also known as
‘automatic type conversion’.
• The compiler converts all operands into the data type of the largest
operand.
Explicit Type Conversion

• The type conversion performed by the programmer by posing the


data type of the expression of specific type is known as explicit type
conversion.
• The explicit type conversion is also known as type casting.
Example:

#include<stdio.h>
void main()
{
int a=5,sum;
double b=8;
b=a+b;//implicit conversion
clrscr();
printf("%f",b);
sum=(int)b+1; //explicit conversion
printf("\n\n%d",sum);
getch();
}

You might also like