0% found this document useful (0 votes)
0 views15 pages

2nd Unit

The document covers expressions, operators, and their associativity in programming, particularly in C. It details various types of operators including arithmetic, relational, logical, assignment, increment/decrement, conditional, bitwise, and special operators, along with their precedence and examples. Additionally, it explains how to solve equations using operator precedence and associativity rules.

Uploaded by

ujjwalsingh52022
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)
0 views15 pages

2nd Unit

The document covers expressions, operators, and their associativity in programming, particularly in C. It details various types of operators including arithmetic, relational, logical, assignment, increment/decrement, conditional, bitwise, and special operators, along with their precedence and examples. Additionally, it explains how to solve equations using operator precedence and associativity rules.

Uploaded by

ujjwalsingh52022
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/ 15

UNIT-2

1. Expression, Operators and Associativity


 An expression is a combination of variables/operands, constants and operators written
according to the syntax of the language and evaluates to a value.
e.g. 7+2*9-4*8+5-4
 An operator is a symbol that the computer to perform certain mathematical or logical
operations. C supports a rich set of operators.
An operator operates on certain data types.
e.g. A+B, A and B are OPERANDS and + is an operator
 The order in which the operators in an expression are performed is determined by its
precedence order. If two operators of same precedence occurs in an expression then
calculation is done according to the ASSOCIATIVITY (either left to right or right to left).
e.g. 2+3-1
two operators are same precedence, then check its associativity which is left to right.
(2+3)-1  5-1  4

Types of Operator

1.1 Arithmetic Operators: have ASSOCIATIVITY Left to Right e.g. ( + , - , * , / , % )


1.2 Relational Operators: have ASSOCIATIVITY Left to Right e.g.( < , > , <= , >= , == , != )
1.3 Logical Operators: have ASSOCIATIVITY Left to Right e.g. ( && , || , ! )
1.4 Assignment Operators: have ASSOCIATIVITY R to L e.g.( = , v op = ( exp )  v = v op (exp) )
1.5 Increment and Decrement Operators: have ASSOCIATIVITY Right to Left e.g. ( ++ , -- )
1.6 Conditional Operators: have ASSOCIATIVITY Right to Left e.g. ( if i>j ? i: j )
1.7 Bitwise Operators: have ASSOCIATIVITY Left to Right e.g. ( << , >> )
1.8 Special Operators: have ASSOCIATIVITY Left to Right e.g. ( , , sizeof(int), &, * )

1.1 Arithmetic Operators:

* Modulo Division: Always consider the sign of numerator.


14%2 = 0
-14%4 = -2
-14%-4 = -2
14%-4 = 2
-14.4%4= Error b’coz Modular operation cannot be used for floating point numbers.

1.2 Relational Operators:

A relational operator is used to compare two values and the result of such an operation is either 1 or
0. It is 1 if the relation is true and 0 if the relation is false. Left hand side value is compared with the
right hand side.
If expression: x>y
So, x and y are operands and > is a relational operator.
10 < 20  true or 1
4.5 <= 10  true or 1
-10 >= 4.5  false or 0
10 !=15  true or1

Mr. Abhinav Gupta 1


1.3 Logical Operators:

X y x&&y x||y !x !y
0 0 0 0 1 1
0 1 0 1 1 0
1 0 0 1 0 1
1 1 1 1 0 0

e.g. if (10>5) && (7==7) e.g. If (10!=10) || (15>20)


true true false || false
= 1 && 1 = 0 0
= 1 = 0

1.4 Assignment Operators:

These are used to assign the result of an expression to a variable.


1.4.1 Simple Assignment
x=5; i.e. 5 is assigned to x
a=b=c=d=10; i.e. 10 is assigned to d, d which becomes 10 is assigned to c and so on
y=2+3; i.e. 2+3= (5 is assigned to y)
1.4.2 Compound Assignment
General form is variable(v) operator(op) = (exp) is equivalent to v = v op (exp)
e.g. x + = (5+y) is equivalent to x = x + (5+y)
 Compound assignment are +=, -=, *=, /=, %=
For example: if x=4 then x %= 2 is equivalent to x = x %2
x = 4 %2
= 0

1.5 Increment and Decrement Operators (Unary operator):

1.5.1 Increment operator: ++ operator adds 1 to the operand.

They are used in 2 ways postfix and prefix.

