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

Unit 2 Part 1 Full Notes

Uploaded by

Anak Yadav
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 views18 pages

Unit 2 Part 1 Full Notes

Uploaded by

Anak Yadav
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

Unit-2

Arithmetic Expressions and Precedence:

 Operators and Expression Using Numeric


 Relational Operators
 Mixed Operands
 Type Conversion
 Logical Operators
 Bit Operations
 Assignment Operator
 Operator precedence & Associativity

Conditional Branching:

 Applying if & Switch Statements


 Nesting if & Else
 Switch
Operator
An operator is a special symbol that tells the compiler to perform a specific operation on
one or more operands.

Example: a + b (here + is an operator and a & b are operands)

Classification of Operator on the basis of number of operands:

1. When an operator operates on one operand, is called unary operator.


2. When an operator operates on two operands, is called binary operator.
3. When an operator operates on three operands, is called ternary Operator.

Types of Operator:

S. No. Types Operator


1. Arithmetic * / %
+ -
2. Relational < <= > >=
== !=
3. Logical !
&&
||
4. Bitwise ~
<< >>
&
^
|
5. Assignment = += -+ *= /= %=
&= ^= |+ <<= >>=
6. Increment / ++ --
Decrement
7. Ternary ?:
8. Special sizeof & *
1. Arithmetic Operator:

The arithmetic operators are used to perform arithmetic/mathematical operations on


operands.

Operator Name Precedence Associativity


+ Addition 2 LR
- Subtraction 2 LR
* Multiplication 2 LR
/ Division 1 LR
% Modulus / Modulo / Remainder 1 LR

Modulus Operator:

 Both Operands must be integers


 Second Operand must be nonzero.

Division Operator:

 Second Operand must be nonzero.


 If both operands are integers then their result will be truncated quotient (Decimal
part dropped)
 If at least one operand is floating pint number then their result will be a floating pint
quotient.

Example:

#include <stdio.h>

void main() {
int a = 10, b = 3;

printf("Addition: %d\n", a + b); // 13


printf("Subtraction: %d\n", a - b); // 7
printf("Multiplication: %d\n", a * b); // 30
printf("Division: %d\n", a / b); // 3 (integer division)
printf("Modulus: %d\n", a % b); // Remainder 1
}
2. Relational Operators:

The relational operators are used to compare two values.

That means the relational operators are used to check the relationship between two values.

Operator Name Precedence Associativity


< Less than 1 LR
<= Less than or equal to 1 LR
> Greater than 1 LR
>= Greater than or equal to 1 LR
== Equal to 2 LR
!= Not equal to 2 LR

These six operators are used to form logical expressions, which represent conditions that
are either true or false. The resulting expressions will be of type integer, since true is
represented by the integer value 1 and false is represented by the value 0.

Note: Any nonzero value, not just 1, is interpreted as true.

Example:

#include <stdio.h>

void main() {
int a = 10, b = 20;

printf("a == b : %d\n", a == b); // 0 (false)


printf("a != b : %d\n", a != b); // 1 (true)
printf("a > b : %d\n", a > b); // 0 (false)
printf("a < b : %d\n", a < b); // 1 (true)
printf("a >= b : %d\n", a >= b); // 0 (false)
printf("a <= b : %d\n", a <= b); // 1 (true)
}
3. Logical Operators or Connectives:

The Logical operators are used to combine or invert conditions.

The logical operators && and || are used to combine multiple conditions into one condition
that are either true or false.

Types of Logical Operators:

 AND (&&): Returns true if both conditions are true.

 OR (||): Returns true if at least one condition is true.

 NOT (!): Inverts the value of the condition.

Truth Table:

a b a && b a || b !a
0 0 0 0 1
0 1 0 1 1
1 0 0 1 0
1 1 1 1 0

Operator Name Precedence Associativity


&& Logical AND 2 LR
|| Logical OR 3 LR
! Logical NOT, Logical Negation 1 RL

Example:

#include <stdio.h>

void main() {
int a = 10, b = 5;

printf("%d\n", a > 5 && b < 10); // 1


printf("%d\n", a < 5 || b < 10); // 1
printf("%d\n", !(a < 5)); // 1
}
4. Bitwise Operators:

Bitwise operators operate on individual bits of integer data types.

First converts numbers into binary, performs the operation bit by bit, and then converts the
result back to decimal.

