PROGRAMMING
FOR
PROBLEM SOLVING
POWER POINT
BY:
PRESENTATION
Y.PADMAVATHI SANJANA
[22J21A05D4]
N.YAMUNA
[22J21A05D1]
C Operators,
Operands,
Expressions.
1/46
OPERATORS,OPERANDS,
EXPRESSIONS.
Operators are symbols which take one or more operands or
expressions and perform arithmetic or logical computations.
Operands are variables or expressions which are used in
conjunction with operators to evaluate the expression.
Combination of operands and operators form an expression.
Expressions are sequences of operators, operands, and
punctuators that specify a computation.
Evaluation of expressions is based on the operators that the
expressions contain and the context in which they are used.
2/46
C OPERATORS
An expression is any valid set of literals, variables,
operators, operands and expressions that evaluates
to a single value.
This value can be a number, a string or a logical value.
For instance a = b + c; denotes an expression in which
there are 3 operands a, b, c and two operator + and
=.Based on operands, operators are classified as
nullary (no operands), unary (1 operand), binary (2
operands), ternary (3 operands).
Expression can result in a value and can produce side effects.
A side effect is a change in the state of the execution environment.
Operators based on
operands
SPECIAL OPERATORS
C supports some special operators of interest such
as comma operator [,], Size of operator(), pointer
operators (& and *) and dot(.) operator.
C OPERATORS
Symbols Operators description
representation
Unary Operators
Unary plus maintains the value of the operand.
+ +aNumber
-
Any plus sign in front of a constant is not part
of the constant.
Unary minus operator negates the value of the
operand. For example, if num variable has the
value 200, -num has the value -200. Any -342
minus sign in front of a constant is not part of
the constant.
6/46
C OPERATORS
You can put the ++/-- before or after the operand. If it appears before
the operand, the operand is incremented/decremented. The
incremented value is then used in the expression.
If you put the ++/-- after the operand, the value of the operand is used
in the expression before the operand is incremented/decremented.
Post-increment. After the result is obtained,
++ the value of the operand is incremented by 1. aNumber++
Post-decrement. After the result is obtained,
-- the value of the operand is decremented by aNumber--
1.
Pre-increment. The operand is incremented
++ by 1 and its new value is the result of the ++yourNumber
expression.
Pre-decrement. The operand is decremented
-- by 1 and its new value is the result of the --yourNumber
expression. 7/46
PROGRAM FOR UNARY PLUS
#include <stdio.h>
#include <conio.h>
int main ()
{
int a = 10; // use unary plus operator
int b = (-10); // It does not change the operand value
printf (" The value of a: %d \n", a);
printf (" The value of b: %d \n", b);
return 0;
}
Output:
The value of a: 10
The value of b: -10
PROGRAM FOR UNARY INCREMENT AND DECREMENT OIPERATOR
// Working of increment and decrement operators
#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;
} Output:
++a = 11
--b = 99
++c = 11.500000
--d = 99.500000
C OPERATORS
ARITHMETIC OPERATORS
operator description Example
+ Adds two operands a+b=20
Subtracts second a-b=10
- operand from first
* Multiplies both operands a*b=44
/ Divides numerator by
denominator
b/a=1
% Gives the remainder
when divided by the
b%a=0
denominator
10/46
PROGRAM FOR ARITHMETIC OPERATORS
#include<stdio.h>
#include<conio.h>
int main()
{ int a,b;
int op;
printf(" 1.Addition\n 2.Subtraction\n 3.Multiplication\n 4.Division\n");
printf("Enter the values of a & b: ");
scanf("%d %d",&a,&b);
printf("Enter your Choice : ");
scanf("%d",&op);
switch(op) Output:
{
case 1 :
printf("Sum of %d and %d is : %d",a,b,a+b);
break; 1.Addition
case 2 : 2.Subtraction
printf("Difference of %d and %d is : %d",a,b,a-b);3.Multiplication
break;
4.Division
case 3 :
Enter the values of a &
printf("Multiplication of %d and %d is : %d",a,b,a*b);
break; b:
case 4 : 20 15
printf("Division of Two Numbers is %d : ",a/b);
break; Enter your Choice : 1
default : Sum of 20 and 15 is : 35
printf(" Enter Your Correct Choice.");
break;
}
return 0;
}
C OPERATORS
Relational Inequality Operators
The relational operators compare two operands and determine the validity of a
relationship.
< Specifies whether the value of the left operand is less than the i < 7
value of the right operand.
Specifies whether the value of the left operand is greater than
> the value of the right operand.
j > 5
Specifies whether the value of the left operand is less than or
<= equal to the value of the right operand.
k <= 4
Specifies whether the value of the left operand is greater than
>= or equal to the value of the right operand.
p >= 3
13/46
C OPERATORS
Relational Equality Operators
The equality operators, like the relational operators, compare two operands for the validity of a
relationship.
Indicates whether the value of the left operand is equal to the value
of the right operand.
The equality operator (==) should not be confused with the
assignment (=) operator.
==
X == 6
For example:
if (x == 4) evaluates to true if x is equal to four.
while
if (x = 4) is taken to be true because (x = 4) evaluates to a nonzero
value (4). The expression also assigns the value 4 to x.
Indicates whether the value of the left operand is not equal to the
!= X!= 6
value of the right operand.
14/46
C OPERATORS
Expressions Evaluates as
(3 == 3) && (4 != 3) True (1) because both operands are true
True (1) because (either) one
(4 > 2) || (7 < 11)
operand/expression is true
(3 == 2) && (7 == 7) False (0) because one operand is false
! (4 == 3) True (1) because the expression is false
NOT(FALSE) = TRUE
NOT(TRUE) = FALSE
15/46
PROGRAM FOR RELATIONAL OPERATORS
#include <stdio.h>
int main() Output:
{
int p = 5, q = 5, r = 10;
printf(“%d == %d is %d \n”, p, r, p == r); 5 == 10 is 0
printf(“%d == %d is %d \n”, p, q, p == q);5 == 5 is 1
printf(“%d > %d is %d \n”, p, r, p > r); 5 > 10 is 0
printf(“%d > %d is %d \n”, p, q, p > q); 5 > 5 is 0
printf(“%d < %d is %d \n”, p, r, p < r); 5 < 10 is 1
printf(“%d < %d is %d \n”, p, q, p < q); 5 < 5 is 0
printf(“%d != %d is %d \n”, p, r, p != r); 5 != 10 is 1
printf(“%d != %d is %d \n”, p, q, p != q); 5 != 5 is 0
printf(“%d >= %d is %d \n”, p, r, p >= r); 5 >= 10 is 0
printf(“%d >= %d is %d \n”, p, q, p >= q);5 >= 5 is 1
printf(“%d <= %d is %d \n”, p, r, p <= r); 5 <= 10 is 1
printf(“%d <= %d is %d \n”, p, q, p <= q);5 <= 5 is 1
return 0;
}
C OPERATORS
Logical NOT Operator (unary)
Logical not operator. Produces value 0 if its
operand or expression is true (nonzero) and the
!(4 == 2)
! value 1 if its operand or expression is false (0). The
!x
result has an int type. The operand must be an
integral, floating, or pointer value.
Operand (or expression) Output
!0 1 ( T )
!1 0 ( F )
16/46
C OPERATORS
Logical AND Operator
Indicates whether both operands are true.
If both operands have nonzero values, the result has the value 1.
Otherwise, the result has the value 0. The type of the result is
int. Both operands must have an arithmetic or pointer type. The
&& usual arithmetic conversions on each operand are performed. x && y
The logical AND (&&) should not be confused with the bitwise AND
(&) operator. For example:
1 && 4 evaluates to 1 (True && True = True)
while
1 & 4 (0001 & 0100 = 0000) evaluates to 0
Operand1 Operand2 Output
0 0 0 ( F )
0 1 0 ( F )
1 0 0 ( F )
1 1 1 ( T )
17/46
C OPERATORS
Logical OR Operator
Indicates whether either operand is true. If either of the
operands has a nonzero value, the result has the value
1. Otherwise, the result has the value 0. The type of the
result is int. Both operands must have a arithmetic or
|| pointer type. The usual arithmetic conversions on each x || y
operand are performed.
The logical OR (||) should not be confused with the
bitwise OR (|) operator. For example:
1 || 4 evaluates to 1 (or True || True = True)
while 1 | 4 (0001 | 0100 = 0101) evaluates to 5
Operand1 Operand2 Output
0 0 0 ( F )
0 1 1 ( T )
1 0 1 ( T )
1 1 1 ( T ) 18/46
PROGRAM FOR LOGICAL OPERATORS
// Working of 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); Output:
result = (a == b) || (c < b);
printf("(a == b) || (c < b) is %d \n", result); (a == b) && (c >
result = (a != b) || (c < b); b) is 1
printf("(a != b) || (c < b) is %d \n", result); (a == b) && (c <
result = !(a != b); b) is 0
printf("!(a != b) is %d \n", result); (a == b) || (c < b)
result = !(a == b); is 1
printf("!(a == b) is %d \n", result); (a != b) || (c < b) is
return 0; 0
} !(a != b) is 1
C OPERATORS
Conditional Operator (ternary)
The first operand/expression is evaluated, and
its value determines whether the second or
third operand/expression is evaluated:
1. If the value is true, the second
operand/expression is evaluated.
?: 2. If the value is false, the third size != 0 ? size : 0
operand/expression is evaluated.
The result is the value of the second or third
operand/expression. The syntax is:
First operand ? second operand :
third operand
Program example: tenary conditional operator
19/46
PROGRAM FOR
// C program toCONDITIONAL OPERATOR(TERNARY)
find largest among two
// numbers using ternary operator
#include <stdio.h>
int main()
{
int m = 5, n = 4;
(m > n) ? printf("m is greater than n that is
%d > %d", m, n)
: printf("n is greater than m that is %d >
%d",n, m);
return 0;
}
Output:
m is greater than n that is
5>4
C OPERATORS
BITWISE OPERATORS :
• Bitwise operators perform manipulations of data at bit
level.
• These operators also perform shifting of bits from right to
left.
Operator are not
• Bitwise operators Description
applied to float or double.
& Bitwise AND
| Bitwise OR
^ Bitwise
exclusive OR
~ Bitwise not
<< Left shift
>> right shift
C OPERATORS
Truth table for bitwise &,|,^ and ~ :
Bitwise NOT
Bitwise NOT
operator
operation
Operand ~operand
0 1
1 0
C OPERATORS
C OPERATORS
• Assignment Operator :
• Assignment operators are used to assign the result of an
expression
to a variable.
• Assignment operators are classified into two types.
They are :
1. Simple assignment operator ( Example : = ) .
2. Compound assignment operators ( Example : +=, -
=,×=,/=,%=,&=,^= ).
C Operators
Assignment operators
Operation Example
operators
= a == b a==b
+= a += b a = a+b
-= a -= b a = a- b
×= a ×= b a = a× b
/= a /= b a=a/b
%= a %= b a = a% b
&= a &= b a = a& b
^= a ^= b a = a^ b
C Operators
End of C Operators,
Operands,
Expressions.
46/29