Postfix <variable name ++> e.g. m++ (Postfix) Use then Change
e.g. int m=1,t;  t = m++;  t = 1 and m = 2
Prefix <++ variable name> e.g. ++m (Prefix) Change then Use
e.g. int m=1,t;  t = ++m;  t = 2 and m = 2

1.5.2 Decrement operator: -- operator subtracts 1 to the operand.

They are also used in 2 ways postfix and prefix.

Postfix <variable name -- > e.g. m-- (Postfix) Use then Change
e.g. int m=1,t;  t = m--;  t = 1 and m = 0
Prefix < -- variable name> e.g. --m (Prefix) Change then Use
e.g. int m=1,t;  t = --m;  t = 0 and m = 0

Mr. Abhinav Gupta 2


1.6 Conditional Operators:

It consists of 2 symbols: the question mark ? and colon : . This is the only operator in C that takes
three operands. It is known as ternary operator.
e.g. larger = i>j ? i:j
here ? and : are conditional operator.
If the condition(i>j) is true then i will be the answer otherwise answer is j.

Syntax: variable = (conditional test expression) ? expression1 : expression2

e.g. it is to find largest of 3 numbers.

Larger = (a>b) ? (a>c) ? a : c : (b>c) ? b : c


If a=5, b=4, c=3
Larger = (5>4) ? (5>3) ? 5 : 3 : (4>3) ? 4 : 3
Larger = 5>4 ? 5 : 4
= 5

1.7 Bitwise Operators:

The bitwise operator operates on each bit of data. These operators are used for testing,
complementing or shifting bits to the right or left. These operators are not use in case of float and
double.

<< Shift left


>> Shift right
& Bitwise AND
| Bitwise OR

Shift left(<<):

e.g. m = j << 1
value of j shifted to left by 1 bit and adding bit 0 to the right of it where i shifted to right by 1 bit and
adding 0 to the right.

e.g.1 j = 00010000
m= j <<1 (shift left by 1 bit)

0 0 0 1 0 0 0 0

0 0 1 0 0 0 0 0
Blank space left. So, add 0

 J=00010000 is equal to 16 but after shifting left by 1 bit, it becomes 32. i.e. shifting a variable
to the left by 1 position is equal to multiply it by 2. So shifting 2 positions (2x2) i.e. multiplying
the number by 4. Similarly 3 positions (2x2x2) i.e. multiplying the number by 8 and so on…

Mr. Abhinav Gupta 3


shift right(>>): n = i >> 1

e.g.2 a = 00010101
m= a >>2 (shift right by 2 bit)
0 0 0 1 0 1 0 1

0 0 0 0 0 1 0 1
Two blank space left. So, add 0

 a is 00010101 is equal to 20 but after shifting right by 2 bit, it becomes 5. i.e. shifting a
variable to the right by 2 position is equal to divide it by 4. So shifting 1 positions (2) i.e.
dividing the number by 2. Similarly 3 positions (2x2x2) i.e. dividing the number by 8 and so
on…

e.g.3
a = 123 & 21 BITWISE AND
0 1 1 1 1 0 1 1 (123)
0 0 0 1 0 1 0 1 (21)
_________________
0 0 0 1 0 0 0 1 (17) which is assign to a

e.g.4

a = 123 | 21 BITWISE OR
0 1 1 1 1 0 1 1 (123)
0 0 0 1 0 1 0 1 (21)
_________________
0 1 1 1 1 1 1 1 (127) which is assign to a

1.8 Special Operators:


C supports some special operators such as comma, sizeof operator, pointer operator ( & and *) etc.

 using comma operator:


e.g. int i , j;
i = (j=3 , j+2);
i.e. j=3 and i=5
Because value of 3 will be assigned to j and then j+2 will be 5 which is assigned to left variable (i).
e.g. value = (x=10, y=5, x+y);
value = 15
Here first x is initialized value 10, then y is initialized 5 and after that value is evaluated as x+y. So
value=15.

 Sizeof operator:
The sizeof operator is a compile time operator and when used with an operand, it returns the no. of
bytes the operand occupies. The operand may be a variable, a constant or a data type.
e.g. int sum;
m = sizeof(sum);
output 2
e.g. float marks;
n = sizeof(marks);
output 4

Mr. Abhinav Gupta 4


2. Precedence and Associativity of All Operands:

Precedence Operator Description Example Associativity

