C Programming Unit - 2 Notes-1
C Programming Unit - 2 Notes-1
UNIT -II
Operators and Expressions
C Operator:
Def.: In C programming, an operator is a special symbol that tells the compiler to
perform a specific mathematical operation on single or two operands or variables or
values. Operators are used to perform operations like arithmetic, comparison, logical
operations, bit manipulation, etc.
1. Unary Operator :
Def.: A unary operator is an operator that acts on only one operand to perform an
operation. It is used to modify or evaluate a single value (operand).
1. Unary Minus
2. Logical NOT Operator
3. Bitwise Complementation
Unary Minus:
The unary minus operator is used to negate the value of its operand. It
changes the sign of a number — turning a positive number into negative and
vice versa.
Example:
#include <stdio.h>
int main()
{
int a = 10;
int b = -5;
return 0;
}
OUTPUT:
Original a = 10
Unary minus of a = -10
Original b = -5
Unary minus of b = 5
Binary Operators:
Definition:
Binary Operator
1. Arithmetic Operators:
+ Addition 2 10 + 5 15
- Subtraction 2 10 - 5 5
* Multiplication 1 10 * 5 50
#include <stdio.h>
OUTPUT
int main()
{
int a = 10, b = 3;
Addition: 13
Subtraction: 7
printf("Addition: %d\n", a + b);
printf("Subtraction: %d\n", a - b); Multiplication: 30
printf("Multiplication: %d\n", a * b); Division: 3
printf("Division: %d\n", a / b);
Modulus: 1
printf("Modulus: %d\n", a % b);
return 0;
}
Arithmetic Expression in C
Expression: An arithmetic expression in C is a combination of operands
(constants, variables) and arithmetic operators (+, -, *, /, %) that are
evaluated to produce a value.
It works just like mathematical expressions you write on paper, but with C’s
operator rules.
a % b
(power) pow(a, b)
(a + b) * c
(a + b) / (c + d)
√ sqrt(a*a + b*b)
Example 1:
Mathematical Expression: 10+20∗310 + 20 * 3
C Expression:
10 + 20 * 3
Step-by-step evaluation:
1. 20 * 3 = 60 (multiplication first)
2. 10 + 60 = 70
Result = 70
Example 2:
Mathematical Expression: (10+20)∗3(10 + 20) * 3
C Expression:
(10 + 20) * 3
Step-by-step evaluation:
1. (10 + 20) = 30 (parentheses first)
2. 30 * 3 = 90
Result = 90
C Expression:
(-b + sqrt(b*b - 4*a*c)) / (2*a)
If a = 1, b = -3, c = 2:
1. b*b = 9
2. 4*a*c = 8
3. b*b - 4*a*c = 1 So evaluation = applying
4. sqrt(1) = 1
5. -(-3) + 1 = 4
precedence + associativity +
6. 2*a = 2 parentheses rules step by step
7. 4 / 2 = 2
Result = 2 until you get a single value.
Increment Operator in C
Definition:
There are two types of increment operators in C:Pre-increment: "First add 1, and then use it."
Post-increment: "First use it, and then add 1."
1. Pre-increment (++x )
2. Post-increment (x++)
1. Pre-increment (++x)
Example:
#include <stdio.h>
int main()
{
int a = 5;
int b;
return 0; OUTPUT
}
Explanation: a=6
b=6
a = 5
++a → now a = 6
b = 6
2. Post-increment (x++)
Example:
#include <stdio.h>
int main()
{
int a = 5;
int b;
return 0; OUTPUT
}
Explanation: a=6
b=5
a = 5
b = a→b = 5
After assignment, a++ makes a = 6
Decrement Operator in C
Definition:
1. Pre-decrement (--x)
Example:
#include <stdio.h>
int main()
{
int a = 5;
int b;
return 0; OUTPUT
}
Explanation:
a=4
a = 5
b=4
--a → now a = 4
b = 4
2. Post-decrement (x--)
Example:
#include <stdio.h>
int main()
{
int a = 5;
int b;
return 0;
} OUTPUT
Explanation:
a=4
a = 5 b=5
b = a→b = 5
After assignment, a-- makes a = 4
#include <stdio.h>
int main() OUTPUT
{
int a = 5, b = 7, c;
c = 12
c = a++ + ++b - --b + a--;
return 0;
}
Step-by-step Evaluation
Initial values:
a = 5, b = 7
Expression:
c = a++ + ++b - --b + a--;
Relational Operators in C
Definition
They are commonly used in decision-making statements like if, while, for.
Note:
<, >, <=, >= have higher precedence than == and !=.
Arithmetic operators (+ - * / % ) are evaluated before relational operators.
Example Program
#include <stdio.h>
int main()
{
int a = 10, b = 20;
OUTPUT
printf("a = %d, b = %d\n", a, b);
printf("a < b : %d\n", a < b); a = 10, b = 20
printf("a > b : %d\n", a > b);
printf("a <= b : %d\n", a <= b);
a < b : 1
printf("a >= b : %d\n", a >= b); a > b : 0
printf("a == b : %d\n", a == b); a <= b : 1
printf("a != b : %d\n", a != b); a >= b : 0
a == b : 0
return 0;
}
a != b : 1
Logical Operators in C
Definition
&& Logical AND (true if both operands are true) 2 Left to Right
Got it � Let’s rewrite the truth tables clearly showing that 0 = False and 1 = True.
2. Logical OR (||)
Example Program:
#include <stdio.h>
int main() OUTPUT
{
int a, b, c;
a = 4; a = 1, b = 1, c = 1
b = 3;
c = a && b;
b = a || b || c;
a = a && b || c;
return 0;
}
Bitwise Operators in C
Definition
| Pipeline Bitwise OR
0 0 0 0 0 0 0 0 0
0 1 0 0 1 1 0 1 1
1 0 1
1 0 0 1 0 1
1 1 0
1 1 1 1 1 1
Example in C
#include <stdio.h>
int main()
OUTPUT
{
int a = 5, b = 3;
a = 5, b = 3
printf("a = %d, b = %d\n", a, b); a & b = 1
printf("a & b = %d\n", a & b); a | b = 7
printf("a | b = %d\n", a | b); a ^ b = 6
printf("a ^ b = %d\n", a ^ b);
return 0;
}
Shift Operators in C
Definition
Shift operators are used to move the bits of a number left or right by a
given number of positions.
Shifts bits to the left, filling with 0 on the right. Each left
<< Left Shift
shift multiplies the number by 2.
The left shift(<<) is a binary operator that takes two numbers, left shifts
the bits of the first operand, and the second operand decides the number
of places to shift. In other words, left-shifting an integer "a" with an integer
"b" denoted as '(a<<b)' is equivalent to multiplying a with 2^b (2 raised to
power b).
Syntax
a << b;
where,
a is the integer value to be shifted.
b specifies how many positions to shift the bits.
But if the size of the data type of a is only 5 bits, then the first bit will
be discarded we will be left with a = 10, which is 01010 in binary. It is
shown in the below image.
Syntax
a >> b;
where,
a is the integer value to be shifted.
b specifies how many positions to shift the bits.
Example: Let's take a=21; which is 10101 in Binary Form. Now, if a is right shifted
by 1 i.e a = a >> 1 then a will become a=a/(2^1). Thus, a = a/(2^1) = 10 which can
be written as 1010.
return 0;
}
Conditional Operator in C
Definition
General form:
Working:
OUTPUT
Example 2: Even or Odd
#include <stdio.h>
int main()
{ Odd
int num = 7;
return 0;
}
Special Operators in C
Definition
Special operators in C are operators that do not fall under the usual categories
(arithmetic, relational, logical, etc.) but have special purposes like handling
memory, structures, or evaluation of expressions.
Example:
OUTPUT
#include <stdio.h>
int main()
{ Before swapping: a = 20, b = 30
int a, b, c, temp;
After swapping: a = 30, b = 20
b = (a = 20, a + 10);
2. sizeof() Operator
That means both int and float take 4 bytes (32 bits) of memory.
Note: On some systems, int may vary (2, 4, or even 8 bytes), but nowadays it’s
almost always 4 bytes.
Perfect � You want a well-structured table like the one in the image (with Precedence, Type,
Operators, and Associativity).
Here’s the complete C Operator Precedence & Associativity Table in that format:
Syntax
variable operator= expression;
This is equivalent to:
variable = variable operator expression;
Example Program
#include <stdio.h>
int main() OUTPUT
{
int x = 3, y = 4, z = 1;
x += y; x = 3, y = 4, z = 1
y -= x;
z *= x;
printf("x = %d, y = %d, z = %d\n", x, y, z);
return 0;
}
Definition
#include <stdio.h>
int main()
{
int a = 10;
float b = 2.5;
float result = a + b;
printf("Result = %f", result);
return 0;
}
Here:
Output:
Result = 12.500000
#include <stdio.h>
int main()
{
float pi = 3.14159;
int value;
Here:
Output:
Value = 3
Definition
Example Program
#include <stdio.h>
#include <math.h>
int main()
{
double a = 16.0, b = 2.0;
OUTPUT
Header File in C
Definition
Syntax:
#include <headerfile.h>
<stdio.h> Standard Input and Output functions ( printf, scanf, gets, puts)
<stdlib.h> Memory allocation, process control, conversions ( malloc, free, exit, atoi)
int main()
{
char str1[] = "Hello", str2[] = "World";
printf("Length of str1 = %lu\n", strlen(str1));
printf("Square root of 25 = %.2f\n", sqrt(25));
return 0;
}
Output:
Length of str1 = 5
Square root of 25 = 5.00
Preprocessor Directives in C
Definition
Example
#include <stdio.h> // File inclusion
#define PI 3.1416 // Macro definition
int main()
{
printf("Value of PI = %.2f", PI);
return 0;
}
Output:
Value of PI = 3.14
Control Structures: Decision making Statements - Simple if, if_else, nested if_else, else_if ladder,
Switch Case, goto, break & continue statements; Looping Statements-Entry controlled and exit
controlled statements, while, do-while, for loops, Nested loops.
Control Structures in C
Definition
1. Decision-Making Statements :
Definition
1. if Statement
Definition
Executes a block of code only if the condition is true.
The if statement is used for deciding between two paths based on a True or
False outcome. It is represented by the following flowchart −
Syntax FLOWCHART
if(condition)
{
// statements if condition is true
}
Example
#include <stdio.h>
int main()
{
int num = 10;
if(num > 0)
{
printf("Number is positive");
}
return 0;
}
Output:
Number is positive
2. if-else Statement
Definition
Executes one block if the condition is true, else executes another block.
The statements inside the body of the if block get executed if and only if the
given condition is true.
Syntax
if(condition)
{
// statements if true
}
else
{
// statements if false
}
Example
#include <stdio.h>
int main() FLOWCHART
{
int num = 7;
if(num % 2 == 0)
{
printf("Even number");
}
else
{
printf("Odd number");
}
return 0;
}
Output:
Odd number
3. Nested if Statement
Definition
Syntax FLOWCHART
if(condition1)
{
if(condition2)
{
// statements if both true
}
}
Example #1
#include <stdio.h>
int main()
{
int num = 25;
if(num > 0)
{
if(num % 5 == 0)
{
printf("Positive and divisible by 5");
}
}
return 0;
}
Output:
Positive and divisible by 5
#include <stdio.h>
int main()
{
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if(a > b)
{ OUTPUT
if(a > c)
{
printf("Largest = %d", a);
}
else Enter three numbers: 12 45 30
{
printf("Largest = %d", c); Largest = 45
}
}
else
{
if(b > c)
{
printf("Largest = %d", b);
}
else
{
printf("Largest = %d", c);
}
}
return 0;
}
4. if-else-if Ladder
Definition
Syntax
if(condition1) FLOWCHART
{
// statements
}
else if(condition2)
{
// statements
}
else if(condition3)
{
// statements
}
else
{
// default statements
}
Example
#include <stdio.h>
int main()
{
int marks = 72;
if(marks >= 90)
{
printf("Grade A");
}
else if(marks >= 75)
{
printf("Grade B");
}
else if(marks >= 50)
{
printf("Grade C"); OUTPUT
}
else
{
printf("Fail"); Grade C
}
return 0;
}
5. switch Statement
Definition
Syntax FLOWCHART
switch(expression)
{
case value1:
// statements
break;
case value2:
// statements
break;
...
default:
// statements if no case matches
}
Example
#include <stdio.h>
int main()
{
int day = 3;
switch(day)
{
case 1: printf("Monday"); break;
case 2: printf("Tuesday"); break;
case 3: printf("Wednesday"); break;
case 4: printf("Thursday"); break;
default: printf("Invalid day");
OUTPUT
}
return 0;
} Wednesday
Example #2:
#include <stdio.h>
int main()
{
int marks, grade;
printf("Enter percentage of marks: ");
scanf("%d", &marks);
grade = marks / 10;
switch(grade)
{
case 10: // For 100
case 9: // 90–99
printf("Excellent! Grade A");
break;
case 8: // 80–89
printf("Very Good! Grade B");
break;
case 7: // 70–79
printf("Good! Grade C");
break;
case 6: // 60–69
OUTPUT
printf("Average! Grade D"); Enter percentage of marks:89
break; Very Good! Grade B
case 5: // 50–59
printf("Pass! Grade E");
break;
default: // Below 50
printf("Fail! Better luck next time.");
}
return 0;
}
Looping Statements in c:
Def: A loop is used when you want to execute a block of code repeatedly until a
certain condition is met.
1. For Loop
Definition:
A for loop is an entry-controlled loop that allows you to execute a block of code
repeatedly with initialization, condition, and increment/decrement in a single
line.
Syntax:
int main()
{ OUTPUT
int i;
for(i = 1; i <= 20; i++)
{ 2 4 6 8 10 12 14 16 18 20
if(i%2==1)
{
printf("%d ", i);
}
}
return 0;
}
int main()
{ OUTPUT
int i, sum = 0;
for(i = 1; i <= 10; i++)
{
sum = sum + i;
Sum = 55
}
printf("Sum = %d", sum);
return 0;
}
int main() 10 9 8 7 6 5 4 3 2 1
{
int i;
for(i = 10; i >= 1; i--)
{
printf("%d ", i);
}
return 0;
}
A nested loop means having one loop inside another loop. The inner loop runs
completely for every single iteration of the outer loop.
Syntax:
for(initialization; condition; increment/decrement)
{
for(initialization; condition; increment/decrement)
{
// Inner loop body
}
// Outer loop body
}
While Loop:
while (condition)
{
// code to be executed
}
Example
Let’s write a simple program that prints numbers from 1 to 5 using a while loop:
#include <stdio.h>
int main()
{
int i = 1;
while (i <= 5)
{ OUTPUT
printf("%d\n", i);
i++; 1
} 2
3
return 0; 4
} 5
Lab #6) Program to read a number, find the sum of the digits, reverse the number
and check it for palindrome.
Program:
#include <stdio.h>
void main()
{
int num, temp, digit, sum = 0, reverse = 0;
temp = num;
// Display results
printf("Sum of digits = %d\n", sum);
printf("Reversed number = %d\n", reverse);
// Check palindrome
if (temp == reverse)
{
printf("The number is a palindrome.\n");
}
else
{
printf("The number is not a palindrome.\n");
}
}
OUTPUT #1 OUTPUT #2
Do - while Loop:
A do...while loop is similar to a while loop, but the difference is that it executes the
block of code at least once before checking the condition. After the first
execution, it keeps repeating as long as the condition is true.
When you want the loop body to run at least once, no matter what.
Useful in menu-driven programs, input validation, or cases where the action
must happen first and then the condition is checked.
do Intialization
Syntax {OR OR do
// code to be executed {
// code to be executed
} while (condition); Increment/decrement
} while (condition);
Example
#include <stdio.h>
int main()
{
int i = 1;
OUTPUT
do
{ 1
printf("%d \n", i); 2
i++; 3
} while (i <= 5); 4
5
return 0;
}
Condition is checked first, then statements Statements are executed at least once,
are executed. then the condition is checked.
Statements may execute zero times if the Statements execute at least once,
condition is false initially. regardless of the condition.
The variable used in the condition must be The variable may be initialized before or
initialized before the loop starts. inside the loop.
Syntax: Syntax:
while (condition) do
{ {
statement(s); statement(s);
} } while (condition);
Definition
1. break
2. continue
3. goto
4. return
1. break Statement
Syntax:
break;
Example:
#include <stdio.h>
int main()
{
for (int i = 1; i <= 5; i++)
{
if (i == 3)
{
break; // exit loop when i == 3
}
OUTPUT
printf("%d\n", i);
}
1
return 0;
} 2
Definition: Skips the current iteration of a loop and jumps to the next iteration.
Syntax:
continue;
Example:
#include <stdio.h>
int main()
{
for (int i = 1; i <= 5; i++)
{
if (i == 3)
{
continue; // skip when i == 3
}
printf("%d\n", i);
}
return 0; OUTPUT
}
1
3. goto Statement
Definition: Transfers control unconditionally to a labeled statement in the same function.
Syntax:
Example:
#include <stdio.h>
int main()
{
int i = 1;
loop:
printf("%d\n", i);
i++;
if (i <= 3)
{
goto loop; // jump to label
OUTPUT
}
return 0; 1
} 2
4. return Statement
Definition: Ends the execution of a function and optionally returns a value to the
caller.
Syntax:
Example:
OUTPUT
#include <stdio.h>
int add(int a, int b)
{
return a + b; Result = 8
}
int main()
{
int result = add(5, 3);
printf("Result = %d\n", result);
return 0;
}