C Programming Notes
C Programming Notes
INTRODUCTION TO
C PROGRAMMING
SUBJECT CODE: 1BPLC205E/105E
MODULE-2
Name:
USN:
College:
AZ Documents
Your Engineering Study Partner
[Link]
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
INTRODUCTION TO C PROGRAMMING
1BPLC205E/105E
MODULE-02
Operators: Introduction to Operators, Arithmetic Operators, Relational Operators,
Logical Operators, Assignment Operators, Increment and Decrement Operators,
Conditional Operators, Precedence of Arithmetic Operators.
Decision Making, Branching, Looping: Introduction, Decision Making with IF
Statement, Simple IF Statement, The IF..ELSE Statement, Nesting of IF..ELSE
Statements, The ELSE IF Ladder, The Switch Statement, The ?: Operator, The GOTO
Statement, WHILE, DO, FOR, Jumps in LOOPS.
Textbook: Chapter 4.1 to 4.7, 4.12, Chapter 6.1 to 6.9, Chapter 7.1 to 7.5
OPERATORS IN C
C programming language provides a rich set of built-in operators that help programmers perform
various operations on data.
An operator is a symbol that instructs the computer to perform a specific operation such as:
• arithmetic calculation
• comparison
• logical decision
• bit manipulation
Example
a+b
Here:
• + → operator
• a and b → operands
Operators are usually used in expressions.
Expression
An expression is a combination of variables, constants, and operators that produces a single value.
[Link] 1
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
Example
10 + 15
Value of expression = 25
Types of Operators in C
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
6. Conditional operator
7. Bitwise operators
8. Special operators
1. Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (remainder)
Example
a+b
a-b
a*b
[Link] 2
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
a/b
a%b
Integer Arithmetic
Example:
int a = 14;
int b = 4;
Results:
a + b = 18
a - b = 10
a * b = 56
a/b=3
a%b=2
Note:
Real Arithmetic
If operands are floating point numbers, the operation is called real arithmetic.
Example:
[Link] 3
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
Example:
15 / 10 = 1
15 / 10.0 = 1.5
#include <stdio.h>
int main()
{
int a = 14, b = 4;
return 0;
}
2. Relational Operators
• 1 (true)
• 0 (false)
Operator Meaning
[Link] 4
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
Operator Meaning
== Equal to
!= Not equal
Example
10 < 20 → TRUE
20 < 10 → FALSE
Example Program
#include <stdio.h>
int main()
{
int a = 10, b = 20;
return 0;
}
3. Logical Operators
Logical operators are used when multiple conditions must be tested together.
Operator Meaning
! Logical NOT
[Link] 5
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
Example
a > b && x == 10
Logical OR (||)
Example
number < 0 || number > 100
!(a > b)
return 0;
}
4. Assignment Operators
Basic Assignment
[Link] 6
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
Example
a = 10
Operator Equivalent
+= a=a+b
-= a=a-b
*= a=a*b
/= a=a/b
%= a=a%b
Example
x += y
means
x=x+y
#include <stdio.h>
int main()
{
int x = 5;
x += 3;
printf("Value of x = %d\n", x);
return 0;
}
++
--
[Link] 7
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
Example
i++
Example
i--
Prefix Increment
++i
Example
i=5
y = ++i
Result:
i=6
y=6
Postfix Increment
i++
Example
i=5
y = i++
Result:
[Link] 8
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
y=5
i=6
Program Example
#include <stdio.h>
int main()
{
int a = 5;
printf("%d\n", ++a);
printf("%d\n", a++);
return 0;
}
6. Conditional Operator
?:
It is also called ternary operator.
Syntax
Example
x = (a > b) ? a : b;
Meaning:
If a > b → x = a
Else → x = b
Program Example
#include <stdio.h>
int main()
{
int a = 10, b = 15, max;
max = (a > b) ? a : b;
[Link] 9
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
return 0;
}
7. Bitwise Operators
Bitwise Operators
Operator Meaning
^ Bitwise XOR
~ Bitwise NOT
Example
5&3
Binary
5 = 101
3 = 011
Result = 001
Program Example
#include <stdio.h>
int main()
{
int a = 5, b = 3;
printf("a & b = %d\n", a & b);
printf("a | b = %d\n", a | b);
[Link] 10
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
return 0;
}
8. Special Operators
Comma Operator
Example
Result:
value = 15
sizeof Operator
The sizeof operator returns the size of a variable or data type in bytes.
Example
sizeof(int)
sizeof(float)
Program Example
#include <stdio.h>
int main()
{
printf("Size of int = %lu\n", sizeof(int));
printf("Size of float = %lu\n", sizeof(float));
return 0;
}
Pointer Operators
&
*
[Link] 11
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
Example
& → address operator
* → pointer operator
ARITHMETIC EXPRESSIONS IN C
Definition
• constants
• variables
• arithmetic operators
Example
a+b
x*y
a*b-c
(a + b) / c
C can handle very complex mathematical expressions involving multiple operators and variables.
However, unlike some other programming languages, C does not have an exponentiation operator.
For example:
x²
cannot be written as x^2 in C because ^ is a bitwise operator.
pow(x,2)
EVALUATION OF EXPRESSIONS
General form:
variable = expression;
[Link] 12
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
Process of evaluation
1. The expression on the right side is evaluated first.
Example
x = a * b - c;
Evaluation steps:
1. Multiply a and b
2. Subtract c
3. Assign result to x
Example Expressions
x = a * b - c;
y = b / c * a;
z = a - b / c + d;
Example:
a = 10;
b = 5;
c = 2;
When an expression contains multiple operators, the order in which they are executed depends on
operator precedence.
Priority Operators
High */%
Low +-
This means multiplication and division are evaluated before addition and subtraction.
[Link] 13
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
Example Expression
x = a - b/3 + c*2 - 1
Let:
a=9
b = 12
c=3
Expression becomes:
x = 9 - 12/3 + 3*2 - 1
Evaluation Process
12/3 = 4
3*2 = 6
Expression becomes
x=9-4+6-1
9-4=5
5 + 6 = 11
11 - 1 = 10
Final result:
x = 10
ROLE OF PARENTHESES
Example:
9 - 12/(3+3)*(2-1)
Step 1
3+3 = 6
2-1 = 1
[Link] 14
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
Expression becomes
9 - 12/6 * 1
Step 2
12/6 = 2
Step 3
9 - 2*1
Step 4
9-2=7
Final result:
NESTED PARENTHESES
Parentheses can also be nested.
Example:
9 - (12/(3+3) * 2) - 1
3+3 = 6
12/6 = 2
2*2 = 4
9-4 = 5
5-1 = 4
Final result:
[Link] 15
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
COMPUTATIONAL PROBLEMS
Example:
a = 1.0/3.0
b = a * 3.0
Mathematically:
(1/3) × 3 = 1
0.999999
2 Division by Zero
Example:
if(b != 0)
result = a/b;
[Link] 16
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
int x = 40000
on a 16-bit system.
Underflow
Example:
int + float
To evaluate such expressions, C converts data types automatically.
Example:
int a = 10;
float b = 2.5;
c = a + b;
Here
a → converted to float
Conversion Hierarchy
char
↓
[Link] 17
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
int
↓
long
↓
float
↓
double
↓
long double
Example
After expression evaluation, the result is converted to the type of the variable on the left side.
Example:
int x;
x = 10.75;
Result:
x = 10
Example
Here
[Link] 18
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
Example Program
#include<stdio.h>
int main()
{
int a = 5, b = 2;
float result;
result = (float)a / b;
return 0;
}
Output
2.500000
x = (int)(y + 0.5);
If
y = 27.6
then
After casting
x = 28
[Link] 19
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
2. Associativity
Operator Precedence
Example
x = 10 + 15 * 2
Multiplication has higher precedence.
So
15 * 2 = 30
10 + 30 = 40
Associativity
Example
10 - 5 - 2
Step 1
10 + 15 = 25
Expression becomes
Step 2
Evaluate relational operators.
[Link] 20
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
x == 25
y < 10
Step 3
Evaluate logical operator &&.
MATHEMATICAL FUNCTIONS IN C
To use them:
#include <math.h>
Function Purpose
sin(x) Sine
cos(x) Cosine
exp(x) Exponential
Example Program
#include<stdio.h>
#include<math.h>
int main()
{
double x = 16;
return 0;
}
Output
[Link] 21
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
In programming, it is often necessary to make decisions and execute different statements depending on
certain conditions. Real-world problems frequently require programs to evaluate conditions and take
appropriate actions based on the results of those conditions.
For example:
• If a student scores more than 40 marks, the student passes the examination.
The C programming language supports several statements that allow programs to make decisions and
control the flow of execution. These statements evaluate conditions and decide which part of the
program should execute.
These statements are known as decision-making statements or control statements because they
control the execution flow of the program.
1. if statement
2. if...else statement
3. nested if...else statement
4. else-if ladder
5. switch statement
6. conditional operator (? :)
7. goto statement
Each of these statements allows the program to choose between alternative actions depending on the
result of a condition.
The if statement is the most fundamental and powerful decision-making statement in C programming.
[Link] 22
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
It allows the program to test a condition and execute a statement or group of statements only when the
condition is true.
General Syntax
if (test_expression)
{
statement-block;
}
Working of if Statement
2. If the condition is true (non-zero), the statements inside the block are executed.
3. If the condition is false (zero), the statements inside the block are skipped.
Thus, the program execution follows one of two possible paths depending on the result of the condition.
Simple if Statement
Syntax
if (condition)
{
statement-block;
}
statement-x;
Explanation
• If the condition is true, the statements inside the block are executed.
• If the condition is false, the block is skipped and the program continues with the next statement.
Example
Program Example
#include<stdio.h>
int main()
{
[Link] 23
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
int marks = 75
if(marks >= 40)
{
printf("Student Passed");
}
return 0;
}
Output:
Student Passed
Example:
De Morgan's Rule
!(A && B) = !A || !B
!(A || B) = !A && !B
Example:
[Link] 24
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
becomes
x > 0 && condition
The if...else statement extends the simple if statement by providing an alternative action when the
condition is false.
Syntax
if(condition)
{
true-block;
}
else
{
false-block;
}
Working
Example
if(number % 2 == 0)
printf("Even Number");
else
printf("Odd Number");
Program Example
#include<stdio.h>
int main()
{
int number;
printf("Enter a number: ");
scanf("%d",&number);
[Link] 25
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
if(number % 2 == 0)
printf("Number is Even");
else
printf("Number is Odd");
return 0;
}
When more than one decision must be made, nested if statements are used.
Syntax
if(condition1)
{
if(condition2)
{
statement1;
}
else
{
statement2;
}
}
else
{
statement3;
}
Working
Example
Suppose a bank offers a bonus to account holders:
[Link] 26
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
if(sex == 'F')
{
if(balance > 5000)
bonus = balance * 0.05;
}
else
{
bonus = balance * 0.02;
}
This occurs when it is unclear which if statement the else belongs to.
Example:
if(condition1)
if(condition2)
statement1;
else
statement2;
Rule:
else always matches with the closest if
Else-if Ladder
When multiple conditions must be tested sequentially, the else-if ladder is used.
Syntax
if(condition1)
statement1;
else if(condition2)
statement2;
[Link] 27
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
else if(condition3)
statement3;
else
default-statement;
Working
1. Condition1 is tested.
else
grade = 'F';
Program Example
#include<stdio.h>
int main()
{
int marks;
printf("Enter marks: ");
scanf("%d",&marks);
[Link] 28
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
printf("Grade B");
else if(marks >= 50)
printf("Grade C");
else
printf("Fail");
return 0;
}
Switch Statement
switch(expression)
{
case value1:
statements;
break;
case value2:
statements;
break;
default:
statements;
}
Working
1. The expression is evaluated.
[Link] 29
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
Example
switch(day)
{
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
default:
printf("Invalid Day");
}
Program Example
#include<stdio.h>
int main()
{
int day;
switch(day)
{
case 1: printf("Monday"); break;
case 2: printf("Tuesday"); break;
case 3: printf("Wednesday"); break;
case 4: printf("Thursday"); break;
case 5: printf("Friday"); break;
case 6: printf("Saturday"); break;
case 7: printf("Sunday"); break;
default: printf("Invalid Day");
}
return 0;
}
[Link] 30
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
Conditional Operator (? :)
Working
Example
max = (a > b) ? a : b;
Program Example
#include<stdio.h>
int main()
{
int a = 10, b = 20, max;
max = (a > b) ? a : b;
printf("Maximum = %d", max);
return 0;
}
[Link] 31
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
Goto Statement
The goto statement transfers control unconditionally to another part of the program.
Syntax
goto label;
label:
statement;
Example
#include<stdio.h>
int main()
{
int i = 1;
start:
printf("%d\n",i);
i++;
if(i <= 5)
goto start;
return 0;
}
Output
1
2
3
4
5
Forward Jump
Backward Jump
[Link] 32
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
Disadvantages of goto
• Makes program difficult to understand
In many programs it becomes necessary to execute a group of statements repeatedly. Such repeated
execution of a sequence of statements is known as looping or iteration.
In real-life programming problems, many operations must be repeated multiple times. For example:
Looping structures allow programmers to write concise programs that perform repetitive operations
without using multiple goto statements.
[Link] 33
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
Concept of Loop
A loop is a program structure that repeats a block of statements until a specified condition becomes
false.
2. Control statement
Control statement
This tests a condition to determine whether the loop should continue or terminate.
[Link] 34
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
Depending on the position of the test condition, loops are classified into two types:
1 Entry-Controlled Loop (Pre-test Loop)
In this type of loop, the condition is tested before entering the loop body.
If the condition is false, the loop body will not execute even once.
Examples:
• while loop
• for loop
In this type of loop, the condition is tested after executing the loop body.
Therefore the loop body executes at least once, regardless of the condition.
Example:
• do-while loop
A variable called counter variable is used to control the number of loop executions.
Example
[Link] 35
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
In a sentinel loop, repetition continues until a special value called sentinel value is encountered.
Example
while(number != -1)
Looping Statements in C
2. do-while loop
3. for loop
It is an entry-controlled loop, meaning the condition is tested before the loop body executes.
Syntax
while (test_condition)
{
statements;
}
When the condition becomes false, control transfers to the statement immediately following the loop.
[Link] 36
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
Here:
• Y is the sentinel value
[Link] 37
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
y = 1;
count = 1;
while(count <= n)
{
y = y * x;
count++;
}
In this loop, the body executes first and the condition is tested afterward.
Syntax
do
{
statements;
}
while(condition);
Characteristics
• Loop body executes at least once
[Link] 38
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
Example Program
#include<stdio.h
int main()
{
int number;
do
{
printf("Enter a number: ");
scanf("%d",&number);
This loop continues until the user enters zero or negative number.
Nested loops occur when one loop exists inside another loop.
[Link] 39
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
Syntax
Output:
0123456789
[Link] 40
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
Output
9876543210
Expressions in Initialization
Infinite Loop
for(;;)
[Link] 41
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
Example:
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
printf("*");
}
printf("\n");
}
1. If the loop condition must be checked before execution → use while or for.
[Link] 42
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
1. break
2. continue
3. goto
Break Statement
The break statement terminates the loop immediately.
Example
for(i=1;i<=10;i++)
{
if(i == 5)
break;
printf("%d", i);
}
Output
1234
Continue Statement
The continue statement skips the remaining part of the loop and starts the next iteration.
Example
for(i=1;i<=5;i++)
{
if(i == 3)
continue;
printf("%d", i);
}
Output
1245
[Link] 43
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
if(number < 0)
{
printf("Negative number");
continue;
}
Exit Function
A program can terminate completely using:
exit(0);
#include<stdlib.h>
Structured Programming
Structured programming is a disciplined programming technique that improves program readability and
maintainability.
It is based on three fundamental control structures:
• easier to understand
• easier to debug
• easier to maintain
[Link] 44
INTRODUCTION TO C PROGRAMMING MODULE-02 AZ Documents
Example
if(expression != 0)
can be written as
if(expression)
Similarly
if(expression == 0)
can be written as
if(!expression)
Example
Looping statements are essential for performing repetitive tasks efficiently in C programs. The three
looping constructs—while, do-while, and for—provide flexible ways to control repetition. Additional
control mechanisms such as break, continue, and exit allow programmers to alter the flow of loops
when necessary. Proper use of structured programming techniques ensures that programs remain
readable, maintainable, and logically organized.
[Link] 45
Thank You
We’re glad to be part of your engineering journey.
Keep exploring, keep innovating, and keep growing.
AZ Documents
Your Engineering Study Partner
[Link]