(a + b) / 4; and array[1] =
() and [] Parentheses, Braces
2;
1 ++ Post-Increment left to right
for( i = 0; i < 10; ++i )
-- Post-Decrement
for( i = 10; i > 0; --i )

~ Bitwise complement flags = ~flags;


++ Pre-Increment for( i = 0; i < 10; ++i )
2 -- Pre-Decrement for( i = 10; i > 0; --i ) right to left
- or + Unary minus or Unary plus int i = -1; or int i = +1;
sizeof Return size in bytes int size =sizeof(float);

* or / Multiplication or Division int i = 2*4; or =10/3;


3 left to right
% Modulus int rem = 4 % 3;
4 + or - Addition or Subtraction int i = 2 + 3; or = 5 - 1; left to right
<< Bitwise shift left int flag = 33 << 1;
5 left to right
>> Bitwise shift right int flag = 33 >> 1;

Comparison less-than
< Comparison less-than-or- if( i < 42 ) ...
<= equal-to if( i <= 42 ) ...
6 left to right
> Comparison greater-than if( i > 42 ) ...
>= Comparison greater-than-or- if( i >= 42 ) ...
equal-to

== Comparison equal-to if( i == 42 ) ...


7 left to right
!= Comparison not-equal-to if( i != 42 ) ...
if( conditionA &&
8 && Logical AND left to right
conditionB )
if( conditionA ||
9 || Logical OR left to right
conditionB ) ...
Ternary conditional (if-then-
10 ? and : int i = (a > b) ? a : b; right to left
else)
= Assignment operator int a = b;
+= Increment and assign a += 3;
-= Decrement and assign b -= 4;
*= Multiply and assign a *= 5;
11 right to left
/= Divide and assign a /= 2;
%= Modulo and assign a %= 3;
<<= Bitwise shift left and assign flag <<= 2;
>>= Bitwise shift right and assign flag >>= 2;
Sequential evaluation
12 , for( i = 0; i < 10; i++ ) left to right
operator

Mr. Abhinav Gupta 5


Solve the equations:
e.g. 3*8/4%4*5
All *, /, % have same precedence. So, check associativity from table which is left to right.
= (3 * 8) / 4 % 4 * 5
= ( 24 / 4 ) % 4 * 5
= (6 % 4 ) * 5
= (2 * 5 )
= 10
e.g. a +=b *=c -=5
+=, *=, -= have same precedence. So check associativity from table which is right to left.
If a=3, b=5, c=8
= a +=b *=c -=5
= a +=b *=(c = c-5)
= a +=b *=(c = 8-5)
= a +=b *= 3
= a += b *=3
= a += (b = b*3)
= a += (b = 5*3)
= a += 15
= (a = a + 15)
= (a = 3 + 15)
= 18 So, a=18, b=15 and c=3

e.g. 6 != 5 && 7<8 && 9>10


= 6 != 5 && (7<8) && 9>10 < and > have highest precedence and associativity left to right
= 6 != 5 && 1 && (9>10)
= 6 != 5 && 1 && 0 now calculate lowest precedence !=
= (6 !=5)&& 1 && 0
= 1 && 1 && 0
= 1 && 0
= 0

e.g. - - 3 * 3 + 4 / 2 – 5 ++ * 4 - and ++ have highest precedence and associativity right to left


= - - 3 * 3 + 4 / 2 – (5++) * 4
=--3*3+4/2– 5 *4
= (- -3)* 3 + 4 / 2 – 5 * 4
= 2 *3+4/2– 5 *4
= (2 * 3) + 4 / 2 – 5 * 4
= 6 + (4 / 2) – 5 * 4
= 6 + 2 – (5 * 4)
= 6 + 2 – 20
= 8 -20
= -12
e.g. Expression m+=100 is equivalent to…… m=m+100

e.g. - - 3 * (3+4) / 2 – 5 ++ * 4
= - - 3 * 7 / 2 - (5++) * 4
= (- - 3)* 7 / 2 - 5 * 4
= (2 * 7) / 2 - 5 * 4
= 14 / 2 - 5 * 4
= 7 - (5 * 4)
= 7 - 20
= -13

Mr. Abhinav Gupta 6


3. Type Conversion For mixed Operands:
Mixed operands means that the operands in an expression can be of different types such as integers,
float, char etc.
To convert 1 type value to other, We have 2 type conversions:
3.1 Implicit type conversion
3.2 Explicit type conversion
3.1 Implicit type conversion:
 When the types of 2 operands in a binary expression are different, C automatically converts 1