a) A bitwise and expression will return a 1 if both bits have a value of 1 (i.e., if both bits are
true). Otherwise, it will return a value of 0.
b) A bitwise or expression will return a 1 if one or more of the bits have a value of 1 (one or
both bits are true). Otherwise, it will return a value of 0.
c) A bitwise exclusive or expression will return a 1 if one of the bits has a value of 1 and
the other has a value of 0 (one bit is true, the other false). Otherwise, it will return a
value of 0.
d) The left shift operator causes all of the bits in the first operand to be shifted to the left
by the number of positions indicated by the second operand. The leftmost bits (i.e., the
overflow bits) in the original bit pattern will be lost. The rightmost bit positions that
become vacant will be filled with 0s.
e) The right shift operator causes all of the bits in the first operand to be shifted to the
right by the number of positions indicated by the second operand. The rightmost bits
(i.e., the underflow bits) in the original bit pattern will be lost. If the bit pattern being
shifted represents an unsigned integer, then the leftmost bit positions that become
vacant will be filled with OS.
Hence, the behavior of the right shift operator is similar to that of the left shift operator
when the first operand is an unsigned integer.
f) The one’s complement operator is a unary operator that causes the bits of its operand
to be inverted (i.e., reversed), so that 1s become OS and OS become 1s.

Operator Name Precedence Associativity


& Bitwise AND 3 LR
| Bitwise OR 5 LR
^ Bitwise XOR (Exclusive OR) 4 LR
<< Left Shift 2 LR
>> Right Shift 2 LR
~ One’s Complement 1 RL

Truth Table:

a b a & b a | b a ^ b ~a
0 0 0 0 0 0
0 1 0 1 1 0
1 0 0 1 1 1
1 1 1 1 0 1
Example:

#include <stdio.h>

void main()
{

int a = 12, b = 10, n = 2; // a = 0000 1100, b = 0000 1010


printf("a & b = %d\n", a & b); // a & b = 0000 1000 = 8
printf("a | b = %d\n", a | b); // a | b = 0000 1110 = 14
printf("a ^ b = %d\n", a ^ b); // a ^ b = 0000 0110 = 6
printf("a << n = %d\n", a << n); // a << n = a * 2^n
printf("a >> n = %d\n", a >> n ); // a >> n = a / 2^n
printf("~a = %d\n", ~a); // ~a = -(a+1)
}
5. Assignment operators

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

Assignment operator is '='.

Left hand side must be a variable; Right hand side may be a variable/constant or expression
that evaluates to single value.

'C' has a set of 'shorthand' assignment operators of the form: v op= exp;

The operator op= is known as the shorthand assignment operator.

The assignment statement v op= exp; is equivalent to v = v op (exp);

Operator Example Precedence Associativity


= a=5 1 RL
+= a +=b  a = a + b 1 RL
-+ a -=b  a = a - b 1 RL
*= a *=b  a = a * b 1 RL
/= a /=b  a = a / b 1 RL
%= a %=b  a = a % b 1 RL
&= a &=b  a = a & b 1 RL
|= a |=b  a = a | b 1 RL
^= a ^=b  a = a ^ b 1 RL
<<= a <<=b  a = a << b 1 RL
>>= a >>=b  a = a >> b 1 RL

Multiple assignments of the form:

a = b = c = d = … = expression;

int a = 3.9; // 3

int a = ‘A’; // 65

int a = ‘A’ + 5; // 70
Example:

#include<stdio.h>

void main()
{
int a, b, c;
a = b = c = 10; // Multiple assignment
printf("a = %d, b = %d, c = %d\n", a, b, c);

int x = 3.142;
printf("x = %d\n", x); // Implicit type conversion

int y = 'A'; // Character to integer conversion


printf("y = %d\n", y); // Print ASCII value of 'A'
printf("y = %c\n", y); // Print character representation of y
}

Output:
6. Increment and Decrement Operators:

Increment (++) and decrement (--) operators in C are used to increase or decrease the value
of a variable by 1.

They can be used in two forms: prefix (before the variable) and postfix (after the variable).

In prefix, the operation is performed before the value is used, while in postfix, the original
value is used first, and then the operation is performed.

Operator Name Explanation


b = ++a Pre increment First adds 1 to a and then the result is
assigned value of a to b
b = a++ Post increment First assigns the value of a to b and
then add 1 to a
b = --a Pre decrement First subtracts 1 from a and then the
result is assigned value of a to b
b = a-- Post First assigns the value of a to b and
decrement then subtract 1 from a

Example:

#include <stdio.h>

void main()
{
int a = 5;
printf("%d\n", ++a); // 6
printf("%d\n", a++); // 6, but a becomes 7 after this line

// 6, because a was 7 and now it is decremented before printing


printf("%d\n", --a);

printf("%d\n", a--); // 6, but a becomes 5 after this line


}
7. Conditional or Ternary operator

Simple conditional operations can be carried out with the conditional operator (? :).

An expression that makes use of the conditional operator is called a conditional expression.
Such an expression can be written in place of the more traditional if -else statement

Syntax:

condition ? expression1 : expression2

If condition is true then expression1 gets executed otherwise expression2.

The conditional operator has its own precedence.

Associativity: R  L

min = (2 < 4) ? 2 : 4 // 2

Example: Program to find the maximum of two numbers using ternary operator

#include <stdio.h>

void main() {
int a = 10, b = 20;
int max;

// Using the ternary operator to find the maximum of a and b


max = (a > b) ? a : b;

printf("Maximum = %d", max);

}
8. Special Operators:

These operators are used to perform some specific tasks.

Operator Name Explanation


Sizeof sizeof It returns the variable or data type's size (in bytes).
& Address or Returns the memory address of a variable.
address-of or
reference
* Dereference Pointer to a variable. Accesses the value at the address specified by
or pointer or a pointer.
indirection
, Comma It can act as both operator and separator.
The comma operator analyses multiple expressions from left to
right and returns the last one. The parenthesis is necessary.
(int) Cast Convert the value from one data type to another explicitly.
85.99;
. and -> Dot and Arrow Access structure members using . for direct objects and -> for
operators object pointers.
() function call
symbol
[] array subscript
operator

Example of Comma operator:

value = (x=10, y=5, x+y); // value = 15

value = x, y, x+y; // value = x


Operator Precedence & Associativity
Operator Precedence

Operator precedence determines the priority of operators in an expression.

The operator with higher precedence is evaluated first.

Example 1

int x = 5 + 3 * 2;

Step 1: 3 * 2 = 6 (because * has higher precedence than +)


Step 2: 5 + 6 = 11

Example 2

int x = 20 / 5 + 3;

Step 1: 20 / 5 = 4
Step 2: 4 + 3 = 7

Associativity:

When two operators of the same precedence appear in an expression, associativity


determines the order of evaluation (left to right or right to left).

Example 1 (Left to Right Associativity)

int x = 10 - 5 - 2;

Subtraction has Left to Right associativity.

Step 1: 10 - 5 = 5
Step 2: 5 - 2 = 3

Example 2 (Right to Left Associativity)

Assignment operator has Right to Left associativity.

int a, b, c;
a = b = c = 10;

Step 1: c = 10
Step 2: b = 10
Step 3: a = 10
Note: Precedence of operators decreases from top to bottom in the given table.
Note: The precedence groups are listed from highest to lowest. Some C compilers also
include a unary plus (+) operator, to complement the unary minus (-) operator. However, a
unary plus expression is equivalent to the value of its operand; i.e., +v has the same value as
v.
Mixed Operands:
When an expression contains operands of different data types, it is called a mixed operand
expression.

Example:

int a = 10;
float b = 3.5;
float c = a + b; // This is a mixed expression (because a is int & b is float)

What Happens Internally?

C follows Type Promotion Rule: Lower data type is converted to higher data type
automatically.

Hierarchy: char  int  float  double

Example 1

int a = 5;
float b = 2.0;
float c = a + b; // 5.0 + 2.0

Step:

 a is converted to float (5.0)


 Result = 7.0

Example 2

int a = 5, b = 2;
float c = a / b;

Output = 2.0 (does not gives 2.5)

Why?

 Both operands are int


 Integer division occurs first → 5/2 = 2
 Then 2 converted to float → 2.0

Correct way:

float c = (float)a / b; // 2.5


Type Conversion
The process of converting one data type into another is called Type Conversion.

It can be done automatically by the compiler or manually by the programmer.

When Does Type Conversion Occur?

 When constants and variables of different types are mixed in an expression.

 When a value is assigned to a variable of a different data type.

 During arithmetic operations involving mixed operands.

Important Rules in Expressions

Rule 1: When operands of different types are used in an expression, they are converted to
the same higher data type before evaluation.

Rule 2: The type of data on the right side of assignment is automatically converted to the
type of the variable on the left side.

Conversion of small data type to big is called broadening / widening.

Conversion of big data type to small is called narrowing.

(small and big in terms of number of bytes)


Two types:

1. Implicit Type Conversion


2. Explicit Type Conversion (Type Casting)

1. Implicit Type Conversion

Done automatically by the compiler without programmer intervention

Example:

float b = 10; // b = 10.0 (int to float)

int x = 3.9; // x = 3 (float to int)

int a = 'A'; // a = 65 (ASCII value) (char to int)

float c = 5 + 2.0; // Mixed Expression  5.0 + 2.0 = 7.0

2. Explicit Type Conversion (Type Casting)

Explicit type conversion is when the programmer manually converts a variable from one
data type to another.

It is done using a type cast operator (type) before the value.

Used to force conversion when automatic (implicit) conversion doesn’t happen or isn’t
desired

Can convert larger types to smaller types or smaller types to larger types, but converting to
smaller types may cause data loss or truncation.

Syntax:

(type) expression

Example:

float result = (float) 5 / 2;

Output = 2.5

Without casting:

5/2=2

You might also like