type to another on the basis of conversion rules. This is known as implicit type conversion.
 In this PROMOTION occurs if right expression has lower rank; DEMOTION occurs right
expression has a higher rank.
Conversion ranks are: Each type has a Rank.

7 double NOTE:
6 float
5 long SOME ASCII VALUES
4 Int
3 Short int value  char value
2 Char
48 to 57  0 to 9
1 Bool
65 to 90  A to Z
1. Example of Promotion in Implicit type conversion:
bool b=true; 97 to 122  a to z
char c=’A’;
int i=1234;
long double d=3458.0004
c=b; // value of c is ASCII OF 1
i=c; // value of i is 65
d=b; // value of d is 1.0
d=i; // value of d is 1234.0
2. Example of Demotion in Implicit type conversion:
bool b=false;
char c=’A’;
short s=78;
int j=32767;
int k=65;
b=c; // value of b is 1(true)
s=j; // value of s is unpredictable or 32767
c=k+1; // value of c is ‘B’
3.2 Explicit type conversion:
It uses unary type operator, which convert data from one type to another. We specify new type in
parenthesis before the value we want to convert.
e.g. (float) a;
Here a is integer but the value of expression is converted to float.
e.g.1 int a;
a = (int)21.3 / (int)4.5
= 21 / 4
= 5
e.g.2 float a;
a = (int) 21.3 / (int) 4.5
= 21 / 4
= 5.000000

Mr. Abhinav Gupta 7


4. Decision Control Statements:
 In decision control statements (if, if-else, if-else if and nested if), group of statements are executed
when condition is true. If condition is false, then else part statements are executed.
 There are types of decision making control statements in C language. They are,
 Syntax and flow chart for each C decision control statements are given in below table with
description.

4.1 if statements

if (condition)
{
//Block of C statements here
//These statements will only execute if the condition is true
}

4.2 if-else statements

if(condition)
{
// Statements inside body of if
}
else
{
//Statements inside body of else
}

Mr. Abhinav Gupta 8


4.3 if- else if statements

if (condition1)
{
//These statements would execute if the condition1 is true
}
else if(condition2)
{
//These statements would execute if the condition2 is true
}
else if (condition3)
{
//These statements would execute if the condition3 is true
}
.
.
else
{
//These statements would execute if all the conditions return false.
}

Mr. Abhinav Gupta 9


4.4 nested if statements

if (condition1)
{
//These statements would execute if the condition1 is true
if (condition2)
{
//These statements would execute if the condition2 is true
}
}

 Program for if else statement in C:


In C if else control statement, group of statements are executed when condition is true. If
condition is false, then else part statements are executed.
#include <stdio.h>
void main()
{
int m=40,n=20;
if (m == n)
Output: m and n are not equal
{
printf(" m and n are equal ");
}
else
{
printf(" m and n are not equal ");
}
}

Mr. Abhinav Gupta 10


 Program for nested if-else if statement in C:
 In “nested if-else if” control statement, if condition 1 is false, then else if condition 2 is
checked and statements are executed if it is true.
 If condition 2 is false, then else part is executed.

#include <stdio.h>
void main()
{
int m=40,n=20;
if (m>n)
{
printf(" m is greater than n ");
} Output: m is greater than n
else if(m<n)
{
printf(" m is less than n ");
}
else
{
printf(" m is equal to n ");
}
}

 Program to check vowel or consonant in C:


#include <stdio.h>
void main()
{
char ch;
int lower,upper;
printf(“Enter a character\n”);
scanf(“%c”,&ch);
lower= (ch == ‘a’ || ch == ‘e’ || ch == ‘i’ || ch == ‘o’ || ch == ‘u’ );
upper= (ch == ‘A’ || ch == ‘E’ || ch == ‘I’ || ch == ‘O’ || ch == ‘U’ );
if (lower || upper)
{
printf(" %c is Vowel ", ch);
} Output:
else Enter a character
{ E
printf(" %c is Consonant ", ch); E is Vowel
}
else
{
printf(" m is equal to n ");
}
}

Mr. Abhinav Gupta 11


 Program to check Leap Year or not in C:
#include <stdio.h>
void main()
{
int yr;
printf(“Enter a Year\n”);
scanf(“%d”,&yr);
if (yr % 4 == 0)
{
if (yr % 100 == 0)
{
if (yr % 400 == 0)
printf(" %d is Leap Year ", yr);
else
printf(" %d is not Leap Year ", yr);
}
else
{
printf(" %d is Leap Year ", yr);
}
}
else
{
printf(" %d is not Leap Year ", yr);
}

Output:
Enter a Year
1600
1600 is Leap Year

 Program to check Largest among 3 different numbers in C:


 Program to find roots of a quadratic equation in C:
 Program to check Leap Year or not in C:
 Program to checkthe number is positive, negative or zero in C:
 Program to check alphabet or not in C:
 Program to check Even or Odd in C:
 Program to check 2 numbers are equal or not in C:
 Program to check number is divisible by 3 and 5 or not in C:
 Program to check number is divisible by 3 or 5 or not in C:
 Program to find second largest number among 3 in C:

Mr. Abhinav Gupta 12


5 switch statements:
The switch statement allows us to execute one code block among many alternatives.
You can do the same thing with the if...else..if ladder. However, the syntax of the switch statement is
much easier to read and write.
5.1 Syntax of switch case:
switch ( expression )
{
case label1 : Statement(s);
break;
case label2 : Statement(s);
break;
case label3 : Statement(s);
break;
.
.
.
default: statement(s);
break;
}

5.2 Rules of using switch case


1 Case Label must be unique.
2 Case Labels must end with colon.
3 Case Label must be integer or character (Not be floating point number). E.g. case ‘A’ :
4 Switch case should have only(or at most) 1 default label.
5 Relational operator are not allowed in switch case. E.g. case <10 : // invalid

Mr. Abhinav Gupta 13


5.3 How does the switch statement work?
 The expression is evaluated once and compared with the values of each case label.
 If there is a match, the corresponding statements after the matching label are executed. For
example, if the value of the expression is equal to constant2, statements after case
constant2: are executed until break is encountered.
 If there is no match, the default statements are executed.
 If we do not use break, all statements after the matching label are executed.
 By the way, the default clause inside the switch statement is optional.

5.4 Use of break and default statements in switch case:


You can use the break statement to end processing of a particular labeled statement within
the switch statement. It branches to the end of the switch statement.
The default statement is executed if no case constant-expression value is equal to the value of
expression.

WAP in C to check that the entered character is vowel or not using switch case.
WAP on C to create a calculator to perform + , - , * and / using case control statement.

6 Comments
Make the program more readable.
Compiler ignores the comments when it translates the programs into executable code.
C uses two different formats:-
6.1 Block comments: When we cover more than one lines such as /*…………………*/
6.2 Line comments: When we cover only one line such as //

7 Identifiers(User Defined Name)


It allows us to name data and other objects in the program. Identifier name can be maximum 31
characters but some compiler allow upto 247 characters.

e.g. int a,b; where, a and b are identifiers which is defined by user.

Rules For identifiers


7.1 Valid symbols are the capital letters A to Z, lower case letter a to z, digits 0 to 9 and underscore ( _ ).
e.g. area, _Name //valid
e.g. $Sum //invalid
7.2 first character of identifiers cannot be a digit. e.g. 2name //invalid
7.3 Name created cannot be keyword. e.g. int //invalid
7.4 It may not have space or hyphen. e.g. total salary //invalid
e.g. student-name //invalid
7.5 No commas or blanks are allowed e.g. Roll,no //invalid

8 Keyword(Pre-defined Name)
Keywords are those words whose meaning has been fixed. All keywords must be written in lower
case. There are only 32 keywords in C.
e.g. int, if, else, float, char, do, while,for etc.

Mr. Abhinav Gupta 14


Q. Predict the output of following C program.
#include<stdio.h>
void main()
{
int a, b = 10;
a = -b--;
printf("a = %d, b = %d", a, b);
}
Output:
a = -10, b = 9

#include<stdio.h> #include<stdio.h>
int main() int main()
{ {
int a = 5; int a = 5;
int b = ++a * a++; ++ has highest precedence then * switch(a)
printf("%d ",b); 6*6++ {
} default:
OUTPUT: 36 a = 4;
case 6:
a--;
case 5:
a = a+1; // a=5+1=6
case 1:
a = a-1; // a=6-1=5
}
printf("%d \n",a);
}
OUTPUT: 5

Mr. Abhinav Gupta 15

You might also like