0% found this document useful (0 votes)
12 views

C Programming - Complete Slide Contents

The document outlines a syllabus for a C Programming course, detailing the structure and key topics such as problem solving, algorithms, flowcharting, coding, and debugging. It emphasizes the importance of understanding problems before coding and provides an overview of programming languages, particularly focusing on C as a middle-level language. Additional sections cover algorithm development, flowcharting techniques, and the compilation process.

Uploaded by

iamanuroadh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

C Programming - Complete Slide Contents

The document outlines a syllabus for a C Programming course, detailing the structure and key topics such as problem solving, algorithms, flowcharting, coding, and debugging. It emphasizes the importance of understanding problems before coding and provides an overview of programming languages, particularly focusing on C as a middle-level language. Additional sections cover algorithm development, flowcharting techniques, and the compilation process.

Uploaded by

iamanuroadh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 425

C Programming (CSC115)

Sulav Nepal
Email: [email protected]
Contact No.: 9849194892
Master’s in Computer Information System (MCIS) – Pokhara University
Bachelor of Science. Computer Science & Information Technology (B.Sc. CSIT) – Tribhuwan University
Microsoft Technology Associate (MTA): Windows Server Administration Fundamentals
Microsoft Certified Technology Specialist (MCTS): Windows Server 2008 R2, Server Virtualization
Microsoft Specialist (MS): Programming in HTML5 with JavaScript and CSS3
Microsoft Students Partner (MSP) 2012 for Nepal
P P
R R
E E
P P
A A
R R
E E
D D

B
Y not ENGLISH nor NEPALI B
Y

S S
U U
L
A Welcome to C Programming Language L
A
V V

N N
E E
P P
A A
L L

2 3/9/2025 6:46 AM
P P
R R
E
P
Syllabus E
P
A  UNIT 1: Problem Solving with Computer A
R R
E E
D
o Problem Analysis D

B B
Y Y
o Algorithms and Flowchart
S S
U U
L o Coding, Compilation, & Execution L
A A
V V
o History of C
N N
E E
P o Structure of C program P
A A
L L
o Debugging,Testing & Documentation
3 3/9/2025 6:46 AM
P P
R R
E E
P P
A A
R R

UNIT 1
E E
D D

B B
Y Y

S PROBLEM SOLVING WITH COMPUTER S


U U
L L
A A
V V

N N
E E
P P
A A
L L

4 3/9/2025 6:46 AM
P P
R
E Introduction R
E
P P
A
R
 Number of problems in our daily life. A
R
E E
D D
 Suppose we have to calculate Simple Interest.
B B
Y Y

S  Suppose we have to prepare a mark sheet. S


U U
L L
A A
V  A computer is a DUMB machine. V

N N
E E
P  A computer cannot do anything alone without software. i.e. P
A
L Program. A
L

5 3/9/2025 6:46 AM
P P
R
E Introduction R
E
P P
A A
R R
E
D
 A software is a set of programs written to solve a particular problem. E
D

B B
Y  Program is a set of instructions on the basis of which computer gives Y

S output/result. S
U U
L L
A A
V  If the instructions are not correct, the computer gives wrong result. V

N N
E E
P P
A A
L L

6 3/9/2025 6:46 AM
P P
R
E Never Ever Forget R
E
P P
A A
R R
E
D
 Just writing code is not sufficient to solve a problem. E
D

B B
Y  Program must be planned before coding in any computer language Y

S available. S
U U
L L
A A
V  There are many activities to be done before and after writing code. V

N N
E E
P P
A A
L L

7 3/9/2025 6:46 AM
P P
R
E Types of Programming Languages R
E
P P
A  Machine Level Language A
R R
E
 Language that a computer actually understands (1’s and 0’s)
E
D  Sequence of instructions written in the form of binary numbers D
 Executes fast as computer don’t need any translation
B B
Y Y
 Assembly Language
S
 Symbolic representation of machine code S
U U
L  Close to machine code but the computer cannot understand L
A  Must be translated into machine code by a separate program called an A
V assembler V

N N
E  High Level Language E
P P
A
 Similar to natural language resulting to ease in learning and writing
A
L  While execution: translated into assembly language then to machine language L
 Slow in execution but is efficient for developing programs
8 3/9/2025 6:46 AM
P P
R
E Overview to C Programming R
E
P P
A  We can assume C as middle level language A
R R
E E
D  This doesn’t mean C is less powerful or harder to use or less developed D

B B
Y  Instead C combines the advantages of high level language with the Y

S
functionalism of assembly language
S
U U
L
 Like high level, C provides block structures, stand-alone functions and L
A A
V small amount of data typing V

N N
E  Like assembly language, C allows manipulations of bits, bytes, pointers and
E
P it is mostly used in system programming P
A A
L L
 Combination of two aspects
9 3/9/2025 6:46 AM
P
R
Stages while solving a problem using P
R
E
P computer E
P
A  Problem Analysis A
R R
E E
D D
 Algorithm Development
B B
Y Y
 Flowcharting
S S
U U
L  Coding L
A A
V V

N  Compilation & Execution


N
E E
P P
A  Debugging & Testing A
L L

10  Documentation 3/9/2025 6:46 AM


P
R
Stages while solving a problem using P
R
E
P computer E
P
A A
R R
E E
D D

B B
Y Y

S S
U U
L L
A A
V V

N N
E E
P P
A A
L L

11 3/9/2025 6:46 AM
P P
R
E Problem Analysis R
E
P P
A
R
 Process of becoming familiar with the problem A
R
E E
D D
 We need to analyze and understand it well before solving
B B
Y Y

S  The user’s requirements cannot be fulfilled without clear S


U U
L understanding of his/her problem in depth L
A A
V V

N  Inadequate identification of problem may cause program less useful N


E
P
and insufficient E
P
A A
L L
 Example: Banking Solution, Hospital Medical Study
12 3/9/2025 6:46 AM
P P
R
E Algorithm Development R
E
P P
A  Step by step description of the method to solve a problem A
R R
E E
D D
 Effective procedure for solving a problem in finite number of steps
B B
Y Y

S
 Developing an algorithm is a step of program design
S
U U
L L
A  Example:An algorithm to find sum of two numbers: A
V V
 Step 1: Start
N  Step 2: Declare variables num1, num2, and sum N
E E
P  Step 3: Read values num1 and num2 P
A  Step 4:Add num1 and num2 and assign the result to sum A
L L
 Step 5: Display sum 𝑠𝑢𝑚 ← 𝑛𝑢𝑚1 + 𝑛𝑢𝑚2
13  Step 6: Stop 3/9/2025 6:46 AM
P P
R
E Algorithm Development R
E
P P
A  Three features of Algorithm: A
R R
E E
D  Sequence D
 Each step in the algorithm is executed in specified order. If not algorithm will fail.
B B
Y Y
 Decision
S  We have to make decision to do something
S
U  If the outcome of the decision is true, one thing is done otherwise other U
L If condition then process1 L
A OR A
V V
If condition then process1 Else process2
N N
E  Repetition E
P Repeat P
A A
L Fill water in kettle
L
Until Kettle is full
14 3/9/2025 6:46 AM
P P
R
E Flowcharting R
E
P P
A A
R  Graphical representation of an algorithm using standard symbols R
E E
D D

B  Includes a set of various standard shaped boxes that are


B
Y interconnected by flow lines Y

S S
U U
L  Flow lines have arrows (direction of flow) L
A A
V V

N  Activities are written within boxes in English N


E E
P P
A A
L  Communicates between programmers and business persons L

15 3/9/2025 6:46 AM
P P
R
E Flowcharting - Advantages R
E
P P
A  Communication – quickly provide logic, ideas, and descriptions of A
R R
E algorithms E
D D

B  Effective Analysis – clear overview of the entire problem B


Y Y

S  Proper Documentation – documents the steps followed in an algorithm and S


U U
L helps us understand its logic in future L
A A
V V
 Efficient Coding – more ease with comprehensive flowchart as a guide
N N
E E
P  Easy in Debugging & Program Maintenance – debugging and maintenance P
A A
L of operating program L

16 3/9/2025 6:46 AM
P P
R
E Flowchart Symbols R
E
P P
A A
R R
E E
D D

B B
Y Y

S S
U U
L L
A A
V V

N N
E E
P P
A A
L L

17 3/9/2025 6:46 AM
P P
R
E Flowchart – Things to Consider R
E
P P
A A
R R
E
D
 There should be start and stop to the flowchart E
D

B B
Y  Only one flow line should emerge from a process symbol Y

S S
U U
L  Only one flow line should enter a decision symbol, but two or three L
A A
V flow lines can leave the decision symbol V

N N
E E
P P
A A
L L

18 3/9/2025 6:46 AM
P P
R
E Example – Sum of Two Numbers R
E
P P
A
R
 Algorithm A
R
E  Step 1: Start E
D D

B B
Y  Step 2: Display “Enter two numbers”
Y

S S
U  Step 3: Read 𝐴 and 𝐵 U
L L
A A
V
 Step 4: 𝐶 = 𝐴 + 𝐵 V

N N
E E
P  Step 5: Display “𝐶 as sum of two numbers” P
A A
L L
 Step 6: Stop
19 3/9/2025 6:46 AM
P P
R
E Example – Sum of Two Numbers R
E
P P
A
R
 Flowchart A
R
E E
D D

B B
Y Y

S S
U U
L L
A A
V V

N N
E E
P P
A A
L L

20 3/9/2025 6:46 AM
P P
R
E Coding R
E
P P
A  The process of transforming the program logic design into computer A
R R
E language format E
D D

B  An act of transforming operations in each box of the flowchart in terms of B


Y the statement of the program Y

S S
U U
L  The code written using programming language is also known as source
L
A code A
V V

N  Coding isn’t the only task to be done to solve a problem using computer. N
E E
P P
A A
L Anyone can code.TRUST ME !! L

21 3/9/2025 6:46 AM
P P
R
E Compilation R
E
P P
A
R
 Process of changing high level language into machine level language A
R
E E
D D
 It is done by special software, called Compiler
B B
Y Y

S  The compilation process tests the program whether it contains syntax S


U U
L errors or not L
A A
V V

N  If syntax errors are present, compiler can not compile the code. N
E E
P P
A A
L L

22 3/9/2025 6:46 AM
P P
R
E Execution R
E
P P
A A
R R
E
D
 Once the compilation is completed then the program is linked with E
D
other object programs needed for execution, there by resulting in a
B B
Y binary program and then the program is loaded in the memory for Y
the purpose of execution and finally it is executed
S S
U U
L L
A  The program may ask user for inputs and generates outputs after A
V V
processing the inputs
N N
E E
P P
A A
L L

23 3/9/2025 6:46 AM
P P
R
E Debugging and Testing R
E
P P
A A
R R
E
D
 Debugging is the discovery and correction of programming errors. E
D

B B
Y  Some errors may remain in the program because the Y

S designer/programmer might have never thought about a particular S


U
L
case. U
L
A A
V V

N
 When error appears debugging is necessary.
N
E E
P P
A A
L L

24 3/9/2025 6:46 AM
P P
R
E Debugging and Testing R
E
P P
A
R
 Testing ensures that program performs correctly the required task A
R
E E
D D
 Verification ensures that program does what the programmer intends
B B
Y to do Y

S S
U U
L  Validation ensures that the program produces the correct results for a L
A
V
set of test data A
V

N N
E  Test data are supplied to the program and output is observed E
P P
A A
L L
 Expected output = Error free
25 3/9/2025 6:46 AM
P P
R
E Program Documentation R
E
P P
A A
R R
E
D
 Helps to those who use, maintain, and extend the program in future E
D

B B
Y  A program may be difficult to understand even to programmer who Y

S wrote the code after some days S


U U
L L
A A
V  Properly documented program is necessary which will be useful and V

N
efficient in debugging, testing, maintenance, and redesign process
N
E E
P P
A A
L L

26 3/9/2025 6:46 AM
P P
R
E Program Documentation - Types R
E
P P
A A
R R
E
D
 Programmer’s Documentation (Technical Documentation) E
D
 Maintain, redesign, and upgrade
B B
Y  Logic, DFD, ER Diagram, algorithm, & flowchart Y

S S
U U
L  User Documentation (User Manual) L
A
V
 Support to the user of the program A
V
 Instructions for installation of the program
N N
E E
P P
A A
L L

27 3/9/2025 6:46 AM
P P
R
E History of C Programming R
E
P P
A  C is a programming language which was born at “AT & T’s Bell Laboratory” A
R of USA in 1972 R
E E
D D
 C was written by Dennis Ritchie, that’s why he is also called as father of C
B B
Y programming language
Y

S S
U  C language was created for a specific purpose i.e. designing UNIX
U
L operating system (which is currently base of many UNIX based OS) L
A A
V V
 From the beginning, C was intended to be useful to allow busy
N programmers to get things done because C is such a powerful, dominant N
E and supple language E
P P
A A
L  Its use quickly spread beyond Bell Labs in the late 70’s L

28 3/9/2025 6:46 AM
P P
R
E Why use C? R
E
P P
A  Robust language A
R R
E E
D D
 Efficient and fast
B B
Y Y
 Highly portable
S S
U U
L  Structured language L
A A
V V

N  Extendibility
N
E E
P P
A  Middle level language A
L L

29  Rich system library 3/9/2025 6:46 AM


P P
R
E Basic Structure of C Program R
E
P P
A A
R R
E E
D D

B B
Y Y

S S
U U
L L
A A
V V

N N
E E
P P
A A
L L

30 3/9/2025 6:46 AM
P P
R
E Basic Structure of C Program R
E
P P
A
R
 Document Section A
R
E  Sets of comment line giving the name of program, the author, E
D D
algorithms, methods used, and other details
B B
Y Y
 Acts as a communication between members of the development team
S S
U U
L
A
 Acts as user manual L
A
V V

N  Example: /* This program adds two numbers */ N


E E
P P
A A
L L

31 3/9/2025 6:46 AM
P P
R
E Basic Structure of C Program R
E
P P
A
R
 Link Section A
R
E  Provides instructions to the compiler to link functions with program E
D D
from the system library
B B
Y Y
 Example: #include<stdio.h>
S S
U  Links input/output functions like printf() and scanf() with the program
U
L L
A A
V V

N N
E E
P P
A A
L L

32 3/9/2025 6:46 AM
P P
R
E Basic Structure of C Program R
E
P P
A
R
 Definition Section A
R
E  In this section all symbolic constants are defined E
D D

B B
Y  This section may be included or excluded while writing a C program
Y

S S
U  Example: U
L L
A #define PI 3.1416
A
V #define FORMULA 3*x*x*x+2*x*x V

N N
E E
P P
A A
L L

33 3/9/2025 6:46 AM
P P
R
E Basic Structure of C Program R
E
P P
A
R
 Global Declaration Section A
R
E  The variables which are used in more than one functions or blocks are E
D D
called global variables
B B
Y Y
 This section also declares all the user-defined functions
S S
U U
L
A
 This section may be included or excluded while writing a C program L
A
V V

N N
E E
P P
A A
L L

34 3/9/2025 6:46 AM
P P
R
E Basic Structure of C Program R
E
P P
A
R
 Main() Function Section A
R
E  Every C program starts with a main() function E
D D
 Declaration part and Executable part
B B
Y Y
 Declaration part declares all the variables used in the execution part
S S
U  int n1;
U
L  int n2=5; L
A A
V V
 Execution part has executable operations like
N N
E  n1=n1+1; E
P P
 n2=n1*5;
A A
L L

35 3/9/2025 6:46 AM
P P
R
E Basic Structure of C Program R
E
P P
A
R
 Subprogram Section A
R
E  This section contains all the user-defined functions that are called in E
D D
the main function.
B B
Y Y
 All the sections except the main function section may be absent when
S S
U they are not required U
L L
A A
V V

N N
E E
P P
A A
L L

36 3/9/2025 6:46 AM
P P
R
E First C Program R
E
P P
A
R
//First program in C A
R
E
D
#include<stdio.h> E
D

B
#include<conio.h>
B
Y void main() Y

S { S
U U
L printf("This is my first program in C"); L
A A
V getch(); V

N } N
E E
P P
A A
L L

37 3/9/2025 6:46 AM
P P
R R
E E
P P
A A
R R
E E
D D

B B
Y Y

S
END OF UNIT ONE
S
U U
L L
A A
V V

N N
E E
P P
A A
L L

38 3/9/2025 6:46 AM
P P
R R
E
P
Syllabus E
P
A  UNIT 2: Elements of C A
R R
E E
D
o C Standards C Character Set D

B B
Y Y
o C Tokens Escape Sequence
S S
U U
L o Delimiters Variables L
A A
V V
o Data Types Structure of a C Program
N N
E E
P o Executing a C Program Constants/Literals P
A A
L L
o Expressions, Statements and Comments
39 3/9/2025 6:46 AM
P P
R R
E E
P P
A A
R R

UNIT 2
E E
D D

B B
Y Y

S ELEMENTS OF C S
U U
L L
A A
V V

N N
E E
P P
A A
L L

40 3/9/2025 6:46 AM
P P
R
E First C Program R
E
P P
A A
R R
E
D
//First program in C E
D

B
#include<stdio.h>
B
Y void main() Y

S { S
U U
L printf(“This is my first program in C.”); L
A A
V printf(“Welcome to programming world!”); V

N } N
E E
P P
A A
L L

41 3/9/2025 6:46 AM
P P
R
E C Standards (ANSI C & C99) R
E
P P
A
R
 ANSI C, ISO C, and Standard C refer to the successive standards for A
R
E the C programming language published by the American National E
D D
Standards Institute (ANSI).
B B
Y Y

S  Historically, the names referred specifically to the original and best-


S
U supported version of the standard (known as C89 or C90). U
L L
A A
V V
 In March 2000,ANSI adopted the ISO/IEC 9899:1999 standard.
N N
E E
P P
A  This standard is commonly referred to as C99. A
L L

42 3/9/2025 6:46 AM
P P
R
E Character Set R
E
P P
A
R
 Set of characters that are used to form words, numbers and expression A
R
E in C is called C character set. E
D D

B B
Y  Characters in C are grouped into the following four categories: Y

S  Letters and alphabets (A…Z, a…z) S


U U
L L
A  Digits (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) A
V V

N
E
 Special Characters (, . ; : ? ‘ “ & ^ * - + < >) N
E
P P
A A
L  White spaces (Blank space, Horizontal tab, etc.) L

43 3/9/2025 6:46 AM
P P
R
E Keywords R
E
P P
A
R
 These are predefined words for a C programming language. A
R
E E
D D
 All keywords have fixed meaning and these meanings cannot be
B B
Y changed. Y

S auto double int struct S


U break else long switch U
L L
A case enum register typedef A
V char return union const V

N float short unsigned continue N


E void for default goto E
P P
A sizeof volatile do if A
L static while extern signed L

44 3/9/2025 6:46 AM
P P
R
E Identifiers R
E
P P
A
R
 Every word used in C program to identify the name of variables, A
R
E functions, arrays, pointers and symbolic constants are known as E
D D
identifiers.
B B
Y Y

S  Names given by user can consist of a sequence of letters and digits,


S
U with a letter as the first character. U
L L
A A
V V
 Example: myVariable, myName, heyYou, callThisNumber45,
N N
E add_this_number, etc. E
P P
A A
L
 There are certain rules to be followed while naming identifiers. L

45 (KEEP THIS IN MIND) 3/9/2025 6:46 AM


P P
R
E Rules for Naming Identifiers R
E
P P
A  It must be a combination of letters and digits and must begin with a A
R R
E letter. E
D D

B  Underscore is permitted between two digits and must begin with a B


Y Y
letter.
S S
U U
L  Only first 31 characters are significant. L
A A
V V

N  Keywords cannot be used. N


E E
P P
A
L
 It is case sensitive. i.e. uppercase and lowercase letters are not A
L
interchangeable.
46 3/9/2025 6:46 AM
P P
R
E Data Types R
E
P P
A
R
 10 is a whole number where as 10.5 is a fractional/rational A
R
E number. E
D D

B B
Y  Similarly in C, 10 is an integer number whereas 10.5 is a float Y

S number. S
U U
L L
A
V
 There are variety of data types available. A
V

N N
E  ANSI C supports three classes of data types: E
P P
A  Primary/Fundamental data types A
L L
 User-Defined data types
3/9/2025 6:46 AM
47  Derived data types
P P
R
E Primary/Fundamental Data Types R
E
P P
A
R
 Primary data types are categorized into five types: A
R
E E
D D
 Integer type (int)
B B
Y Y
 Floating point type (float)
S S
U U
L L
A  Double-precision floating point type (double) A
V V

N  Character type (char) N


E E
P P
A A
L  Void type (void)
L

48 3/9/2025 6:46 AM
P P
R
E Integer Type R
E
P P
A  Integers are whole numbers. A
R R
E E
D  Requires 16 bits of storage. i.e. 2 bytes for 16-bit compiler. D

B B
Y  Requires 32 bits of storage. i.e. 4 bytes for 32-bit compiler.
Y

S S
U  Three classes of integer:
U
L  Integer (int) L
A  Short integer (short int) A
V  Long integer (long int) V

N N
E  Both signed and unsigned forms. E
P P
A A
L  Defined as: L
int a;
49 3/9/2025 6:46 AM
int myValue=6;
P P
R
E Integer Type R
E
P P
A Signed Integer Unsigned Integer A
R R
E It represents both positive and negative integers It represents only positive integers E
D D
The data type qualifier is signed int or int. The data type qualifier is unsigned int or
B B
unsigned.
Y Y
Variables are defined as:
S signed int a; Variables are defined as:
S
U int b; unsigned int a; U
L unsigned b; L
A A
V By default all int are signed Unsigned int have to be declared explicitly V

N It ranges from −215 to +215 It ranges from 0 to +216 N


E E
i.e. −32768 to 32767 i.e. 0 to 65535
P P
A A
L Its conversion character is d Its conversion character is u L

50 3/9/2025 6:46 AM
P P
R
E Floating Point Type R
E
P P
A
R
 Floating point types are fractional numbers A
R
E E
D D
 In C, it is defined by float
B B
Y Y

S  Reserves 32 bits (i.e. 4 bytes) S


U U
L L
A A
V  Variables are defined as: V

N float a; N
E E
P float myValue=6.5; P
A A
L L

51 3/9/2025 6:46 AM
P P
R
E Double Precision Floating Point Type R
E
P P
A
R
 Used to represent the floating point numbers A
R
E E
D D
 Reserves 64 bits (i.e. 8 bytes)
B B
Y Y

S  In C, it is defined by double S
U U
L L
A A
V  Variables are defined as: V

N double a; N
E E
P double myValue=4244.546; P
A A
L L

52 3/9/2025 6:46 AM
P P
R
E Character Type R
E
P P
A
R
 A single character can be defined as a character type data A
R
E E
D D
 Stored in 8 bits (1 byte)
B B
Y Y

S  The qualifier signed or unsigned may be used with char S


U U
L L
A A
V  The unsigned char has values between 0 and 255 V

N N
E E
P  The signed char has values from -128 to 127 P
A A
L L

53 3/9/2025 6:46 AM
P P
R
E Character Type R
E
P P
A
R
 Each character is represented by an ASCII (American Standard Code A
R
E for Information Interchange) E
D D

B B
Y  Example: “A” is represented by 65, “B” is represented by 66, “a” is Y

S represented by 97,“z” is represented by 122. S


U U
L L
A
V
 With conversion character d, it will display ASCII value A
V

N N
E  With conversion character c, it will display character E
P P
A A
L L

54 3/9/2025 6:46 AM
P P
R
E ASCII Table R
E
P P
A A
R R
E E
D D

B B
Y Y

S S
U U
L L
A A
V V

N N
E E
P P
A A
L L

55 3/9/2025 6:46 AM
P P
R
E NOTE R
E
P P
A
R
 The difference of corresponding uppercase and lowercase character is A
R
E always 32 E
D D
 ASCII value of “a” – ASCII value of “A” = 32
B
Y
 ASCII value of “m” – ASCII value of “M” = 32 B
Y

S S
U
 Using this logic, we can convert uppercase letter into its lowercase U
L L
A and vice – versa. A
V V

N N
E E
P P
A A
L L

56 3/9/2025 6:46 AM
P P
R
E Void Type R
E
P P
A
R
 This void type has no value A
R
E E
D D
 This is usually used to specify a type of function when it does not
B B
Y return any value to the calling function Y

S S
U U
L  Example: L
A A
V void main() V

N void whatIsThis(); N
E E
P P
A A
L L

57 3/9/2025 6:46 AM
P P
R
E Derived Data Types R
E
P P
A  C supports a feature called type definition which allows users to define an A
R R
E identifier that would represent an existing data type. E
D D

B  typedef statement is used to give new name to an existing data type. B


Y Y

S  It allows users to define new data types that are equivalent to an existing S
U U
L data types. L
A A
V V
 General form:
N N
E typedef existing_data_type new_name_for_existing_data_type;
E
P P
A A
L  Example: L

58
typedef int integer; 3/9/2025 6:46 AM
P P
R
E Constants R
E
P P
A
R
 A constant is a quantity that doesn’t change during the execution A
R
E E
D D
 These fixed values are also called literals
B B
Y Y

S  Constants can be of any of the basic data types like an integer S


U U
L constant, a floating constant, a character constant, or a L
A
V
string literal A
V

N N
E  There are enumeration constants as well E
P P
A A
L L

59 3/9/2025 6:46 AM
P P
R
E Integer Literals R
E
P P
A
R
 An integer literal can be a decimal, octal, or hexadecimal constant. A
R
E E
D D
 A prefix specifies the base or radix: 0x or 0X for hexadecimal, 0 for
B B
Y octal, and nothing for decimal. Y

S S
U U
L  An integer literal can also have a suffix that is a combination of U L
A
V
and L, for unsigned and long, respectively. A
V
85 /* decimal */
N N
0213 /* octal */
E  Following are other examples of E
P 0x4b /* hexadecimal */ P
A various types of integer literals: 30 /* int */ A
L L
30l /* long */
60 3/9/2025 6:46 AM
P P
R
E Character Constants R
E
P P
A
R
 Character literals are enclosed in single quotes A
R
E
D
‘x’ can be stored in a simple variable of char type E
D

B B
Y  A character literal can be a: Y

S  Plain character (e.g.‘x’) S


U U
L  An escape sequence (e.g.‘\t’) L
A
V
 Or a universal character (e.g.‘\u02C0’) A
V

N N
E  There are certain characters in C that represent special meaning E
P P
A when preceded by a backslash. For example, new line (\n) or tab (\t) A
L L

61 3/9/2025 6:46 AM
P P
R
E String Constants R
E
P P
A
R
 Sequence of characters enclosed in double quotes A
R
E E
D D
 May contain letters, numbers, special characters or blank spaces
B B
Y Y

S  Example: S
U U
L “hello” L
A A
V “hi” V

N “2076” N
E E
P P
A A
L L

62 3/9/2025 6:46 AM
P P
R
E Variables R
E
P P
A
R
 A symbolic name which is used to store data item. i.e. a numerical A
R
E quantity or a character constant. E
D D

B B
Y  Unlike constant, the value of a variable can change during the Y

S execution of a program. S
U U
L L
A
V
 The same variable can store different value at different portion of a A
V
program.
N N
E E
P P
A  Variable name may consist of letters, digits, or underscore characters. A
L L

63 3/9/2025 6:46 AM
P P
R
E Variable Declaration R
E
P P
A
R
 Any variable should be defined before using it in a program A
R
E E
D D
 Variable declaration syntax:
B B
Y data-type variable_name1, variable_name2, … Y

S S
U U
L  Valid declaration are: L
A A
V int n1; V

N int centi, temp; N


E E
P float radius; P
A A
L char gender; L

64 3/9/2025 6:46 AM
P P
R
E Rules for Variable Declaration R
E
P P
A  The variable name should start with only letters. A
R R
E E
D D
 The variable name shouldn’t be keyword.
B B
Y Y

S
 White spaces are not allowed between characters of variable but
S
U underscores are approved. U
L L
A A
V
 The variable name is case sensitive. V

N TEMP and temp are different variables. N


E E
P P
A A
L  No two variables of the same name are allowed to be declared in the L
same scope.
65 3/9/2025 6:46 AM
P P
R
E Preprocessor Directives R
E
P P
A
R
 Collection of special statements that are executed at the beginning of A
R
E a compilation process. E
D D

B B
Y  Placed in the source program before the main function. Y

S #include<stdio.h> //used for file inclusion S


U U
L #define PI 3.1416 //defining symbolic constant PI L
A A
V #define TRUE 1 //used for defining TRUE as 1 V

N N
E E
P  These statements are called preprocessor directives as they are P
A processed before compilation of any other source code in the program. A
L L

66 3/9/2025 6:46 AM
P P
R
E Escape Sequences R
E
P P
A
R
 An escape sequence is a non-printing characters used in C. A
R
E E
D D
 Character combination consisting of backslash (\) followed by a
B B
Y letter or by a combination of digits. Y

S S
U U
L  Each sequences are typically used to specify actions such as carriage L
A
V
return, backspace, line feed, or move cursors to next line. A
V

N N
E E
P P
A A
L L

67 3/9/2025 6:46 AM
P P
R
E Escape Sequences R
E
P P
A Character Escape Sequence ASCIIValue A
R R
E Bell (alert) \a 007
E
D Backspace \b 008 D

B Horizontal tab \t 009


B
Y Vertical tab \v 011 Y

S Newline (linefeed) \n 010


S
U Form feed \f 012 U
L L
A Carriage return \r 013 A
V Quotation mark (“) \“ 034 V

N Apostrophe (’) \’ 039 N


E Question mark (?) \? 063 E
P P
A Backslash (\) \\ 092 A
L Null \0 000 L

68 3/9/2025 6:46 AM
P P
R
E Escape Sequence in C R
E
P P
A
R
#include<stdio.h> A
R
E
D
void main() E
D

B
{
B
Y printf(“Hello! \n I am testing an escape sequence”); Y

S } S
U U
L L
A A
V OUTPUT: V

N Hello! N
E E
P I am testing an escape sequence P
A A
L L

69 3/9/2025 6:46 AM
P P
R
E Escape Sequence in C R
E
P P
A
R
#include<stdio.h> A
R
E
D
void main() E
D

B
{
B
Y printf(“Hello \tWorld \n”); Y

S printf(“He said, \“Hello\””); S


U U
L } L
A A
V V

N OUTPUT: N
E E
P Hello World P
A A
L He said,“Hello” L

70 3/9/2025 6:46 AM
P P
R
E Tokens in C R
E
P P
A  The basic elements recognized by the C compiler are the “tokens”. A
R R
E E
D D
 In C, tokens are of six types:
B  Keywords (e.g. int, while, float, printf) B
Y Y

S  Identifiers (e.g. sum, total, num_of_hours) S


U U
L L
A  Constants (e.g 10, 20, -15.4) A
V V

N  Strings (e.g.“ram”,“hello”) N
E E
P P
A  Special symbols (e.g. (), {})
A
L L
 Operators (e.g. +, –, *, /)
71 3/9/2025 6:46 AM
P P
R
E Delimiters R
E
P P
A
R
 A delimiter is a unique character or series of characters that indicates A
R
E the beginning or end of a specific statement, string or function body E
D D
set.
B B
Y Y

S  Delimiter examples include:


S
U  Round brackets or parentheses – () U
L L
A A
V V
 Curly brackets – {}
N N
E E
P  Escape sequence or comments – /* P
A A
L L
 Double quotes for defining string literals – “ ” 3/9/2025 6:46 AM
72
P P
R
E Expressions R
E
P P
A
R
 In programming, an expression is any legal combination of symbols A
R
E that represents a value. E
D D

B B
Y  For example, in the C language, 𝒙 + 𝟓 is a legal expression. Y

S S
U U
L  Every expression consists of at least one operand and can have one or L
A
V
more operators. A
V

N N
E E
P P
A A
L L

73 3/9/2025 6:46 AM
P P
R
E Expressions R
E
P P
A
R
 Operands are values and operators are symbols that represent A
R
E particular actions. E
D D

B B
Y  Types of expressions: Y

S Type Explanation Example S


U U
L Infix Expression in which operator is in between operands a+b
L
A Prefix Expression in which operator is written before operands +ab A
V V
Postfix Expression in which operator is written after operands ab+
N N
E E
P P
A A
L L

74 3/9/2025 6:46 AM
P P
R
E Comments R
E
P P
A  Used for program documentation. A
R R
E E
D D
 Comments are not compiled.
B B
Y Y

S
 The C syntax for writing comment is
S
U /* U
L L
A anything written in between slash and asterisk and asterisk A
V
and slash is a comment V

N */ N
E E
P P
A A
L  Another way to write comment in C is L

75
//using double slash (this line only) 3/9/2025 6:46 AM
P P
R R
E E
P P
A A
R R
E E
D D

B B
Y Y

S
END OF UNIT TWO
S
U U
L L
A A
V V

N N
E E
P P
A A
L L

76 3/9/2025 6:46 AM
P P
R R
E
P
Syllabus E
P
A
R
 UNIT 3: Input and Output A
R
E E
D D
o Conversion Specification
B B
Y Y
o Reading a character
S S
U U
L L
A o Writing a character A
V V

N o I/O Operations N
E E
P P
A A
L o Formatted I/O
L

77 3/9/2025 6:46 AM
P P
R R
E E
P P
A A
R R

UNIT 3
E E
D D

B B
Y Y

S INPUT AND OUTPUT S


U U
L L
A A
V V

N N
E E
P P
A A
L L

78 3/9/2025 6:46 AM
P P
R
E Data Input and Output R
E
P P
A A
R R
E
D
 A program without any input or output has no meaning. E
D

B B
Y  Input → Process → Output Y

S S
U U
L  Reading the data from input devices and displaying the result are the L
A A
V two main tasks of any program. V

N N
E E
P P
A A
L L

79 3/9/2025 6:46 AM
P P
R
E Data Input and Output R
E
P P
A A
R R
E
D
 Input/Output functions are the links between the user and the E
D
terminal.
B B
Y Y

S  Input functions that are used to read data from keyboard are called S
U
L
standard input functions. Example: scanf(), getchar(), getch(), U
L
A etc. A
V V

N N
E  Output functions that are used to display the result on the screen are E
P P
A called standard output functions. Example: printf(), putchar(), A
L puts(), etc. L

80 3/9/2025 6:46 AM
P P
R
E Data Input and Output R
E
P P
A  In C, the standard library stdio.h provides functions for input and A
R R
E output. E
D D

B  The instruction #include<stdio.h> tells the compiler to search B


Y Y
for a file named stdio.h and places its contents at this point in the
S program. S
U U
L L
A A
V  The contents of the header file become part of the source code when it V

N
is compiled.
N
E E
P P
A  The input/output functions are classified into two types: A
L
 Formatted functions L

81  Unformatted functions 3/9/2025 6:46 AM


P P
R
E Formatted Functions R
E
P P
A
R
 Formatted functions allow to read the input from the keyboard or the A
R
E output displayed on screen to be formatted according to our E
D D
requirements.
B B
Y input function: scanf() Y

S output function: printf() S


U U
L L
A
V
 Example: Consider the following data – 50, 13.45, Ram (int, A
V
float, char)
N N
E E
P P
A  This is possible using the scanf() function which stands for scan A
L
formatted. L

82 3/9/2025 6:46 AM
P P
R
E Formatted Functions R
E
P P
A  The built-in function scanf() can be used to enter input data into the A
R R
E computer from a standard input device. E
D D

B  The general form of scanf is B


Y Y
scanf(“control string”, arg1, arg2, …, argn);
S control string → format in which data is to be entered S
U U
L arg1, arg2, …→ location where data is stored preceded by L
A ampersand (&) A
V V

N N
E  The control string consists of individual groups of data formats, with one
E
P group for each input data item. P
A A
L L
 Each data format must begin with a percentage sign.
83 3/9/2025 6:46 AM
P P
R
E Use of printf() & scanf() R
E
P P
A A
R R
E
D
 %c → character → printf(“%c”,‘a’); E
D

B B
Y  %d → decimal integer → printf(“%d”, 100); Y

S S
U U
L  %f → floating point number → printf(“%f ”, 1.234); L
A A
V V

N  %s → string → printf(“%s”,“C-book”); N
E E
P P
A A
L L

84 3/9/2025 6:46 AM
P P
R
E Format Specifiers for I/O R
E
P P
A A
R R
E Data Type Format Specifier
E
D D
int %d
B B
Y char %c Y

S float %f S
U U
L double %lf L
A A
V short int %hd V

N unsigned int %u N
E E
P long int %ld P
A A
L long long int %lld L

85 3/9/2025 6:46 AM
P P
R
E C Output R
E
P P
A
#include<stdio.h>  All valid C programs must contain A
R R
E the main() function. The code
D
void main() execution begins from the start of
E
D

B
{ the main() function.
B
Y /* Displays the string inside Y

S quotations */  The printf() is a library function to


S
U printf("C Programming"); send formatted output to the U
L screen. The function prints the L
A } string inside quotations. A
V V

N N
E OUTPUT  To use printf() in our program, we E
P need to include stdio.h header file P
A C Programming A
L using the #include<stdio.h> L
statement.
86 3/9/2025 6:46 AM
P P
R
E Integer Output R
E
P P
A
R
#include<stdio.h> A
R
E
D
int main()  We use %d format specifier to E
D
{ print int types.
B B
Y int testInteger = 5; Y

S printf("Number = %d", testInteger);  Here, the %d inside the S


U
L
return 0; quotations will be replaced by U
L
A
} the value of testInteger. A
V V

N N
E OUTPUT E
P P
A Number = 5 A
L L

87 3/9/2025 6:46 AM
P P
R
E Float & Double Output R
E
P P
A #include<stdio.h> A
R R
E
int main()
D {  To print float, we use %f format E
D
float num1 = 13.5; specifier.
B double num2 = 12.4; B
Y Y
float num3 = 14.62113;
S printf("number1 = %f\n", num1);  Similarly, we use %lf to print S
U printf("number2 = %lf\n", num2);
L printf("number3 = %.3f", num3);
double values U
L
A
return 0; A
V V
}
N
 Also, we use %.3f to print float
N
E
OUTPUT
value with three decimal places. E
P P
A number1 = 13.500000 A
L
number2 = 12.400000 L

88 number3 = 14.621 3/9/2025 6:46 AM


P P
R
E Characters Output R
E
P P
A
R
#include<stdio.h> A
R
E
D
int main() E
D

B
{
B
Y char chr = 'a';  To print char, we use %c format Y

S printf("character = %c", chr); specifier. S


U
L
return 0; U
L
A
V
} A
V

N N
E OUTPUT E
P P
A character = a A
L L

89 3/9/2025 6:46 AM
P P
R
E C Input R
E
P P
 In C programming, scanf() is one of the
A
R
#include <stdio.h> commonly used function to take input from the
A
R
E int main() user.
E
D  The scanf() function reads formatted input D
{ from the standard input such as keyboards.
B
Y
int testInteger; B
 Here, we have used %d format specifier inside Y
printf("Enter an integer: "); the scanf() function to take int input from the
S user. S
U scanf("%d", &testInteger); U
L printf("Number = %d",testInteger);  When the user enters an integer, it is stored in L
A A
return 0; the testInteger variable.
V V

N
}  Notice, that we have used &testInteger inside N
E scanf().
E
P P
A OUTPUT  It is because &testInteger gets the address of A
L testInteger, and the value entered by the user is L
Enter an integer: 4 stored in that address.
90
Number = 4 3/9/2025 6:46 AM
P P
R
E Float & Double Input/Output R
E
P P
A #include <stdio.h> A
R int main() R
E { E
D D
float num1;
double num2;
B B
printf("Enter a number: ");
Y
scanf("%f", &num1);  We use %f and %lf format Y

S printf("Enter another number: "); specifier for float and double S


U scanf("%lf", &num2);
L printf("num1 = %f\n", num1); respectively. U
L
A printf("num2 = %lf", num2); A
V return 0; V
}
N N
E E
P
OUTPUT
P
A Enter a number: 12.523
A
L Enter another number: 10.2 L
num1 = 12.523000
91 num2 = 10.200000 3/9/2025 6:46 AM
P P
R
E Characters Input/Output R
E
P P
A
R
#include <stdio.h>  When a character is entered by the A
user in the above program, the R
E int main() E
D character itself is not stored. D
{
B
char chr; B
Y  Instead, an integer value (ASCII Y
printf("Enter a character: "); value) is stored.
S S
U scanf("%c",&chr); U
L printf("You entered %c.", chr); L
A  And when we display that value A
V return 0; using %c text format, the entered V

N
} character is displayed. N
E E
P P
A OUTPUT  If we use %d to display the A
L character, it's ASCII value is L
Enter a character: g
printed.
92
You entered g 3/9/2025 6:46 AM
P P
R
E ASCII Value R
E
P P
A #include <stdio.h>  When a character is entered by the A
R int main() R
E user in the above program, the E
{
D
char chr;
character itself is not stored. D
printf("Enter a character: ");
B B
scanf("%c", &chr);
Y
//When %c is used, a character is displayed  Instead, an integer value (ASCII Y

S printf("You entered %c.\n",chr); value) is stored. S


U //When %d is used,ASCII value is displayed
U
L printf("ASCII value is %d.", chr); L
A return 0;  And when we display that value A
V } using %c text format, the entered V

N character is displayed. N
OUTPUT
E E
P Enter a character: g
P
A You entered g.  If we use %d to display the A
L ASCII value is 103. character, it's ASCII value is L

93
printed. 3/9/2025 6:46 AM
P P
R
E Input/Output Multiple Values R
E
P P
A #include <stdio.h> A
R R
E
int main()
E
D { D
int a;
B float b; B
Y Y
printf("Enter integer and then a float: ");
S //Taking multiple inputs S
U scanf("%d%f", &a, &b); U
L printf("You entered %d and %f", a, b); L
A
return 0; A
V V
}
N N
E E
P OUTPUT
P
A Enter integer and then a float: -3 A
L
3.4 L

94 You entered -3 and 3.400000 3/9/2025 6:46 AM


P P
R
E Input & Output of Basic Types in C R
E
P P
A
R
 Integer A
R
E  Input: scanf("%d", &intVariable); E
D D
 Output: printf("%d", intVariable);
B B
Y Y

S  Float S
U
L
 Input: scanf("%f", &floatVariable); U
L
A  Output: printf("%f", floatVariable); A
V V

N N
E  Character E
P P
A  Input: scanf("%c", &charVariable); A
L
 Output: printf("%c", charVariable); L

95 3/9/2025 6:46 AM
P P
R
E Input & Output of Advanced Types in C R
E
P P
A A
R R
E
D
 String E
D
 Input: scanf("%s", stringVariable);
B B
Y  Output: printf("%s", stringVariable); Y

S S
U U
L L
A A
V V

N N
E E
P P
A A
L L

96 3/9/2025 6:46 AM
P P
R
E Example – 1 R
E
P P
A // Program to display user input integer, float, and string values A
R R
E
#include<stdio.h>
E
D void main() D
{
B B
Y int n1; Y
float n2;
S S
U char ch[10];
U
L printf("Enter an integer number:"); L
A scanf("%d", &n1); A
V V
printf("Enter a float number:");
N scanf("%f", &n2); N
E E
P printf("Enter a string:");
P
A scanf("%s",&ch); A
L
printf("\nInteger Number: %d \t Float Number: %f \tString: %s",n1,n2,ch); L

97 } 3/9/2025 6:46 AM
P P
R
E Example – 2 R
E
P P
A // Program to add, subtract, multiply, and divide two whole numbers A
R #include<stdio.h> R
E void main() E
D D
{
B int a, b, c; B
Y float d; Y
printf("Enter two whole numbers:\n");
S S
scanf("%d%d", &a, &b);
U U
L c = a + b;
L
A printf("\nSum = %d", c); A
V c = a - b; V
printf("\nDifference = %d", c);
N N
c = a * b;
E E
P printf("\nMultiplication = %d", c);
P
A d = (float)a / b; A
L printf("\nDivision = %f", d); L
}
98 3/9/2025 6:46 AM
P P
R
E Example – 3 R
E
P P
A
R
// Program to convert a temperature given in Celsius to Fahrenheit A
R
E
D
#include<stdio.h> E
D

B
void main()
B
Y { Y

S float c, f; S
U U
L printf("Enter temperature in celsius:"); L
A A
V scanf("%f", &c); V

N f = c * 9 / 5 + 32; N
E E
P printf("Temperature in fahrenheit = %f", f); P
A A
L } L

99 3/9/2025 6:46 AM
P P
R
E Example – 4 R
E
P P
A // Program to find area and circumference of circle A
R R
E #include<stdio.h> E
D #include<math.h> D

B #define PI 3.1415 B
Y void main() Y

S { S
U float r, a, c; U
L L
A printf("Enter radius:"); A
V scanf("%f", &r); V

N a = PI * pow(r, 2); N
E c = 2 * PI * r; E
P P
A printf("Area = %f\n",a); A
L printf("Circumference = %f",c); L

100 } 3/9/2025 6:46 AM


P P
R
E Unformatted Functions R
E
P P
A  Unformatted I/O functions allow to supply input or display output A
R R
E in user desired format. E
D D

B  getch(), getche(), getchar(), gets(), puts(), putchar(), etc. are the B


Y Y
examples of unformatted input/output functions.
S S
U U
L  Unformatted input and output functions do not contain format L
A A
V specifier in their syntax. V

N N
E
 Unformatted I/O functions are used for storing data more compactly. E
P P
A A
L L
 Mainly, unformatted I/O functions are used for character and string
101 data types. 3/9/2025 6:46 AM
P P
R
E getchar() & putchar() Functions R
E
P P
A  The getchar() function reads a character from the terminal and returns it A
R as an integer. R
E E
D D
 This function reads only single character at a time.
B B
Y Y
 You can use this method in a loop in case you want to read more than one
S S
U character. U
L L
A A
V  The putchar() function displays the character passed to it on the screen and
V
returns the same character.
N N
E E
P  This function too displays only a single character at a time. P
A A
L L
 In case you want to display more than one characters, use putchar() method
102 in a loop. 3/9/2025 6:46 AM
P P
R
E getchar() & putchar() Functions R
E
P P
A
R
#include <stdio.h> A
R
E
D
void main( ) E
D

B
{
B
Y int c; Y

S printf("Enter a character:\n"); S
U
L
// Take a character as input and store it in variable c U
L
A c = getchar(); A
V V
// display the character stored in variable c
N N
E printf("The entered character is: "); E
P putchar(c); P
A A
L } L

103 3/9/2025 6:46 AM


P P
R
E gets() & puts() Functions R
E
P P
A
R
 The gets() function reads a line from stdin (standard input) into the A
R
E buffer pointed to by str pointer, until either a terminating newline or E
D D
EOF (end of file) occurs.
B B
Y Y

S  The puts() function writes the string str and a trailing newline to
S
U stdout (standard output). U
L L
A A
V V
 str → This is the pointer to an array of chars where the C string is
N N
E stored. (Ignore if you are not able to understand this now) E
P P
A A
L L

104 3/9/2025 6:46 AM


P P
R
E gets() & puts() Functions R
E
P P
A
R
#include<stdio.h> A
R
E
D
void main() E
D

B
{
B
Y /* character array of length 100 */ Y

S char name[100]; S
U
L
printf("Enter your name:\n"); U
L
A gets(name); A
V V
printf("Your name is ");
N N
E puts(name); E
P
A
} P
A
L L

105 3/9/2025 6:46 AM


P P
R
E scanf() & gets() – Difference R
E
P P
A
R
 The main difference between these two functions is that scanf() stops A
R
E reading characters when it encounters a space, but gets() reads space E
D D
as character too.
B B
Y Y

S  If you enter name as Sulav Nepal using scanf() it will only read
S
U and store Sulav and will leave the Nepal part after space. U
L L
A A
V V
 But gets() function will read it completely.
N N
E E
P P
A A
L L

106 3/9/2025 6:46 AM


P P
R R
E E
P P
A A
R R
E E
D D

B B
Y Y

S
END OF UNIT THREE
S
U U
L L
A A
V V

N N
E E
P P
A A
L L

107 3/9/2025 6:46 AM


P P
R
E Syllabus R
E
P P
A
R
 UNIT 4: Operators and Expressions (4 Hrs.) A
R
E o Arithmetic Operator E
D D
o Relational Operator
B B
Y o Logical or Boolean Operator
Y
o Assignment Operator
S S
U o Ternary Operator U
L L
A o Bitwise Operator A
V
o Increment or Decrement Operator V

N o Conditional Operator N
E E
P o Special Operators (sizeof and comma) P
A A
L o Evaluation of Expression (implicit and explicit type conversion)
L
o Operator Precedence and Associativity
108 3/9/2025 6:46 AM
P P
R R
E E
P P
A A
R R

UNIT 4
E E
D D

B B
Y Y

S OPERATORS AND EXPRESSIONS S


U U
L L
A A
V V

N N
E E
P P
A A
L L

109 3/9/2025 6:46 AM


P P
R
E Operators R
E
P P
A
R
 Symbol that operates on certain data type or data item. A
R
E E
D D
 Used in program to perform certain mathematical or logical
B B
Y manipulations. Y

S S
U U
L  For example: In a simple expression 5+6, the symbol “+” is called an L
A
V
operator which operates on two data items 5 and 6. A
V

N N
E  The data items that operator act upon are called operands. E
P P
A A
L L

110 3/9/2025 6:46 AM


P P
R
E Expression R
E
P P
A
R
 An expression is a combination of variables, constants, and operators A
R
E written according to syntax of the language. E
D D

B B
Y  For example: Y

S 8+10 S
U
L
a+c*d U
L
A a>b A
V V
a/c
N N
E E
P P
A A
L L

111 3/9/2025 6:46 AM


P P
R
E Operators R
E
P P
A
R
 We can classify operators into: A
R
E  Unary Operators E
D D
 Which requires only one operand
B  For example: ++, -- B
Y Y

S
U
 Binary Operators S
U
L  Which requires two operands L
A A
V
 For example: +, -, *, /, <, >
V

N N
E  Ternary Operators E
P P
 Which requires three operands
A A
L  For example:“?:” (conditional operator) L

112 3/9/2025 6:46 AM


P P
R
E Arithmetic Operators R
E
P P
A
R
 Assume variable A holds 20 and variable B holds 10, then A
R
E E
D Operator Description Example D

B + Adds two operands 𝐴 + 𝐵 = 30 B


Y − Subtracts second operand from the first 𝐴 − 𝐵 = 10 Y

S ∗ Multiplies both operands 𝐴 ∗ 𝐵 = 200 S


U / Divides numerator by de-numerator 𝐴/𝐵 = 2 U
L L
A % Modulus operator and remainder of after an integer division 𝐴%𝐵 = 0 A
V V
++ Increment operator increases the integer value by one 𝐴 ++= 21
N −− Decrement operator decreases the integer value by one 𝐴 −−= 19 N
E E
P P
A A
L L

113 3/9/2025 6:46 AM


P P
R
E Integer Arithmetic R
E
P P
A
R
 Division Rule A
R
E E
D D
 𝑖𝑛𝑡/𝑖𝑛𝑡 = 𝑖𝑛𝑡
B B
Y Y
 𝑓𝑙𝑜𝑎𝑡/𝑓𝑙𝑜𝑎𝑡 = 𝑓𝑙𝑜𝑎𝑡
S S
U U
L L
A  𝑖𝑛𝑡/𝑓𝑙𝑜𝑎𝑡 = 𝑓𝑙𝑜𝑎𝑡 A
V V

N  𝑓𝑙𝑜𝑎𝑡/𝑖𝑛𝑡 = 𝑓𝑙𝑜𝑎𝑡 N
E E
P P
A A
L L

114 3/9/2025 6:46 AM


P P
R
E EXAMPLE – ONE R
E
P P
A #include<stdio.h>
A
R #include<conio.h>
R
E void main()
E
D { int a=21; int b=10; int c;
D
c=a+b;
B printf("Line 1 -Value of c is %d\n",c); B
Y c=a-b; Y
printf("Line 2 -Value of c is %d\n",c);
S c=a*b; S
U printf("Line 3 -Value of c is %d\n",c); U
L c=a/b; L
A printf("Line 4 -Value of c is %d\n",c); A
V c=a%b; V
printf("Line 5 -Value of c is %d\n",c);
N a++; N
E printf("Line 6 -Value of c is %d\n",a); E
P a--; P
A printf("Line 7 -Value of c is %d\n",a); A
L getch(); L
}
115 3/9/2025 6:46 AM
P P
R
E Relational Operators R
E
P P
A
R
 Assume variable A holds 10 and variable B holds 20, then A
R
E Operator Description Example E
D D
Checks if the values of two operands are equal or not. If yes, then ሺ𝐴 =
==
B the condition becomes true. = 𝐵ሻ𝑖𝑠 𝑛𝑜𝑡 𝑡𝑟𝑢𝑒 B
Y Y
Checks if the values of two operands are equal or not. If the
!= 𝐴! = 𝐵 𝑖𝑠 𝑡𝑟𝑢𝑒
S values are not equal, then the condition becomes true.
S
U Checks if the value of left operand is greater than the value of U
L > 𝐴 > 𝐵 𝑖𝑠 𝑛𝑜𝑡 𝑡𝑟𝑢𝑒
right operand. If yes, then the condition becomes true. L
A A
V Checks if the value of left operand is less than the value of right
< 𝐴 < 𝐵 𝑖𝑠 𝑡𝑟𝑢𝑒 V
operand. If yes, then the condition becomes true.
N Checks if the value of left operand is greater than or equal to the N
ሺ𝐴 >
E >= E
value of right operand. If yes, then the condition becomes true. = 𝐵ሻ 𝑖𝑠 𝑛𝑜𝑡 𝑡𝑟𝑢𝑒
P P
A Checks if the value of left operand is less than or equal to the A
L
<= ሺ𝐴 <= 𝐵ሻ 𝑖𝑠 𝑡𝑟𝑢𝑒
value of right operand. If yes, then the condition becomes true. L

116 3/9/2025 6:46 AM


P P
R
E EXAMPLE – TWO R
E
P P
A #include<stdio.h>
A
#include<conio.h>
R R
void main()
E E
{ int a=21; int b=10; int c;
D if(a==b) D
{ printf("Line 1 - a is equal to b\n"); }
B else B
Y { printf("Line 1 - a is not equal to b\n"); } Y
if(a<b)
S { printf("Line 2 - a is less than b\n"); } S
U else
U
L { printf("Line 2 - a is not less than b\n"); }
L
A if(a>b)
{ printf("Line 3 - a is greater than b\n"); }
A
V V
else
{ printf("Line 3 - a is not greater than b\n"); }
N if(a<=b) N
E { printf("Line 4 - a is either less than or equal to b\n"); } E
P if(a>=b) P
A { printf("Line 4 - a is either greater than or equal to b\n");} A
L getch(); L
}

117 3/9/2025 6:46 AM


P P
R
E Logical Operators R
E
P P
A
R
 Assume variable A holds 1 and variable B holds 0, then A
R
E E
D Operator Description Example D

B
Called “Logical AND Operator”. If both the operands are
&& 𝐴 && 𝐵 𝑖𝑠 𝑓𝑎𝑙𝑠𝑒 B
Y non-zero, then the condition becomes true.
Y
Called “Logical OR Operator”. If any of the two operands is
S || ሺ𝐴 | 𝐵 𝑖𝑠 𝑡𝑟𝑢𝑒
non-zero, then the condition becomes true. S
U U
L Called “Logical NOT Operator”. It is used to reverse the L
! 𝐴 && 𝐵 𝑖𝑠 𝑡𝑟𝑢𝑒
A ! logical state of its operand. If a condition is true, then A
! 𝐴 || 𝐵 𝑖𝑠 𝑓𝑎𝑙𝑠𝑒
V Logical NOT Operator will make it false. V

N N
E E
P P
A A
L L

118 3/9/2025 6:46 AM


P P
R
E EXAMPLE – THREE R
E
P P
A #include<stdio.h>
A
#include<conio.h>
R R
void main()
E E
{ int a=5, b=20;
D if(a && b) D
{ printf("Line 1 - Condition is true\n"); }
B else B
Y { printf("Line 1 - Condition is not true\n"); } Y
if(a || b)
S { printf("Line 2 - Condition is true\n"); } S
U else
U
L { printf("Line 2 - Condition is not true\n"); }
L
A // Let us change the value of a and b
a=0; b=10;
A
V V
if(a && b)
{ printf("Line 3 - Condition is true\n"); }
N else N
E { printf("Line 3 - Condition is not true\n"); } E
P if(!(a && b)) P
A { printf("Line 4 - Condition is true\n"); } A
L getch(); L
}

119 3/9/2025 6:46 AM


P P
R
E Assignment Operators R
E
P P
A A
R Operator Description Example R
E Simple assignment operator. Assigns values from right side 𝐶 = 𝐴 + 𝐵 will assign E
D = D
operands to left side operand the value of 𝐴 + 𝐵 to 𝐶
B Add AND assignment operator. It adds the right operand to the 𝐶 += 𝐴 is equivalent to B
+=
Y left operand and assign the results to the left operand 𝐶 =𝐶+𝐴 Y

S
Subtract AND assignment operator. It subtracts the right operand 𝐶 −= 𝐴 is equivalent to
−= S
U from the left operand and assigns the result to the left operand 𝐶 =𝐶−𝐴
U
L Multiply AND assignment operator. It multiplies the right L
A 𝐶 ∗= 𝐴 is equivalent to
∗= operand with the left operand and assigns the result to the left A
V 𝐶 =𝐶∗𝐴 V
operand
N Divide AND assignment operator. It divides the left operand with 𝐶/= 𝐴 is equivalent to N
/=
E the right operand and assigns the result to the left operand 𝐶 = 𝐶/𝐴 E
P P
A
Modulus AND assignment operator. It takes modulus using two 𝐶% = 𝐴 is equivalent to
%= A
L operands and assigns the result to the left operand 𝐶 = 𝐶%𝐴
L

120 3/9/2025 6:46 AM


P P
R
E EXAMPLE – FOUR R
E
P P
A #include<stdio.h> A
R #include<conio.h> R
E void main() E
D D
{ int a=1, b=2, c;
B c=a+b; B
Y printf("Line 1 - c=%d",c); Y
c+=a;
S S
printf("\nLine 2 - c=%d",c);
U U
L c-=a;
L
A printf("\nLine 3 - c=%d",c); A
V c*=a; V
printf("\nLine 4 - c=%d",c);
N N
c/=a;
E E
P printf("\nLine 5 - c=%d",c);
P
A c%=a; A
L printf("\nLine 6 - c=%d",c); L
getch();
121 3/9/2025 6:46 AM
}
P P
R
E Increment & Decrement Operators R
E
P P
A A
R  Increment operator is used to increase the value of an operand by 1 R
E E
D D

B  Decrement operator is used to decrease the value of an operand by 1 B


Y Y

S Operator Description Example S


U ++ + +𝑣𝑎𝑟𝑖𝑎𝑏𝑙𝑒 (prefix notation) 𝑣𝑎𝑟𝑖𝑎𝑏𝑙𝑒 = 𝑣𝑎𝑟𝑖𝑎𝑏𝑙𝑒 + 1 U
L L
A ++ 𝑣𝑎𝑟𝑖𝑎𝑏𝑙𝑒 + + (postfix notation) 𝑣𝑎𝑟𝑖𝑎𝑏𝑙𝑒 = 𝑣𝑎𝑟𝑖𝑎𝑏𝑙𝑒 + 1 A
V V
−− − −𝑣𝑎𝑟𝑖𝑎𝑏𝑙𝑒 (prefix notation) 𝑣𝑎𝑟𝑖𝑎𝑏𝑙𝑒 = 𝑣𝑎𝑟𝑖𝑎𝑏𝑙𝑒 − 1
N −− 𝑣𝑎𝑟𝑖𝑎𝑏𝑙𝑒 − − (postfix notation) 𝑣𝑎𝑟𝑖𝑎𝑏𝑙𝑒 = 𝑣𝑎𝑟𝑖𝑎𝑏𝑙𝑒 − 1 N
E E
P P
A A
L L

122 3/9/2025 6:46 AM


P P
R
E Increment & Decrement Operators R
E
P P
A A
R R
E
D
 Pre-Increment or Pre-Decrement (++a or --a) E
D
 CHANGE the value of the variable
B B
Y  USE the new value Y

S S
U U
L  Post-Increment or Post-Decrement (a++ or a--) L
A
V
 USE the original value of the variable A
V
 CHANGE the value of the variable
N N
E E
P P
A A
L L

123 3/9/2025 6:46 AM


P P
R
E EXAMPLE – FIVE R
E
P P
A /* Use of pre increment & post increment Operators */ A
R #include<stdio.h> R
E E
D #include<conio.h>
D
void main()
B { B
Y Y
int a=20, b=10;
S printf("INCREMENT OPERATORS\n");
S
U printf("%d\n",a); U
L printf("%d\n",++a); L
A printf("%d\n",a++); A
V printf("%d\n",a); V
printf("\nDECREMENT OPERATORS\n");
N N
E
printf("%d\n",b);
E
P printf("%d\n",--b);
P
A printf("%d\n",b--); A
L printf("%d\n",b); L
getch();
124 3/9/2025 6:46 AM
}
P P
R
E Conditional Operator R
E
P P
A
R
 The operator pair “?:” is known as conditional operator. A
R
E E
D
 It takes three operands. D

B B
Y
 Also called as ternary operator. Y

S S
U
L
 General form: U
L
A 𝑒𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛1? 𝑒𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛2: 𝑒𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛3 A
V 𝑒𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛1 is evaluated first V

N If 𝑒𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛1 is true
N
E then value of 𝑒𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛2 is the value of condition expression E
P Else P
A A
L
the value of 𝑒𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛3 is the value of conditional expression
L

125 3/9/2025 6:46 AM


P P
R
E EXAMPLE – SIX R
E
P P
A
R
#include<stdio.h> A
R
E
D
#include<conio.h> E
D

B
void main()
B
Y { int num1, num2, larger; Y

S printf("Enter two numbers:\n"); S


U U
L scanf("%d %d", &num1, &num2); L
A A
V larger=num1>num2?num1:num2; V

N printf("\nThe larger number is %d", larger); N


E E
P getch(); P
A A
L } L

126 3/9/2025 6:46 AM


P P
R
E Bitwise Operator R
E
P P
A A
R  Bitwise operators are used for manipulating data at bit level.
R
E E
D D
 These operators are used for testing the bits or shifting them to the left or
B to the right. B
Y Y

S S
U  Can be applied only to integer-type operands and not to float or double.
U
L L
A A
V  Three types of bitwise operators V
 Bitwise Logical Operators
N N
E  Bitwise Shift Operators E
P  One’s Complement Operator P
A A
L L

127 3/9/2025 6:46 AM


P P
R
E Bitwise Logical Operator R
E
P P
A A
R  Performs logical tests between two integer-type operands. R
E E
D D

B  These operators work on their operands bit-by-bit starting from the B


Y least significant (i.e. rightmost) bit. Y

S S
U U
L  Three logical bitwise operators: L
A A
V  Bitwise AND (&) V

N  Bitwise OR (|)
N
E  Bitwise Exclusive OR (^) E
P P
A A
L L

128 3/9/2025 6:46 AM


P P
R
E Bitwise AND (&) R
E
P P
A A
R  Logical ANDing between two operands. R
E E
D D
 The result of ANDing operation is 1 if both the bits have a value of
B B
Y 1; otherwise it is 0. Y

S S
U  Consider 𝑛𝑢𝑚1 = 45 and 𝑛𝑢𝑚2 = 25 U
L L
A  𝑛𝑢𝑚1 → 0000 0000 0010 1101 A
V
 𝑛𝑢𝑚2 → 0000 0000 0001 1001 V

N N
E E
P  If 𝑛𝑢𝑚3 = 𝑛𝑢𝑚1 & 𝑛𝑢𝑚2 P
A A
L  𝑛𝑢𝑚3 → 0000 0000 0000 1001
L

129 3/9/2025 6:46 AM


P P
R
E Bitwise OR (|) R
E
P P
A A
R  Logical ORing between two operands. R
E E
D D
 The result of ORing operation is 1 if either of the bits have a value of
B B
Y 1; otherwise it is 0. Y

S S
U  Consider 𝑛𝑢𝑚1 = 45 and 𝑛𝑢𝑚2 = 25 U
L L
A  𝑛𝑢𝑚1 → 0000 0000 0010 1101 A
V
 𝑛𝑢𝑚2 → 0000 0000 0001 1001 V

N N
E E
P  If 𝑛𝑢𝑚3 = 𝑛𝑢𝑚1 | 𝑛𝑢𝑚2 P
A A
L  𝑛𝑢𝑚3 → 0000 0000 0011 1101
L

130 3/9/2025 6:46 AM


P P
R
E Bitwise Exclusive XOR (^) R
E
P P
A A
R  Logical Exclusive ORing between two operands. R
E E
D D
 The result of Exclusive ORing operation is 1 only if one of the bits
B B
Y have a value of 1; otherwise it is 0. Y

S S
U  Consider 𝑛𝑢𝑚1 = 45 and 𝑛𝑢𝑚2 = 25 U
L L
A  𝑛𝑢𝑚1 → 0000 0000 0010 1101 A
V
 𝑛𝑢𝑚2 → 0000 0000 0001 1001 V

N N
E E
P  If 𝑛𝑢𝑚3 = 𝑛𝑢𝑚1 ^ 𝑛𝑢𝑚2 P
A A
L  𝑛𝑢𝑚3 → 0000 0000 0011 0100
L

131 3/9/2025 6:46 AM


P P
R
E EXAMPLE – SEVEN R
E
P P
A
R
#include<stdio.h> A
R
E
D
void main() E
D

B
{
B
Y int num1=45, num2=25,AND, OR, XOR; Y

S AND = num1 & num2; S


U U
L OR = num1 | num2; L
A A
V XOR = num1 ^ num2; V

N printf("AND=%d\n",AND); N
E E
P printf("OR=%d\n", OR); P
A A
L printf("XOR=%d\n", XOR); L

132 } 3/9/2025 6:46 AM


P P
R
E Bitwise Shift Operators R
E
P P
A A
R  These operators are used to move bit patterns either to the left or to R
E
D
the right. E
D

B B
Y  There are two bitwise shift operators: Y

S  Left shift (<<) S


U U
L  Right shift (>>) L
A A
V V

N N
E E
P P
A A
L L

133 3/9/2025 6:46 AM


P P
R
E Bitwise Left Shift (<<) Operators R
E
P P
A A
R  It causes the operand to be shifted to the left by n positions. R
E
D
operand<<n E
D

B B
Y  The leftmost n bits in the original bit pattern will be lost and the Y

S rightmost n bits empty positions will be filled with 0’s. S


U U
L L
A A
V  Example: 𝑛𝑢𝑚1 = 45; execute 𝑛𝑢𝑚2 = 𝑛𝑢𝑚1 ≪ 3;
V

N 𝒏𝒖𝒎𝟏 0000 0000 0010 1101


N
E Shift 1 0000 0000 0101 1010 E
P P
A Shift 2 0000 0000 1011 0100
A
L Shift 3 0000 0001 0110 1000 𝑛𝑢𝑚2 L

134 3/9/2025 6:46 AM


P P
R
E Bitwise Right Shift (>>) Operators R
E
P P
A A
R  It causes the operand to be shifted to the right by n positions. R
E
D
operand>>n E
D

B B
Y  The empty leftmost n bits position will be filled with 0’s, if the Y

S operand is an unsigned integer. S


U U
L L
A A
V  Example: 𝑼𝒏𝒔𝒊𝒈𝒏𝒆𝒅 𝒊𝒏𝒕 𝒏𝒖𝒎𝟏 = 𝟒𝟓; execute 𝒏𝒖𝒎𝟐 = 𝒏𝒖𝒎𝟏 ≫ 𝟑; V

N 𝒏𝒖𝒎𝟏 0000 0000 0010 1101


N
E Shift 1 0000 0000 0001 0110 E
P P
A Shift 2 0000 0000 0000 1011 A
L Shift 3 0000 0000 0000 0101 𝑛𝑢𝑚2 L

135 3/9/2025 6:46 AM


P P
R
E Bitwise One’s Complement Operator R
E
P P
A A
R  It is a unary operator which inverts all the bits represented by its R
E
D
operand. i.e. all 0’s becomes 1’s and all 1’s becomes 0’s. E
D

B B
Y  For any integer 𝒏, bitwise one’s complement of 𝒏 will be – ሺ𝒏 + 𝟏ሻ. Y

S S
U U
L  Example: If 𝒏𝒖𝒎𝟏 = 𝟒𝟓, then we execute the statement; 𝒏𝒖𝒎𝟐 = ~𝒏𝒖𝒎𝟏; L
A A
V V

N  The resulting bit pattern represents the decimal −𝟒𝟔.


N
E E
P 𝒏𝒖𝒎𝟏 0000 0000 0010 1101 P
A A
L ~𝑛𝑢𝑚1 1111 1111 1101 0010 𝑛𝑢𝑚2
L

136 3/9/2025 6:46 AM


P P
R
E Bitwise One’s Complement Operator R
E
P P
A A
R R
E E
D D

B B
Y Y

S S
U U
L L
A A
V V

N N
E E
P P
A A
L L

137 3/9/2025 6:46 AM


P P
R
E Bitwise One’s Complement Operator R
E
P P
A A
R R
E E
D D

B B
Y Y

S S
U U
L L
A A
V V

N N
E E
P P
A A
L L

138 3/9/2025 6:46 AM


P P
R
E EXAMPLE – EIGHT R
E
P P
A
R
#include<stdio.h> A
R
E
D
void main() E
D

B
{
B
Y int num1=45, left, right, comp; Y

S left = num1<<3; S
U U
L right = num1>>3; L
A A
V comp = ~num1; V

N printf("%d\n", left); N
E E
P printf("%d\n", right); P
A A
L printf("%d\n", comp); L

139 } 3/9/2025 6:46 AM


P P
R
E Special Operators R
E
P P
A A
R  C supports some special operators like sizeof operator and comma R
E
D
operator. E
D

B B
Y  sizeof Operator: Y

S  It is used with an operand to return the number of bytes it occupies. S


U U
L L
A
V
 The operand may be constant, variable or a data type qualifier. A
V

N N
E E
P P
A A
L L

140 3/9/2025 6:46 AM


P P
R
E Special Operators R
E
P P
A
R
 C supports some special operators like sizeof operator and comma A
R
E operator. E
D D

B  comma Operator: B
Y  It can be used to link related expressions together. Y

S S
U  A comma-linked list of expressions are evaluated from left-to-right and the
U
L value of the rightmost expression is the value of the combined expressions. L
A A
V
 For example: 𝑛𝑢𝑚3 = ሺ𝑛𝑢𝑚1 = 45, 𝑛𝑢𝑚2 = 25, 𝑛𝑢𝑚1 + V

N
𝑛𝑢𝑚2ሻ
N
E  At first, 45 is assigned to 𝑛𝑢𝑚1
E
P  Then, 25 is assigned to 𝑛𝑢𝑚2 P
A  Then finally, sum of 𝑛𝑢𝑚1 and 𝑛𝑢𝑚2 is assigned to 𝑛𝑢𝑚3 A
L L

141 3/9/2025 6:46 AM


P P
R
E EXAMPLE – NINE R
E
P P
A
R
#include <stdio.h> A
R
E
D
int main() E
D

B
{
B
Y short int a = 0; Y

S printf("Size of variable a : %d\n",sizeof(a)); S


U
L
printf("Size of int data type : %d\n",sizeof(int)); U
L
A printf("Size of char data type : %d\n",sizeof(char)); A
V V
printf("Size of float data type : %d\n",sizeof(float));
N N
E printf("Size of double data type : %d\n",sizeof(double)); E
P return 0; P
A A
L } L

142 3/9/2025 6:46 AM


P P
R
E Expressions R
E
P P
A  Expressions are the representation of code inside C text editor which is recognized by A
R compiler. R
E E
D D
 An expression in C consists of syntactically valid combination of operators and operands
B that computes to a value. B
Y Y

S  The C expressions are not a statement.


S
U U
L  The expressions are the basic building blocks of statement. L
A A
V V
 The C expressions are slightly differ than mathematical expressions.
N N
E E
 An expression is a combination of variables, constants, and operators written according to
P P
A
the syntax of C language.
A
L L

143 3/9/2025 6:46 AM


P P
R
E Evaluation of Expressions R
E
P P
A A
R
 The changing an entity of one data type into another is called type casting or
R
E coercion. E
D D

B  This is performed to take advantage of certain features of type representations. B


Y Y

S  In general, fundamental data types can be converted. S


U U
L L
A  The word coercion is used to denote an implicit type casting. A
V V

N  There are two types of casting: N


E E
 ImplicitType Casting
P P
A  ExplicitType Casting A
L L

144 3/9/2025 6:46 AM


P P
R
E Implicit Type Casting R
E
P P
A
 The conversion of data is performed either during compilation or run time is A
R R
E
called implicit type casting.
E
D D
 It is the automatic type conversion process performed by compiler itself.
B B
Y Y
 The data can be lost during this type of casting.
S S
U U
L  The conversion from 𝑓𝑙𝑜𝑎𝑡 to 𝑖𝑛𝑡 can cause loss of higher order bits. L
A A
V V
 For example:
N int p, t, r, i; N
E i=p*t*r/100.00; E
P P
A
Here, the variables are declared as integer but in the calculation 𝑖 = 𝑝 ∗ 𝑡 ∗
𝑟/100.00, after dividing by 100.00, the value of 𝑖 will change in to float variable. A
L L

145 3/9/2025 6:46 AM


P P
R
E Explicit Type Casting R
E
P P
A
 Explicit type conversion can either performed by built-in functions or by a special A
R R
E
syntax generated by coder.
E
D D
 These syntax changes one data type to other by using conversion keyword.
B B
Y Y
 It is a secure manner of changing variables from one data type to other.
S S
U U
L  For example: L
A int a=97, b=65; A
V V
printf("%c %c", (char)a, (char)b);
N N
E  Here, the ASCII value of 𝑎 and 𝐴 are 97 and 65 respectively. So, we print the E
P value after type casting from 𝑖𝑛𝑡𝑒𝑔𝑒𝑟 to 𝑐ℎ𝑎𝑟𝑎𝑐𝑡𝑒𝑟. P
A A
L L

146 3/9/2025 6:46 AM


P P
R
E Operator Precedence and Associativity R
E
P P
A A
R  The precedence is used to determine how an expression involving R
E
D
more than one operator is evaluated. E
D

B B
Y  There are distinct level of precedence. Y

S S
U U
L  The operators at the higher level of precedence are evaluated first. L
A A
V V

N  Operators of same precedence are evaluated either from “left to right” N


E E
P or “right to left” depending on the level also known as P
A associativity. A
L L

147 3/9/2025 6:46 AM


Category Operator Associativity
P P
R Postfix () [] -> . ++ -- Left to Right
R
E Multiplicative */% Left to Right E
P P
A Additive +- Left to Right
A
R Shift << >> Left to Right R
E E
D Relational < <= > >= Left to Right
D
Equality == != Left to Right
B B
Y Bitwise AND & Left to Right
Y
Bitwise OR | Left to Right
S S
U Bitwise XOR ^ Left to Right
U
L Logical AND && Left to Right L
A A
V Logical OR || Left to Right
V
Comma , Left to Right
N N
E Unary + - ! ~ ++ -- (type) * & sizeof Right to Left
E
P Conditional ?: Right to Left P
A A
L = += -= *= /= %= >>=
Assignment Right to Left L
<<= &= ^= |=
148 3/9/2025 6:46 AM
P P
R R
E E
P P
A A
R R
E E
D D

B B
Y Y

S
END OF UNIT FOUR
S
U U
L L
A A
V V

N N
E E
P P
A A
L L

149 3/9/2025 6:46 AM


P P
R
E Syllabus R
E
P P
A
R
 UNIT 5: Control Statement A
R
E o Conditional Statements E
D D

B B
Y o Decision Making and Branching (if, if else, nested if else, else if ladder,
Y
switch statements)
S S
U U
L
A
o Decision Making and Looping (for, while, do while loops) L
A
V V

N o Exit function N
E E
P P
A o Break and Continue A
L L

150 3/9/2025 6:46 AM


P P
R R
E E
P P
A A
R R

UNIT 5
E E
D D

B B
Y Y

S CONTROL STATEMENT S
U U
L L
A A
V V

N N
E E
P P
A A
L L

151 3/9/2025 6:46 AM


P P
R
E Error Types in C Programming R
E
P P
A
R
 While writing C programs, errors may occur which are also known as A
R
E bugs. E
D D

B B
Y  Errors may occur unwillingly which may prevent the program to Y

S compile and run correctly as per the expectation of the programmer. S


U U
L L
A
V
 Basically there are three types of errors in C programming: A
V
 Runtime Errors
N N
E  Compile Errors E
P P
A  Logical Errors
A
L L

152 3/9/2025 6:46 AM


P P
R
E Runtime Errors in C R
E
P P
A
R
 C runtime errors are those errors that occur during the execution of a A
R
E C program and generally occur due to some illegal operation E
D D
performed in the program.
B B
Y Y

S  Examples of some illegal operations that may produce runtime errors


S
U are: U
L L
A  Dividing a number by zero A
V V
 Trying to open a file which does not exist
N  Lack of free memory space N
E E
P P
A A
L L

153 3/9/2025 6:46 AM


P P
R
E Compile Errors in C R
E
P P
A
R
 Compile errors are those errors that occur at the time of compilation A
R
E of the program. E
D D

B B
Y  C compile errors may be further classified as: Y

S  Syntax Errors S
U  Example: 𝑖𝑛𝑡 𝑎, 𝑏: U
L L
A  This will produce syntax error as the statement is terminated with ∶ rather than ;
A
V V

N  Semantic Errors N
E E
P  Example: 𝑏 + 𝑐 = 𝑎;
P
A  Here, we are trying to assign a value of 𝑎 in the value obtained by adding 𝑏 and 𝑐 A
L which has no meaning in C. L

154
 The correct statement will be: 𝑎 = 𝑏 + 𝑐; 3/9/2025 6:46 AM
P P
R
E Logical Errors in C R
E
P P
A
R
 Logical errors are the errors in the output of the program. A
R
E E
D D
 The presence of logical errors leads to undesired or incorrect output.
B B
Y Y

S  These errors are caused due to error in the logic applied in the S
U U
L program to produce the desired output. L
A A
V V

N  Logical errors could not be detected by the compiler, and thus, N


E
P
programmers has to check the entire coding of a C program line by E
P
A line. A
L L

155 3/9/2025 6:46 AM


P P
R
E Control Statements R
E
P P
A  The statement which alter the flow of execution of the program are known A
R R
E as control statements. E
D D

B  Sometimes we have to do certain tasks depending on whether a condition is B


Y true or not. Y

S S
U U
L  Similarly, it is necessary to perform repeated actions or skip some
L
A statements. A
V V

N  For these operations, control statements are needed. N


E E
P P
A A
L  There are two types of control statements:
L
 Decision Making (or branching) Statements
156 3/9/2025 6:46 AM
 Loop or Repeating Construct
P P
R
E Control Statements R
E
P P
A  Decision Making (or branching) Statements A
R R
E
 If statement
E
D  If…Else statement D
 Else…If statement
B
Y
 Nested If…Else statement B
Y
 Switch statement
S S
U U
L  Loop or Repeating Construct L
A  For loop A
V  While loop V

N  Do…While loop
N
E E
P P
A  NOTE:
A
L  Branching is deciding what actions to take L
 Looping is deciding how many times to take a certain action
157 3/9/2025 6:46 AM
P P
R
E Decision Making in C R
E
P P
A  Programmer specifies one or more A
R R
E
conditions to be evaluated.
E
D D

B  If the condition is determined to be true, a


B
Y statement is executed. Y

S S
U  If the condition is determined to be false, U
L the other statements is to be executed. L
A A
V V
 Typical decision making structure found
N N
E in most of the programming language is:
E
P P
A A
L L

158 3/9/2025 6:46 AM


P P
R
E if Statement R
E
P P
A
R
 An if statement consists of a A
R
E Boolean expression followed by E
D D
one or more statements.
B B
Y Y

S  Syntax:
S
U if(boolean_expression) U
L L
A { A
V V
/* statement(s) will execute if the
N Boolean expression is true */ N
E E
P } P
A A
L L

159 3/9/2025 6:46 AM


P P
R
E if Statement R
E
P P
A
R
 If the Boolean expression evaluates to true, then the block of code A
R
E inside the “if ” statement will be executed. E
D D

B B
Y  If the Boolean expression evaluates to false, then the first set of code Y

S after the end of “if ” statement will be executed. S


U U
L L
A
V
 C programming language assumes any non-zero and non-null values A
V
as true and if it is either zero or null, then it is assumed as false
N N
E value. E
P P
A A
L L

160 3/9/2025 6:46 AM


P P
R
E Example - 1 R
E
P P
A
R
#include<stdio.h> A
R
E #include<conio.h> E
D D
void main()
B B
Y { Y

S
int a=10;
S
U if(a<20) U
L L
A { A
V V
printf("a is less than 20\n");
N
E
} N
E
P
A
printf("Value of a is: %d\n", a); P
A
L getch(); L

161 } 3/9/2025 6:46 AM


P P
R
E if…else Statement R
E
P P
A
R
 An if statement can be followed by an optional else statement, which A
R
E executes when the Boolean expression is false. E
D D

B B
Y  Syntax: Y

S if(boolean_expression) S
U
L
{ /* statement(s) will execute if the Boolean expression is true */ U
L
A } A
V V
else
N
E
{ /* statement(s) will execute if the Boolean expression is false */ N
E
P } P
A A
L L

162 3/9/2025 6:46 AM


P P
R
E if…else Statement R
E
P P
A A
R R
E E
D D

B B
Y Y

S S
U U
L L
A A
V V

N N
E E
P P
A A
L L

163 3/9/2025 6:46 AM


P P
R
E if…else Statement R
E
P P
A
R
 If the Boolean expression evaluates to true, then the if block will be A
R
E executed, otherwise, the else block will be executed. E
D D

B B
Y  C programming language assumes any non-zero and non-null values Y

S as true and if it is either zero or null, then it is assumed as false S


U value. U
L L
A A
V V

N N
E E
P P
A A
L L

164 3/9/2025 6:46 AM


P P
R
E Example - 2 R
E
P P
A #include<stdio.h> A
R #include<conio.h> R
E E
D void main()
D
{
B int a=100; B
Y Y
if(a<20)
S { S
U printf("a is less than 20\n"); U
L L
A
}
A
V else V
{
N N
E
printf("a is not less than 20\n");
E
P } P
A printf("Value of a is %d", a); A
L
getch(); L

165
} 3/9/2025 6:46 AM
P P
R
E if…else if…else Statement R
E
P P
A  An if statement can be followed by an optional else if . . . else A
R R
E statement, which is very useful to test various conditions. E
D D

B  Syntax: B
Y Y
if(boolean_expression1)
S
U
{ /* statement(s) will execute if the Boolean expression 1 is true */ S
U
L } L
A A
V else if(boolean_expression2) V
{ /* statement(s) will execute if the Boolean expression 2 is true */
N N
E } E
P else P
A A
L { /* statement(s) will execute if both Boolean expressions are false */ L
}
166 3/9/2025 6:46 AM
P P
R
E Example - 3 R
E
P P
A #include<stdio.h>
A
#include<conio.h>
R R
void main()
E E
{
D int a=100; D
if(a==10)
B { B
Y printf("Value of a is 10\n"); Y
}
S else if(a==20) S
U {
U
L printf("Value of a js 20\n");
L
A }
else if(a==30)
A
V V
{
printf("Value of a is 30\n");
N } N
E else E
P { P
A printf("None of the values are matching\n"); A
L } L
printf("Exact value of a is %d\n",a);
getch(); 3/9/2025 6:46 AM
167
}
P P
R
E Nested if Statement R
E
P P
A
R
 It is always legal in C programming to nest if-else statements, i.e. you A
R
E can use one if or else if statement inside another if or else if E
D D
statement(s).
B B
Y Y

S  Syntax:
S
U if(boolean_expression1) U
L L
A { /* statement(s) will execute if the Boolean expression 1 is true */ A
V V
if(boolean_expression2)
N
E
{/* statement(s) will execute if the Boolean expression 2 is true */ N
E
P } P
A A
L } L

168 3/9/2025 6:46 AM


P P
R
E Example - 4 R
E
P P
A #include<stdio.h> A
R #include<conio.h> R
E E
D void main()
D
{
B int a=100, b=200; B
Y Y
if(a==100)
S { S
U if(b==200) U
L L
A
{
A
V printf("Value of a is 100 & b is 200\n"); V
}
N N
E
}
E
P printf("Exact value of a is: %d\n",a); P
A printf("Exact value of b is: %d\n",b); A
L
getch(); L

169
} 3/9/2025 6:46 AM
P P
R
E Loop Control Statements R
E
P P
A
R
 Loop control statements change execution from its normal sequence. A
R
E E
D D
 When execution leaves a scope, all automatic objects that were
B B
Y created in that scope are destroyed. Y

S S
U U
L  C supports the following control statements: L
A A
V  break statement
V
 continue statement
N N
E  goto statement E
P P
A A
L L

170 3/9/2025 6:46 AM


P P
R
E Break Statement R
E
P P
A
R
 The break statement in C programming has the following two usages: A
R
E  When a break statement is encountered inside a loop, the loop is E
D D
immediately terminated and the program control resumes at the next
B statement following the loop. B
Y Y

S S
U  It can be used to terminate a case in the switch statement.
U
L L
A A
V
 Syntax: V

N break; N
E E
P P
A A
L L

171 3/9/2025 6:46 AM


P P
R
E Example - 5 R
E
P P
A #include<stdio.h> A
R #include<conio.h> R
E E
D void main()
D
{
B int a=10; B
Y Y
while(a<20)
S { S
U printf("Value of a: %d\n",a); U
L L
A
a++;
A
V if(a>15) V
{
N N
E
break;
E
P } P
A } A
L
getch(); L

172
} 3/9/2025 6:46 AM
P P
R
E Continue Statement R
E
P P
A  The continue statement in C programming works somewhat like the break A
R R
E statement. E
D D

B  Instead of forcing termination, it forces the next iteration of the loop to B


Y take place, skipping any code in between. Y

S S
U U
L  For the for loop, continue statement causes the conditional test and
L
A increment portions of the loop to execute. A
V V

N  For the while and do…while loops, continue statement causes the program N
E E
P control to pass to the conditional tests. P
A A
L L
 Syntax:
173 3/9/2025 6:46 AM
continue;
P P
R
E Example - 6 R
E
P P
A #include<stdio.h> A
R R
E int main() E
D D
{
B int i=1; //initializing a local variable B
Y Y
//starting a loop from 1 to 10
S for(i=1;i<=10;i++) S
U
L
{ U
L
A if(i==5)
A
V { //if value of i is equal to 5, it will continue the loop V
continue;
N N
E }
E
P printf("%d \n",i); P
A } //end of for loop A
L L
return 0;
174 } 3/9/2025 6:46 AM
P P
R
E break vs. continue R
E
P P
A A
R R
break continue
E E
D The break statement is used to terminate the D
The continue statement is used to bypass the
control from the switch…case structure as well in
B execution of the further statements.
the loop. B
Y Y
When the break statement is encountered, it
When the continue statement is encountered, it
S terminates the execution of the entire loop or S
bypasses single pass of the loop.
U switch case. U
L L
It doesn’t bypass the current loop though it
A It is used to bypass current pass through a loop. A
V transfers the control out of the loop.
V
The loop doesn’t terminate when continue is
N The loop terminates when a break is encountered.
encountered. N
E E
P Syntax: break; Syntax: continue;
P
A A
L L

175 3/9/2025 6:46 AM


P P
R
E goto Statement R
E
P P
A
R
 To alter the normal sequence of program execution by A
R
E unconditionally transferring control to some other part of the E
D D
program.
B B
Y Y

S  General expression:
S
U  goto label: U
L L
A  Here, label is an identifier used to label the target statement to which the control A
V would be transferred. V

N N
E E
P  Generally the use of goto statement is avoided as it makes program P
A
L
illegible. A
L

176 3/9/2025 6:46 AM


P P
R
E goto Statement R
E
P P
A
R
 This statement is used in unique situations like: A
R
E  Branching around statements or group of statements under certain E
D D
conditions.
B B
Y Y
 Jumping to the end of a loop under certain conditions, thus bypassing
S S
U the remainder of the loop during current pass. U
L L
A A
V  Jumping completely out of the loop under certain conditions, V

N
terminating the execution of a loop.
N
E E
P P
A A
L L

177 3/9/2025 6:46 AM


P P
R
E Example - 7 R
E
P P
A /* Program to calculate the sum and average of positive numbers. If the user enters a negative number, the sum and A
R average are displayed */
R
E #include <stdio.h>
E
D int main() D
{
B int maxInput = 100,i; B
Y double number, average, sum = 0.0; Y
for (i = 1; i <= maxInput; ++i)
S { S
U printf("%d. Enter a number: ", i);
scanf("%lf", &number);
U
L L
if (number < 0.0)
A { A
V goto jump; // go to jump if the user enters a negative number V
}
N sum += number; N
E }
E
P jump:
P
A average = sum / (i - 1);
A
L printf("Sum = %.2f\n", sum);
L
printf("Average = %.2f", average);
return 0;
178 3/9/2025 6:46 AM
}
P P
R
E Switch Statement R
E
P P
A  Switch statement allows a program to select one statement for execution of a set of alternatives.
A
R R
E  Only one of the possible statements will be executed, the remaining statements will be skipped.
E
D D
 The multiple usage of if…else statement increases the complexity of the program, hard to read, and difficult to follow the
program.
B B
Y Y
 Switch statement removes these disadvantages by using a simple and straight forward approach.
S S
 Syntax:
U U
switch(expression)
L { case caseConstant1: L
A statement(s); A
V break; V
case caseConstant2:
N statement(s);
break;
N
E E
.
P . P
A . A
L default: L
statement;
} 3/9/2025 6:46 AM
179
P P
R
E Switch Statement R
E
P P
A  The following rules apply to a switch statement: A
R  The expression used in a switch statement must have an integral or enumerated type. R
E E
D  You can have any number of case statements within a switch. Each case is followed by the value to be D
compared to and a colon.
B B
Y  The caseConstant for a case must be the same data type as the variable in the switch, and it must be a Y
constant or a literal.
S S
U  When the variable being switched on is equal to a case, the statements following that case will execute U
L until a break statement is reached. L
A A
V  When a break statement is reached, the switch terminates, and the flow of control jumps to the next V
line following the switch statement.
N N
E  Not every case needs to contain a break. If no break appears, the flow of control will fall through to E
P subsequent cases until a break is reached. P
A A
L  A switch statement can have an optional default case, which must appear at the end of the switch. The L
default case can be used for performing a task when none of the cases is true. No break is needed in
the default case. 3/9/2025 6:46 AM
180
P P
R
E Example - 8 R
E
P P
#include<stdio.h>
A A
#include<conio.h>
R void main() R
E { E
D int choice;
D
LOOP:
printf("Select 1 for file, 2 for Edit, 3 for Save\n");
B printf("1==>File\n2==>Edit\n3==>Save\n"); B
Y scanf("%d",&choice); Y
switch(choice)
S {
S
case 1:
U U
printf("\nYou have chosen File Menu Item\n");
L break; L
A case 2: A
V printf("\nYou have chosen Edit Menu Item\n");
V
break;
case 3:
N printf("\nYou have chosen Save Menu Item\n"); N
E break; E
P default:
P
A printf("\nINVALID OPTION CHOSED\n");
A
goto LOOP;
L } L
getch();
181 } 3/9/2025 6:46 AM
P P
R
E Loop or Repeating Construct R
E
P P
A
R
 You may encounter situations, when a block of code needs to be A
R
E executed several number of times. E
D D

B B
Y  Programming languages provide various control structures that allow Y

S for more complicated execution paths. S


U U
L L
A
V
 A loop statement allows us to execute a statement or group of A
V
statements multiple times.
N N
E E
P P
A A
L L

182 3/9/2025 6:46 AM


P P
R
E for Loop R
E
P P
A  A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a A
R specific number of times.
R
E E
D  The syntax for a for loop in C programming language is: D
for(init ; condition ; increment)
B { statement(s);
B
Y }
Y

S  The init step is executed first, and only once. This step allows you to declare and initialize any loop control S
U variables.
U
L L
A  Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop A
V does not execute and the flow of control jumps to the next statement just after the for loop. V

N  After the body of the for loop executes, the flow of control jumps back up to the increment statement. This N
E statement allows you to update any loop control variables. This statement can be left blank, as long as a E
P semicolon appears after the condition.
P
A A
L  The condition is now evaluated again. If it is true, the loop executes and the process repeats itself. After the L
condition becomes false, the for loop terminates.
183 3/9/2025 6:46 AM
P P
R
E Example - 9 R
E
P P
A
R
#include<stdio.h> A
R
E
D
#include<conio.h> E
D

B
void main()
B
Y { Y

S int a; S
U U
L for(a=10;a<=20;a++) L
A A
V { V

N printf("Value of a is %d\n",a); N
E E
P } P
A A
L getch(); L

184 } 3/9/2025 6:46 AM


P P
R
E while Loop R
E
P P
A  A while loop in C programming repeatedly executes a target statement as A
R R
E long as a given condition is true. E
D D

B  The syntax for a while loop in C programming language is: B


Y
while(condition) Y

S { statement(s); S
U } U
L L
A A
V V
 Statement(s) may be a single statement or a block of statements.
N N
E E
P  The condition may be any expression, and true is any nonzero value. P
A A
L L
 The loop iterates while the condition is true.
185 3/9/2025 6:46 AM
P P
R
E Example - 10 R
E
P P
A
R
#include<stdio.h> A
R
E #include<conio.h> E
D D
void main()
B B
Y { Y

S
int a=10;
S
U while(a<20) U
L L
A { A
V V
printf("Value of a is %d\n",a);
N
E
a++; N
E
P
A
} P
A
L getch(); L

186 } 3/9/2025 6:46 AM


P P
R
E do…while Loop R
E
P P
A  Unlike for and while loops, which test the loop condition at the top of the A
R R
E loop, the do…while loop in C programming checks its condition at the E
D bottom of the loop. D

B B
Y  A do…while loop is similar to a while loop, except the fact that it is Y

S guaranteed to execute at least one time. S


U U
L L
A  The syntax for a do…while loop in C programming language is: A
V
do V

N { statement(s); N
E } while(condition); E
P P
A A
L L
 Conditional expression appears at the end of the loop, so the statement(s)
187 in the loop executes once before the condition is tested. 3/9/2025 6:46 AM
P P
R
E Example - 11 R
E
P P
A
R
#include<stdio.h> A
R
E #include<conio.h> E
D D
void main()
B B
Y { Y

S
int a=10;
S
U do U
L L
A { A
V V
printf("Value of a is %d\n",a);
N
E
a++; N
E
P
A
} while(a<20); P
A
L getch(); L

188 } 3/9/2025 6:46 AM


P P
R
E for vs. while R
E
P P
A A
R R
for while
E E
D It is definite loop It may be definite and may not be definite D

B
The loop expression is scattered throughout the
The loop expression is within a one block B
Y program
Y
It contains three expressions It contains only one expression
S S
U Syntax: Syntax:
U
L for(init; condition; increment/decrement) while(test expression) L
A { statement(s); } { body of loop } A
V V

N N
E E
P P
A A
L L

189 3/9/2025 6:46 AM


P P
R
E while vs. do…while R
E
P P
A A
R R
while do…while
E E
D do…while loop is exit-controlled loop. i.e. the D
while loop is entry-controlled loop. i.e. test
body of the loop is executed first without checking
B condition is evaluated first and body of loop is
condition and at the end of body of loop, the B
Y executed only if this test is true. Y
condition is evaluated for repetition of next time.
S The body of the loop may not be executed at all if S
U the condition is not satisfied at the very fist The body of loop is always executed at least once. U
L attempt. L
A A
V Loop is not terminated with semicolon. Loop is terminated with semicolon.
V
Syntax:
N Syntax:
do{ N
E while(test expression) E
Body of loop
P { body of loop } P
A
}while(test expression);
A
L L

190 3/9/2025 6:46 AM


P P
R
E Nested loops in C R
E
P P
A
R
 C programming allows to use one loop inside another loop. A
R
E E
D D
 The inner loop is said to be nested within the outer loop.
B B
Y  Nested for loop Y

S  Nested while loop S


U
L
 Nested do…while loop U
L
A A
V V

N N
E E
P P
A A
L L

191 3/9/2025 6:46 AM


P P
R
E Nested for loop R
E
P P
A
R
for(init; condition; increment) A
R
E
D
{ for(init; condition; increment) E
D

B
{ statement(s);
B
Y } Y

S statement(s); S
U U
L } L
A A
V V

N N
E E
P P
A A
L L

192 3/9/2025 6:46 AM


P P
R
E Nested for loop R
E
P P
A #include<stdio.h> A
R R
E #include<conio.h> E
D D
void main()
B { B
Y Y
int i, j;
S S
U for(i=1; i<=9; i++)
U
L { L
A A
V for(j=1; j<=i; j++) V

N printf("%d",i);
N
E printf("\n"); E
P P
A } A
L
getch(); L

193 } 3/9/2025 6:46 AM


P P
R
E Nested while loop R
E
P P
A
R
while(condition) A
R
E
D
{ while(condition) E
D

B
{ statement(s);
B
Y } Y

S statement(s); S
U U
L } L
A A
V V

N N
E E
P P
A A
L L

194 3/9/2025 6:46 AM


P P
R
E Nested while loop R
E
P P
A #include <stdio.h> A
R R
E
int main()
E
D { D
int a = 1, b = 1;
B while(a <= 5) B
Y Y
{
S b = 1;
S
U while(b <= 5)
U
L { L
A printf("%d ", b); A
V b++; V
}
N printf("\n"); N
E a++; E
P } P
A A
return 0;
L L
}
195 3/9/2025 6:46 AM
P P
R
E Nested do…while loop R
E
P P
A
R
do A
R
E
D
{ statement(s); E
D

B
do
B
Y { statement(s); Y

S } while(condition); S
U U
L }while(condition); L
A A
V V

N N
E E
P P
A A
L L

196 3/9/2025 6:46 AM


P P
R
E Nested do while loop R
E
P P
A #include <stdio.h> A
R R
E int main() E
D D
{
B do B
Y Y
{
S printf("I'm from outer do-while loop "); S
U do U
L L
{
A A
V printf("\nI'm from inner do-while loop ");
V
}
N while(1 > 10); N
E E
P }
P
A while(2 > 10); A
L L
return 0;
197 } 3/9/2025 6:46 AM
P P
R
E exit function R
E
P P
A  The C library function void exit(int status) terminates the A
R R
E calling process immediately. E
D D

B #include<stdio.h> B
Y
#include<conio.h> Y

S void main() S
U U
L { L
A printf("Start of the program . . . \n"); A
V V
printf("Exiting the program . . . \n");
N exit(0); N
E E
P printf("End of the program . . . \n"); P
A
L
getch(); A
L
}
198 3/9/2025 6:46 AM
P P
R R
E E
P P
A A
R R
E E
D D

B B
Y Y

S
END OF UNIT FIVE
S
U U
L L
A A
V V

N N
E E
P P
A A
L L

199 3/9/2025 6:46 AM


P P
R
E Syllabus R
E
P P
A  UNIT 6:Arrays A
R o Introduction to Array R
E E
D D
o Types of Array (Single, Dimensional, and Multidimensional)
B B
Y o Declaration and Memory Representation of Array Y

S S
U o Initialization of Array
U
L L
A o Character Array and Strings A
V V
o Reading andWriting Strings
N N
E E
P o Null Character P
A A
L o String Library Functions (string length, string copy, string concatenation, string L
compare)
200 3/9/2025 6:46 AM
P P
R R
E E
P P
A A
R R

UNIT 6
E E
D D

B B
Y Y

S ARRAYS S
U U
L L
A A
V V

N N
E E
P P
A A
L L

201 3/9/2025 6:46 AM


P P
R
E Arrays R
E
P P
A
R
 An array is used to store a collection of data, but it is often more A
R
E useful to think of an array as a collection of variables of the same E
D D
type.
B B
Y Y

S  Array is a data structure that store a number of data items as a single


S
U entity (object). U
L L
A A
V V
 The individual data items are called elements and all of them have
N N
E same data types. E
P P
A A
L
 Array is used when multiple data items have common characteristics. L

202 3/9/2025 6:46 AM


P P
R
E Arrays R
E
P P
A
R
 Instead of declaring, number0, number1, …, and number99; we A
R
E declare one array variable such as numbers and use numbers[0], E
D D
numbers[1], …, and numbers[99] to represent individual variables.
B B
Y Y

S  A specific element in an array is accessed by an index.


S
U U
L L
A
V
 All arrays consist of contiguous memory locations. A
V

N N
E  The lowest address corresponds to the first element and the highest E
P P
A address to the last element. A
L L

203 3/9/2025 6:46 AM


P P
R
E Defining an Array R
E
P P
A  An array is defined as following: A
R <storage_class> <type_of_array> <name_of_array> [<number of elements in array>]; R
E int num[35]; //an integer array with 35 elements E
D D
char ch[5]; //an array of five characters
B B
Y  storage_class: it may be auto, register, static, and extern (optional). Y

S S
U  type_of_array: it is the type of elements that an array stores. For U
L L
A example:‘char’,‘int’, etc.
A
V V

N  name_of_array: this is the name that is given to array. At least the


N
E name should be in context with what is being stored in the array. E
P P
A A
L  [number of elements]: this value in subscripts [] indicates the number L
of elements the array stores.
204 3/9/2025 6:46 AM
P P
R
E Initializing an Array R
E
P P
A  You can initialize an array in C either one by one or using a single A
R R
E statement as follows: E
D D
double balance[5] = {1234.5, 6.7, 8.9, 1.0, 11.12};
B B
Y Y
 The number of values between braces {} cannot be larger than the number
S S
U of elements that we declare for the array between square brackets [].
U
L L
A A
V  If you omit the size of the array, an array just big enough to hold the V

N initialization is created.Therefore, if you write N


E double balance[] = {1234.5, 6.7, 8.9, 1.0, 11.12}; E
P P
A A
L L
 The above statement shows that the 5th element in the array has a value of
205 11.12 (i.e. balance[4]=11.12). 3/9/2025 6:46 AM
P P
R
E Initializing an Array R
E
P P
A
R
 All arrays have 0 as the index of their first element which is also A
R
E called the base index and the last index of an array will be total size E
D D
of the array minus 1.
B B
Y Y

S  Shown below is the pictorial representation of the array we discussed:


S
U double balance[5] = {1234.5, 6.7, 8.9, 1.0, 11.12}; U
L L
A A
V V
0 1 2 3 4
N
E
balance 1234.5 6.7 8.9 1.0 11.12 N
E
P P
A A
L L

206 3/9/2025 6:46 AM


P P
R
E Accessing an Array Elements R
E
P P
A
R
 An element is accessed by indexing the array name. A
R
E E
D D
 This is done by placing the index of the element within a square
B B
Y brackets after the name of the array. For example: Y

S double num = balance[4]; S


U U
L L
A
V
 This above statement will take the 5th element from an array and A
V
assign the value to num variable.
N N
E E
P P
A A
L L

207 3/9/2025 6:46 AM


P P
R
E Example – 1 R
E
P P
A
R
#include<stdio.h> A
R
E
D
#include<conio.h> E
D

B
void main()
B
Y { Y

S double balance[5] = {1234.5, 6.7, 8.9, 1.0, 11.12}; S


U U
L double num=balance[4]; L
A A
V printf("%d",num); V

N getch(); N
E E
P } P
A A
L L

208 3/9/2025 6:46 AM


P P
R
E Example – 2 (Sorting) R
E
P P
#include<stdio.h>
A #include<conio.h> A
R void main()
R
E {
E
int num[30],i,j,n,temp;
D printf("How many numbers are there?\n"); D
scanf("%d",&n);
printf("Enter %d numbers:\n",n);
B for(i=0;i<n;i++) B
Y {
Y
scanf("%d",&num[i]);
}
S for(i=0;i<n-1;++i) S
U {
U
for(j=i+1;j<n;j++)
L { L
A if(num[i]>num[j])
A
V {
V
temp=num[i];
num[i]=num[j];

N num[j]=temp;
N
}
E } E
P }
P
A printf("The numbers are: ");
A
for(i=0;i<n;i++)
L { L
printf("%d\t",num[i]);
}
209 getch(); 3/9/2025 6:46 AM
}
P P
R
E Characteristics of an Array R
E
P P
A
R
 The declaration of 𝑖𝑛𝑡 𝑎[5] is nothing but creation of 5 variables A
R
E of integer type in the memory. E
D D

B B
Y  All the elements of an array share the same name, distinguished from Y

S one another by element number or array index. S


U U
L L
A
V
 Any element can be modified separately without disturbing another A
V
element.
N N
E E
P P
A  Element basis operation should be carried out rather than taking as a A
L
whole. L

210 3/9/2025 6:46 AM


P P
R
E Types of Array: Single Dimensional R
E
P P
A
R
 A list of items can be given one variable name using only one A
R
E subscript (or dimension or index) and such a variable is called a E
D D
single-subscripted variable or a one-dimensional array.
B B
Y Y

S  The value of the single subscript or index from 0 to 𝑛 − 1 refers to


S
U the individual array elements; where 𝑛 is the size of the array. U
L L
A A
V V
 For example: The declaration 𝑖𝑛𝑡 𝑎[5]; is a 1-D array of integer
N N
E data type with 5 elements: 𝑎 0 , 𝑎 1 , 𝑎 2 , 𝑎 3 , & 𝑎[4]. E
P P
A A
L L

211 3/9/2025 6:46 AM


P P
R
E Types of Array: Multi Dimensional R
E
P P
A
R
 Those arrays having more than one dimension are called multi A
R
E dimensional arrays. E
D D

B B
Y  Multi-dimensional arrays are defined in the same way as one Y

S dimensional arrays, except that a separate pair of square brackets is S


U required for each subscript or dimension or index. U
L L
A A
V V
 Thus a 2-D array requires two pairs of square brackets (also called as
N N
E matrix); a 3-D array requires three pairs of square brackets; and so E
P
A
on. P
A
L L

212 3/9/2025 6:46 AM


P P
R
E Types of Array: Multi Dimensional R
E
P P
A
R
 Syntax for defining multidimensional array is: A
R
E storage_class data_type array_name [dim1] [dim2] … [dimN]; E
D D

B B
Y  Here, dim1, dim2, … , dimN are positive valued integer expressions Y

S that indicate the number of array elements associated with each S


U
L
subscript. U
L
A A
V V
 Thus, total number of elements is equal to dim1*dim2*…*dimN
N N
E E
P P
A  For example: 𝑖𝑛𝑡 𝑠𝑢𝑟𝑣𝑒𝑦 3 5 [12] A
L L

213 3/9/2025 6:46 AM


P P
R
E 2 Dimensional Array R
E
P P
A
R
 𝑚 ∗ 𝑛 – Two dimensional array can be thought as tables of A
R
E values having 𝑚 rows and 𝑛 columns. E
D D

B B
Y  For example: 𝑖𝑛𝑡 𝑥 3 3 can be shown as follows: Y

S S
𝐶𝑜𝑙 1 𝐶𝑜𝑙 2 𝐶𝑜𝑙 3
U U
L 𝑅𝑜𝑤 1 𝑥0 0 𝑥0 1 𝑥0 2 L
A A
𝑅𝑜𝑤 2 𝑥1 0 𝑥1 1 𝑥1 2
V V
𝑅𝑜𝑤 3 𝑥2 0 𝑥2 1 𝑥2 2
N N
E E
P P
A A
L L

214 3/9/2025 6:46 AM


P P
R
E Declaration of 2 Dimensional Array R
E
P P
A
R
 Like one dimensional array, two dimensional arrays must also de A
R
E declared before using it. E
D D

B B
Y  Syntax: Y

S storage_class data_type array_name [row_size] [col_size]; S


U U
L L
A
V
 For example: A
V
𝑖𝑛𝑡 𝑚𝑎𝑡𝑟𝑖𝑥 2 3 ; //matrix having 2 rows and 3 columns
N N
E 𝑓𝑙𝑜𝑎𝑡 𝑚 10 20 ; E
P P
A 𝑐ℎ𝑎𝑟 𝑠𝑡𝑢𝑑𝑒𝑛𝑡𝑠 10 15 ; A
L L

215 3/9/2025 6:46 AM


P P
R
E Initialization of 2–D Array R
E
P P
A
R
 For example: A
R
E
D
𝑖𝑛𝑡 𝑚𝑎𝑡𝑟𝑖𝑥 2 3 = { 2,4,6 , 8,10,12 }; E
D

B B
Y 𝑖𝑠 𝑒𝑞𝑢𝑖𝑣𝑎𝑙𝑒𝑛𝑡 𝑡𝑜 Y

S S
U U
L L
A 𝑚𝑎𝑡𝑟𝑖𝑥 0 0 = 2; 𝑚𝑎𝑡𝑟𝑖𝑥 1 0 = 8; A
V V

N 𝑚𝑎𝑡𝑟𝑖𝑥 0 1 = 4; 𝑚𝑎𝑡𝑟𝑖𝑥 1 1 = 10; N


E E
P P
A A
L 𝑚𝑎𝑡𝑟𝑖𝑥 0 2 = 6; 𝑚𝑎𝑡𝑟𝑖𝑥 1 2 = 12; L

216 3/9/2025 6:46 AM


P P
R
E Accessing of 2–D Array Elements R
E
P P
A
R
 In 2-D array, the first dimension specifies number of rows and second A
R
E specifies columns. E
D D

B B
Y  A row is 1-D array. 2-D array contains multiple rows (i.e. 1-D Y

S arrays). S
U U
L L
A
V
 2-D array is traversed row by row (i.e. every column elements in first A
V
row are traversed first and then column elements of second row are
N N
E traversed and so on). E
P P
A A
L
 Nested loop is used to traverse the 2-D array. L

217 3/9/2025 6:46 AM


P P
R
E Accessing of 2–D Array Elements R
E
P P
A
R
 Let us consider a 2-D array 𝑚𝑎𝑟𝑘𝑠 of size 4 ∗ 3 (i.e. matrix A
R
E having 4 rows and 3 columns). E
D D

B 35 10 11
B
Y 34 90 76 Y

S 13 8 5
S
U 76 4 1 U
L L
A A
V  The array is traversed in the following order: V
35 → 10 → 11 → 34 → 90 → 76 → 13 → 8 → 5 → 76 → 4 → 1
N N
E  i.e. 𝑚𝑎𝑟𝑘𝑠 0 0 → 𝑚𝑎𝑟𝑘𝑠 0 1 → 𝑚𝑎𝑟𝑘𝑠 0 2 → E
P P
A 𝑚𝑎𝑟𝑘𝑠 1 0 → 𝑚𝑎𝑟𝑘𝑠 1 1 → 𝑚𝑎𝑟𝑘𝑠 1 2 → A
L 𝑚𝑎𝑟𝑘𝑠 2 0 → 𝑚𝑎𝑟𝑘𝑠 2 1 → 𝑚𝑎𝑟𝑘𝑠 2 2 → L
𝑚𝑎𝑟𝑘𝑠 3 0 → 𝑚𝑎𝑟𝑘𝑠 3 1 → 𝑚𝑎𝑟𝑘𝑠 3 2 3/9/2025 6:46 AM
218
P P
R
E Example – 3 R
E
P P
A #include<stdio.h>
A
#include<conio.h>
R R
void main()
E E
{
D int matrix[2][3],i,j; D
for(i=0;i<2;i++)
B { B
Y for(j=0;j<3;j++) Y
{
S printf("Enter Matrix [%d][%d]:",i,j); S
U scanf("%d",&matrix[i][j]);
U
L }
L
A }
printf("The entered matrix is:\n");
A
V V
for(i=0;i<2;i++)
{
N for(j=0;j<3;j++) N
E { E
P printf("%d\t",matrix[i][j]); P
A } A
L printf("\n"); L
}
getch(); 3/9/2025 6:46 AM
219
}
P P
R
E Addition & Subtraction of Matrix - WAP R
E
P P
A A
R R
E E
D D

B B
Y Y

S S
U U
L L
A A
V V

N N
E E
P P
A A
L L

220 3/9/2025 6:46 AM


P P
R
E Bubble Sort R
E
P P
A
R
for(i=0;i<n-1;i++) A
R
E { E
D D
for(j=0;j<n-1-i;j++)
B B
Y { Y

S
if(num[j]>num[j+1])
S
U { U
L L
A temp=num[j]; A
V V
num[j]=num[j+1];
N
E
num[j+1]=temp; N
E
P
A
} P
A
L } L

221 } 3/9/2025 6:46 AM


P P
R
E Selection Sort R
E
P P
A for(i = 0; i < n - 1; i++) A
R R
E { E
D min=i; D

B for(j = i + 1; j < n; j++)


B
Y { Y
if(a[min] > a[j])
S S
U min=j;
U
L } L
A A
V
if(min != i)
V
{
N temp=a[i]; N
E E
a[i]=a[min];
P P
A a[min]=temp;
A
L } L
} 3/9/2025 6:46 AM
222
P P
R
E Sequential (Linear) Search R
E
P P
A #include <stdio.h> for(i=0; i<n; i++) A
R R
E int main() { E
D { if(array[i]==element) D

B int array[100], element, i, n, found=0; { B


Y printf("Found at location %d", i); Y
printf("Enter number of elements in
S array\n"); found=1;
S
U scanf("%d", &n); break; U
L L
A printf("Enter %d integer(s)\n", n); }
A
V
for (i = 0; i < n; i++) } V

N scanf("%d", &array[i]); if(!found)


N
E printf("Enter a number to search\n"); printf("Element not found"); E
P P
A scanf("%d", &element); return 0;
A
L } L

223 3/9/2025 6:46 AM


P P
R
E Arrays and Strings R
E
P P
A  In C programming, an array of character are called strings. A
R R
E E
D D
 A string is terminated by null character \0.
B For example:“C Strings” B
Y Y
Here, “C Strings” is a string. When compiler encounters strings, it appends null
S character at the end of string. S
U U
L C S t r i n g s \0 L
A A
V
 Strings are actually one-dimensional array of characters terminated by a V

N null character. N
E E
P P
A  If you follow the rule of array initialization, then you can write the above A
L
statement as follow: L

224 char greeting[]=“Hello”; 3/9/2025 6:46 AM


P P
R
E Arrays and Strings R
E
P P
A
R
char greeting[]=“Hello”; A
R
E
D
 Following is the memory presentation of the above defined string in E
D
C:
B B
Y Index 0 1 2 3 4 5 Y
Variable H e l l o \0
S S
U Address 0x98765 0x98764 0x98763 0x98762 0x98761 0x98760 U
L L
A A
V V
 Actually, we do not place the null character at the end of a string
N N
E constant but the C compiler automatically places the \0 at the end E
P of the string when it initializes the array. P
A A
L L

225 3/9/2025 6:46 AM


P P
R
E Initialization of Strings R
E
P P
A
R
 In C, string can be initialized in different number of ways. A
R
E 𝑐ℎ𝑎𝑟 𝑐 = "hello"; E
D D
OR
B 𝑐ℎ𝑎𝑟 𝑐 = {′h′, ′e′, ′l′, ′l′, ′o′, ′\0′}; B
Y Y

S S
U c[0] c[1] c[2] c[3] c[4] c[5] U
L L
h e l l o \0
A A
V V

N N
E E
P P
A A
L L

226 3/9/2025 6:46 AM


P P
R
E Declaration of Strings R
E
P P
A
R
 Strings are declared in C in similar manner as arrays. A
R
E E
D D
 Only difference is that, strings are of char type.
B B
Y 𝑐ℎ𝑎𝑟 𝑐 5 ; Y

S c[0] c[1] c[2] c[3] c[4]


S
U h e l l o U
L L
A A
V V

N  Strings can also be declared using pointer.


N
E 𝑐ℎ𝑎𝑟 ∗ 𝑝; E
P P
A A
L L

227 3/9/2025 6:46 AM


P P
R
E Example – 4 R
E
P P
A
R
#include<stdio.h> A
R
E
D
#include<conio.h> E
D

B
void main()
B
Y { Y

S char greeting[]={'H','e','l','l','o','\0'}; S
U U
L printf("Greeting message is %s\n",greeting); L
A A
V char greetings[]="Hello"; V

N printf("Greeting message is %s\n",greetings); N


E E
P getch(); P
A A
L } L

228 3/9/2025 6:46 AM


P P
R
E Example – 5 R
E
P P
A
R
#include<stdio.h> A
R
E
D
#include<conio.h> E
D

B
void main()
B
Y { Y

S char name[20]; S
U U
L printf("Enter your name:"); L
A A
V scanf("%s",&name); V

N printf("Your name is %s",name); N


E E
P getch(); P
A A
L } L

229 3/9/2025 6:46 AM


P P
R
E Example – 6 R
E
P P
A #include<stdio.h> A
R #include<conio.h> R
E void main() E
D D
{
B char name[30],ch; B
Y int i=0; Y
printf("Enter your name:");
S S
while(ch!='\n')
U U
L {
L
A ch=getchar(); A
V name[i]=ch; V
i++;
N N
}
E E
P name[i]='\0';
P
A printf("Your name is %s",name); A
L getch(); L
}
230 3/9/2025 6:46 AM
P P
R
E gets() and puts() function R
E
P P
A
R
 The gets() function is used to read a string of text, containing A
R
E whitespaces, until a newline character is encountered. E
D D
 Syntax: 𝑔𝑒𝑡𝑠ሺ𝑣𝑎𝑟𝑖𝑎𝑏𝑙𝑒_𝑛𝑎𝑚𝑒ሻ;
B B
Y Y

S  It takes a string from user and stores in a string variable S


U
L
𝑣𝑎𝑟𝑖𝑎𝑏𝑙𝑒_𝑛𝑎𝑚𝑒 U
L
A A
V V
 The puts() function is used to display the string onto the screen.
N N
E  Syntax: 𝑝𝑢𝑡𝑠ሺ𝑣𝑎𝑟𝑖𝑎𝑏𝑙𝑒_𝑛𝑎𝑚𝑒ሻ; E
P P
A A
L L

231 3/9/2025 6:46 AM


P P
R
E Example – 7 R
E
P P
A
R
#include<stdio.h> A
R
E
D
#include<conio.h> E
D

B
void main()
B
Y { Y

S char name[30]; S
U U
L puts("Enter your name: "); L
A A
V gets(name); V

N printf("Your name is "); N


E E
P puts(name); P
A A
L getch(); L

232 } 3/9/2025 6:46 AM


P P
R
E String Library Functions R
E
P P
A
R
 The header file <string.h> contains some string manipulation A
R
E functions. E
D D
String Functions Its use
B strlen Returns number of characters in string B
Y Y
strlwr Converts all the characters in the string into lower case characters
S strcat Adds one string at the end of another string S
U U
L strcpy Copies a string into another L
A strcmp Compares two strings and returns zero if both are equal A
V V
strdup Duplicates a string
N strstr Finds the first occurrence of given string in another string N
E E
P strchr Finds the first occurrence of given character in a string P
A strset Sets all the characters of string to given character or symbol A
L L
strrev Reverse a string
233 3/9/2025 6:46 AM
P P
R
E Example – 8 R
E
P P
A #include<stdio.h>
A
R #include<conio.h>
R
E void main()
E
D {
D
char str1[20]="Hello ";
B char str2[20]="world!"; B
Y char str3[20]; Y
int len;
S strcpy(str3,str1); S
U printf("Displaying str3: %s\n",str3); U
L /* L
A strcpy(str3,strcat(str1,str2)); A
V printf("Displaying str3: %s\n",str3); V
*/
N /* N
E len=strlen(str3); E
P printf("Length of str3 is %d",len); P
A */ A
L getch(); L
}
234 3/9/2025 6:46 AM
P P
R
E Transpose of a Matrix R
E
P P
A #include <stdio.h> /*finding transpose matrix*/ A
#include <conio.h>
R for(i=0;i<M;i++) R
#define M 3
E { E
#define N 3
D D
void main() for(j=0;j<N;j++)
{ {
B int matrix[M][N],transpose[N][M],i,j; B
transpose[j][i]=matrix[i][j];
Y printf("\nEnter the elements of matrix:\n"); Y
}
for(i=0;i<M;i++)
S { }
S
U for(j=0;j<N;j++) printf("\nThe transpose matrix is:\n"); U
{
L scanf("%d", &matrix[i][j]); for(i=0;i<M;i++) L
A }
{ A
V }
for(j=0;j<N;j++) V
printf("\nThe matrix to be transposed is:\n");
for(i=0;i<M;i++) {
N N
{ printf("%d\t",transpose[i][j]);
E E
for(j=0;j<N;j++) }
P { P
printf("\n");
A printf("%d\t", matrix[i][j]);
A
L } }
printf("\n"); L
}
getch();
235 } 3/9/2025 6:46 AM
P P
R
E Multiplication of a Matrix R
E
P P
A #include <stdio.h> A
R #include <conio.h> R
E E
D #include <stdlib.h>
D
void main()
B { B
Y Y
int matrix1[10][10],matrix2[10][10],i,j,k,product[10][10],M,N,P,Q;
S int row_mul_col=0; S
U printf("\nEnter order of first matrix (less than 10*10):\n"); U
L L
A
scanf("%d %d",&M,&N);
A
V printf("\nEnter order of second matrix (less than 10*10):\n"); V
scanf("%d %d",&P,&Q);
N N
E
if(N!=P)
E
P { P
A printf("\nThe matrices are unsuitable for multiplication.\n"); A
L getch(); L
exit(0);
236 3/9/2025 6:46 AM
}
P P
R
E Multiplication of a Matrix R
E
P P
A printf("\nEnter the elements of first matrix:\n"); A
R for(i=0;i<M;i++) R
E E
D {
D
for(j=0;j<N;j++)
B { B
Y scanf("%d",&matrix1[i][j]); Y
}
S S
U }
U
L printf("\nThe first matrix is:\n"); L
A for(i=0;i<M;i++) A
V V
{
N for(j=0;j<N;j++)
N
E { E
P printf("%d\t",matrix1[i][j]); P
A } A
L printf("\n"); L
} 3/9/2025 6:46 AM
237
P P
R
E Multiplication of a Matrix R
E
P P
A printf("\nEnter the elements of second matrix:\n"); A
R for(i=0;i<P;i++) R
E E
D {
D
for(j=0;j<Q;j++)
B { B
Y scanf("%d",&matrix2[i][j]); Y
}
S S
U }
U
L printf("\nThe second matrix is:\n"); L
A for(i=0;i<P;i++) A
V V
{
N for(j=0;j<Q;j++)
N
E { E
P printf("%d\t",matrix2[i][j]); P
A } A
L printf("\n"); L
} 3/9/2025 6:46 AM
238
P P
R
E Multiplication of a Matrix R
E
P P
A
R
/*multiply two matrices*/ A
R
E
D
for(i=0;i<M;i++) //first row E
D

B
{
B
Y for(j=0;j<Q;j++) //second col Y

S { S
U for(k=0;k<N;k++) //first col U
L L
A { A
V row_mul_col += matrix1[i][k]*matrix2[k][j]; V

N }
N
E product[i][j]=row_mul_col; E
P P
A row_mul_col=0;
A
L } L

239 } 3/9/2025 6:46 AM


P P
R
E Multiplication of a Matrix R
E
P P
A
R
printf("\nThe matrix after multiplication is:\n"); A
R
E
D
for(i=0;i<M;i++) E
D

B
{
B
Y for(j=0;j<Q;j++) Y

S { S
U
L
printf("%d\t",product[i][j]); U
L
A } A
V V
printf("\n");
N N
E } E
P P
A getch(); A
L L
}
240 3/9/2025 6:46 AM
P P
R R
E E
P P
A A
R R
E E
D D

B B
Y Y

S
END OF UNIT SIX
S
U U
L L
A A
V V

N N
E E
P P
A A
L L

241 3/9/2025 6:46 AM


P P
R
E Syllabus R
E
P P
A  UNIT 7: Functions A
R o Library Functions R
E E
D D
o User defined functions
B B
Y o Function prototype, Function call, and Function Definition Y

S S
U o Nested and Recursive Function
U
L L
A o Function Arguments and Return Types A
V V
o Passing Arrays to Function
N N
E E
P o Passing Strings to Function P
A A
L o Passing Arguments byValue, Passing Arguments by Address L

242 3/9/2025 6:46 AM


o Scope visibility and lifetime of a variable, Local and GlobalVariable
P P
R R
E E
P P
A A
R R

UNIT 7
E E
D D

B B
Y Y

S FUNCTIONS S
U U
L L
A A
V V

N N
E E
P P
A A
L L

243 3/9/2025 6:46 AM


P P
R
E Function R
E
P P
A
R
 A function is a group of statements that together perform a task. A
R
E E
D D
 Every C program has at least one function, which is main(), and all
B B
Y the most trivial programs can define additional functions. Y

S S
U U
L  You can divide up your code into separate functions. L
A A
V V

N  How you divide up your code among different function is up to you – N


E
P
but logically the division should be such that each function performs E
P
A a specific task. A
L L

244 3/9/2025 6:46 AM


P P
R
E Function R
E
P P
A  Suppose a program where a set of operations has to be repeated often, A
R though not continuously. R
E E
D D
 Loops seems like a better option.
B B
Y Y
 Instead of inserting the program statements for these operations at so many
S S
U places, a separate program segment is written and compiled it separately. U
L L
A A
V  As many times as it is needed, the segment program is called.
V

N N
E  The separate program segment is called a function.
E
P P
A A
L  The C functions can be classified into two categories:
L
 User Defined Function
245  Library Function 3/9/2025 6:46 AM
P P
R
E Advantages of Function R
E
P P
A  Manageability A
R R
E
 Easier to write and keep track of.
E
D  Easier to understand and maintain. D

B B
Y  Code Reusability Y
 Can be used multiple times.
S S
U U
L  Non-redundant programming L
A
 Same function can be called when needed. A
V V

N
E
 Logical Clarity N
E
P  Reduced number of code in main function. P
A A
L L
 Easy to divide work
246  Large work can be divided by writing different functions. 3/9/2025 6:46 AM
P P
R
E Library Functions R
E
P P
A  Also known as built-in function. A
R R
E E
D D
 These are the functions which are already written, compiled and
B
Y
placed in C library and they are not required to be written by a B
Y
programmer.
S S
U U
L  The functions name, its return type, their argument number and types L
A A
V have been already defined. V

N N
E
 We can use these functions as required. E
P P
A A
L L
 For example: printf(), scanf(), sqrt(), getch(), etc.
247 3/9/2025 6:46 AM
P P
R
E User – Defined Functions R
E
P P
A  These are functions which are defined by user at the time of writing a A
R R
E program. E
D D

B  The user has choice to choose its name, return type, arguments and B
Y Y
their types.
S S
U U
L  The job of each user-defined functions is as defined by the user. L
A A
V V

N  A complex C problems can be divided into a number of user-defined N


E
P
functions. E
P
A A
L L
 The function main() is an user-defined function.
248 3/9/2025 6:46 AM
P P
R
E main() Function R
E
P P
A A
R R
E
D
 The function main() is an user defined function except that the E
D
name of function is defined or fixed by the language.
B B
Y Y

S  The return type, argument and body of the function are defined by S
U
L
the programmer as required. U
L
A A
V V

N
 The function is executed first, when the program starts execution.
N
E E
P P
A A
L L

249 3/9/2025 6:46 AM


P P
R
E main() Function R
E
P P
A A
R R
E
D
 The function main() has two arguments that traditionally are E
D
called argc and argv and return a signed integer.
B B
Y Y
Argument Name Description
S S
U argc Argument count Length of the argument vector U
L L
A argv Argument vector Array of character pointers A
V V

N N
E E
P P
A A
L L

250 3/9/2025 6:46 AM


P P
R
E Components associated with Function R
E
P P
A A
R R
E
D
 Function Definition E
D

B B
Y  Function Declaration (or Prototype) Y

S S
U U
L  Return Statement L
A A
V V

N  Accessing/Calling a function N
E E
P P
A A
L L

251 3/9/2025 6:46 AM


P P
R
E Function Definition R
E
P P
A  The collection of program statements that describes the specific task A
R R
E to be done by the function is called function definition. E
D D

B  It consists of function header, which defines functions name, its B


Y Y
return type and its argument list and a function body, which
S is a block of code enclosed in parenthesis. S
U U
L L
A A
V  Syntax: V

N
return_type function_name(data_type variable1, data_type variable2, …)
N
E { ... E
P
A
statements; P
A
L … L
}
252 3/9/2025 6:46 AM
P P
R
E Function Definition Syntax R
E
P P
A
R
 ReturnType: A
R
E  A function may return a value. E
D D

B B
Y  The return_type is the data type of the value the function returns.
Y

S S
U  Some functions perform the desired operations without returning a U
L
A
value. L
A
V V

N  In this case, the return_type is the keyword void. N


E E
P P
A A
L L

253 3/9/2025 6:46 AM


P P
R
E Function Definition Syntax R
E
P P
A
R
 Function Name: A
R
E  This is the actual name of the function. E
D D

B B
Y  The function name and the parameter list together constitute the
Y
function signature.
S S
U U
L L
A  Function Body: A
V
 The function body contains a collection of statements that define what V

N the function does. N


E E
P P
A A
L L

254 3/9/2025 6:46 AM


P P
R
E Function Definition Syntax R
E
P P
A
R
 Parameters A
R
E  Parameters include data_type variable1, data_type variable2, … E
D D

B B
Y  When a function is invoked, you pass a value to the parameter.
Y

S S
U  This value is referred to as actual parameter or argument. U
L L
A A
V
 The parameter list refers to the type, order, and number of the V

N parameters of a function. N
E E
P P
A  Parameters are optional; i.e. a function may contain no parameters. A
L L

255 3/9/2025 6:46 AM


P P
R
E Syntax – Example R
E
P P
A int add(int a, int b) //function header A
R R
E { int sum; //function body (statements) E
D D
sum=a+b; //function body (statements)
B B
Y return sum; //returns value of sum to whoever called
Y
}
S S
U U
L L
A ************************************************** A
V V

N float areaOfCircle(float radius) //function header N


E E
P { return 3.1428*radius*radius; //returns radius P
A A
L } L

256 3/9/2025 6:46 AM


P P
R
E Function Declaration (or Prototype) R
E
P P
A  The function declaration or prototype is model or blueprint of the A
R R
E function. E
D D

B  If functions are used before they are defined, then function declaration or B
Y prototype is necessary which provides the following information to the Y

S compiler. S
U  The name of the function. U
L L
A  The type of the value returned by the function.
A
V  The number and the type of arguments that must be supplied when calling the V
function.
N N
E E
P
A
 A function declaration tells the compiler about a function name and how P
A
L to call the function. The actual body of the function can be defined L
separately.
257 3/9/2025 6:46 AM
P P
R
E Function Declaration (or Prototype) R
E
P P
A
R
 A function declaration has the following parts: A
R
E return_type function_name(parameter list); E
D D

B B
Y  For the above defined function add() & areaOfCircle(), the function Y

S declaration is as follow: S
U
L
int add(int num1, int num2); U
L
A float areaOfCircle(float radius); A
V V

N N
E E
P P
A A
L L

258 3/9/2025 6:46 AM


P P
R
E Return Statement R
E
P P
A
R
 It is the statement that is executed just before the function completes A
R
E its job and control is transferred back to the calling function. E
D D

B B
Y  The job of return statement is to hand over some value given by Y

S function body to the point where the call was made. S


U U
L L
A
V
 Two purposes of return statement: A
V
 Immediately transfer the control back to the calling program
N N
E  Returns the value to the calling function E
P P
A A
L L

259 3/9/2025 6:46 AM


P P
R
E Return Statement R
E
P P
A
R
 Syntax: A
R
E return (expression); E
D D

B B
Y  A function may or may not return a value. If a function doesn’t Y

S return a value the return type in the function definition and S


U
L
declaration is specified as void. U
L
A A
V V

N N
E E
P P
A A
L L

260 3/9/2025 6:46 AM


P P
R
E Accessing/Calling a Function R
E
P P
A
R
 A function can be called or accessed by specifying its name, followed A
R
E by a list of arguments enclosed in parenthesis and separated by E
D D
commas/
B B
Y Y

S  For example:
S
U  A function add() with two arguments is called by add(a,b) to add U
L L
A two numbers. A
V
 If function call doesn’t require any arguments any empty pair of V

N parenthesis must follow the name of function. N


E E
P P
A A
L L

261 3/9/2025 6:46 AM


P P
R
E Accessing/Calling a Function R
E
P P
A
R
 General Form: A
R
E  If function has parameters but it doesn’t return value. E
D D
function_name(variable1,variable2,…);
B B
Y Y
 If function has no arguments and it doesn’t return value.
S S
U function_name();
U
L L
A A
V  If function has no arguments and it does return value. V

N
variable_name=function_name();
N
E E
P P
A A
L L

262 3/9/2025 6:46 AM


P P
R
E Example – 1 R
E
P P
A #include<stdio.h>
A
#include<conio.h>
R R
int add(int a, int b)
E E
{
D int sum; D
sum=a+b;
B return sum; B
Y } Y
float areaOfCircle(float radius)
S { S
U return 3.1428*radius*radius;
U
L }
L
A void main()
{
A
V V
int a,b,mySum;
float myArea;
N printf("Enter two numbers:\n"); N
E scanf("%d%d",&a,&b); E
P mySum=add(a,b); P
A printf("The sum is %d\n",mySum); A
L myArea=areaOfCircle(mySum); L
printf("Area of circle is %f\n",myArea);
getch(); 3/9/2025 6:46 AM
263
}
P P
R
E Example – 2 R
E
P P
#include<stdio.h>
A A
#include<conio.h>
R int add(int,int); R
E float areaOfCircle(float); E
D void main()
D
{
int a,b,mySum;
B float myArea; B
Y printf("Enter two numbers:\n"); Y
scanf("%d%d",&a,&b);
S mySum=add(a,b);
S
printf("The sum is %d\n",mySum);
U U
myArea=areaOfCircle(mySum);
L printf("Area of circle is %f\n",myArea); L
A getch(); A
V }
V
int add(int a, int b)
{
N int sum; N
E sum=a+b; E
P return sum;
P
A }
A
float areaOfCircle(float radius)
L { L
return 3.1428*radius*radius;
264 } 3/9/2025 6:46 AM
P P
R
E Function Arguments & Return Type R
E
P P
A
R
 There are three category of functions according to the return value A
R
E and arguments: E
D D
 Functions with no arguments and no return value
B B
Y Y
 Functions with arguments and no return value
S S
U U
L L
A  Function with arguments and return value
A
V V

N N
E E
P P
A A
L L

265 3/9/2025 6:46 AM


P P
R
E Function Arguments & Return Type R
E
P P
A  Functions with no arguments and no return value: A
R R
E  When function has no arguments, it does not receive any data from the calling E
D function. D

B B
Y  Similarly when it doesn’t return a value, the calling function does not receive Y
any data from the called function.
S S
U U
L  Thus, in such type of functions there is no data transfer between the calling L
A function and the called function. A
V V

N  Keyword “void” means the function doesn’t return any value. N


E E
P P
A  There is no arguments within parenthesis which implies function has no A
L argument and it doesn’t receive any data from the called function. L

266 3/9/2025 6:46 AM


P P
R
E Function Arguments & Return Type R
E
P P
A  Functions with no arguments and no return value: A
R R
E
#include<stdio.h>
E
D #include<conio.h> D
void add()
B
Y
{ B
Y
int a,b,sum;
S printf("Enter any two numbers:\n"); S
U scanf("%d%d",&a,&b); U
L L
A sum=a+b;
A
V printf("\nThe sum is %d\n",sum); V
}
N N
E void main()
E
P { P
A add(); A
L L
getch();
267 } 3/9/2025 6:46 AM
P P
R
E Function Arguments & Return Type R
E
P P
A  Functions with arguments but no return value: A
R  We pass arguments while calling a function but nothing is returned to the calling R
E function. E
D D
#include<stdio.h>
#include<conio.h>
B B
Y
void add(int a,int b)
{ Y

S int sum=0;
S
U sum=a+b;
U
L printf("\nThe sum is %d\n",sum);
L
A } A
V void main() V
{
N int a,b; N
E printf("Enter two numbers:\n"); E
P scanf("%d%d",&a,&b); P
A add(a,b); A
L getch(); L
}
268 3/9/2025 6:46 AM
P P
R
E Function Arguments & Return Type R
E
P P
A  Functions with arguments and return value: A
R  We pass arguments and we expect a return value R
E E
#include<stdio.h>
D D
#include<conio.h>
B
int add(int a,int b)
{ B
Y Y
int sum=0;
S sum=a+b;
S
U return sum;
U
L } L
A void main() A
V { V
int a,b,sum;
N printf("Enter two numbers:\n"); N
E scanf("%d%d",&a,&b); E
P sum=add(a,b); P
A printf("The sum is %d",sum); A
L getch(); L
}
269 3/9/2025 6:46 AM
P P
R
E Passing Arrays to Function R
E
P P
A
R
 It is possible to pass the value of an array element and even an entire A
R
E array as an argument to a function. E
D D

B B
Y  To pass an entire array to a function, the array name must appear by Y

S itself, without brackets or subscripts, as an actual argument in S


U function call statement. U
L L
A A
V V
 When declaring a one-dimensional array as a formal argument, the
N N
E array name is written with a pair of empty square brackets. E
P P
A A
L
 The size of the array is not specified within the formal argument L

270 declaration. 3/9/2025 6:46 AM


P P
R
E Passing Arrays to Function R
E
P P
A  Syntax for function call passing array as argument: A
R function_name(array_name) R
E E
D D
 Syntax for function prototype which accepts array:
B return_type function_name(data_type array_name[]); B
Y Y
Or
S return_type function_name(data_type *pointer_variable); S
U U
L L
A  When array is passed to a function, the values of the array elements are not passed
A
V to the function rather the array name is interpreted as the address of the first V
array element.
N N
E E
P  The address is assigned to the corresponding formal argument when the function
P
A is called. A
L L
 The formal argument therefore becomes a pointer to the first array element.
271 3/9/2025 6:46 AM
P P
R
E Passing Arrays to Function R
E
P P
A #include<stdio.h> A
R #include<conio.h> R
E E
D void display(int n)
D
{
B printf("%d\t",n); B
Y Y
}
S void main() S
U { U
L L
A
int i, num[5]={12,23,34,45,56};
A
V printf("\nThe elements of an array are: "); V
for(i=0;i<5;i++)
N N
E
{
E
P display(num[i]); P
A } A
L
getch(); L

272
} 3/9/2025 6:46 AM
P P
R
E Passing Entire Array to Function R
E
P P
A // Program to calculate the sum of array elements by passing to a function
A
R #include <stdio.h>
R
E float calculateSum(float num[]);
E
D int main()
D
{
B float result, num[] = {23.4, 55, 22.6,3, 40.5,18}; B
Y // num array is passed to calculateSum() Y
result = calculateSum(num);
S printf("Result = %.2f", result); S
U return 0; U
L } L
A float calculateSum(float num[]) A
V { V
float sum = 0.0;
N for (int i = 0; i < 6; ++i) N
E { E
P sum += num[i]; P
A } A
L return sum; L
}
273 3/9/2025 6:46 AM
P P
R
E Passing Strings to Function R
E
P P
A #include<stdio.h> A
R R
E
#include<conio.h>
E
D void display(char ch[]) D
{
B B
Y printf("String output: "); Y
puts(ch);
S S
U }
U
L void main() L
A { A
V V
char c[50];
N printf("Enter String:\n"); N
E E
P gets(c);
P
A display(c); A
L
getch(); L

274 } 3/9/2025 6:46 AM


P P
R
E Passing Arguments by Value R
E
P P
A
R
 When values of actual arguments are passed to the function as A
R
E arguments, it is known as passing by value. E
D D

B B
Y  Here, the value of each actual argument is copied into corresponding Y

S formal argument of the function definition. S


U U
L L
A
V
 Note: The contents of the actual arguments in the calling function A
V
are not changed, even if they are changed in the called function.
N N
E E
P P
A A
L L

275 3/9/2025 6:46 AM


P P
R
E Passing Arguments by Value R
E
P P
A #include<stdio.h> A
R #include<conio.h> R
E void swap(int a,int b) E
D D
{
B int t; B
Y t=a; Y
a=b;
S S
b=t;
U U
L printf("\nValues within swap: a=%d & b=%d",a,b);
L
A } A
V void main() V
{
N N
int x=2,y=3;
E E
P printf("Before swap function call: x=%d & y=%d",x,y);
P
A swap(x,y); A
L printf("\nAfter swap function call: x=%d & y=%d",x,y); L
getch();
276 3/9/2025 6:46 AM
}
P P
R
E Passing Arguments by Address R
E
P P
A
R
 When addresses of actual arguments are passed to the function as A
R
E arguments (instead of values of actual arguments), it is known as E
D D
passing by address.
B B
Y Y

S  Here, the address of each actual argument is copied into


S
U corresponding formal argument of the function definition. U
L L
A A
V V
 In this case, the formal arguments must be of type pointers.
N N
E E
P P
A  Note: The values contained in addresses of the actual arguments in A
L
the calling function are changed, if they are changed in the called L

277 function. 3/9/2025 6:46 AM


P P
R
E Passing Arguments by Address R
E
P P
A #include<stdio.h> A
R #include<conio.h> R
E void swap(int *x,int *y) E
D D
{
B int temp; B
Y temp=*x; Y
*x=*y;
S S
*y=temp;
U U
L printf("Values within swap: x=%d & y=%d\n",*x,*y);
L
A } A
V void main() V
{
N N
int a=23, b=34;
E E
P printf("Before swap function call: a=%d & b=%d\n",a,b);
P
A swap(&a,&b); A
L printf("After swap function call: a=%d & b=%d\n",a,b); L
getch();
278 3/9/2025 6:46 AM
}
P P
R
E Recursive Function R
E
P P
A
R
 Within a function body, if the function calls itself, the mechanism is A
R
E known as “Recursion” and the function is known as “Recursive E
D D
Function”.
B B
Y Y

S  To solve a problem using recursive method, two conditions must be


S
U satisfied: U
L L
A  Problem could be written or defined in term of its previous result. A
V V
 Problem statement must include a stopping condition.
N N
E E
P P
A A
L L

279 3/9/2025 6:46 AM


P P
R
E WAP to find factorial R
E
P P
A #include<stdio.h>
A
R #include<conio.h>
R
E int fact(int n)
E
D {
D
int res=1, i;
B for(i=n;i>=1;i--) B
Y { Y
res=res*i;
S } S
U return res; U
L } L
A void main() A
V { V
int n,factorial;
N int fact(int); N
E printf("Enter any number:\n"); E
P scanf("%d",&n); P
A factorial=fact(n); A
L printf("Factorial is %d\n",factorial); L
getch();
280 } 3/9/2025 6:46 AM
P P
R
E WAP to find factorial – recursion R
E
P P
A #include<stdio.h>
A
R #include<conio.h>
R
E int fact(int n)
E
D {
D
int res;
B if(n==1) B
Y return(1); Y
else
S res=n*fact(n-1); S
U return res; U
L } L
A void main() A
V { V
int n,factorial;
N int fact(int); N
E printf("Enter any number:\n"); E
P scanf("%d",&n); P
A factorial=fact(n); A
L printf("Factorial is %d\n",factorial); L
getch();
281 } 3/9/2025 6:46 AM
P P
R
E Recursion vs. Iteration R
E
P P
A A
R R
E Recursion Iteration
E
D A function is called from the definition of the Loop is used to do repeated task. D
same function to do repeated task.
B B
Y Recursion is a top-down approach to problem Iterations is like bottom-up approach; it begins Y
solving; it divides the problem into pieces. with what is known and from this it constructs the
S solution step by step. S
U U
L
In recursion, a function calls to itself until some In iteration, a function does not call to itself.
condition will be satisfied. L
A A
V Problem could be written or defined in term of its It is not necessary to define a problem in term of V
previous result to solve a problem using recursion. its previous result to solve using iteration.
N N
E All problems cannot be solved using recursion. All problems can be solved using iteration. E
P P
A A
L L

282 3/9/2025 6:46 AM


P
R
Scope visibility and lifetime of a P
R
E
P variable E
P
A
R
 Variables in C are categorized into four different storage classes A
R
E according to the scope and lifetime of variables. E
D D
 Local (Automatic) variables
B
Y
 Global (External) variables B
Y
 Static variables
S S
U  Register variables U
L L
A A
V V
 The scope of variable determines over what part(s) of the program a
N
E
variable is actually available for use (active). N
E
P P
A A
L  Lifetime refers to the period of time during which a variable retains L

283 a given value during execution of a program (alive). 3/9/2025 6:46 AM


P P
R
E Variables R
E
P P
A
R
 Variables can also be broadly categorized, depending on the place of A
R
E their declaration, as internal (local) or external (global). E
D D

B B
Y  Local variables are those which are declared within a particular Y

S function. S
U U
L L
A
V
 Global variables are declared outside of any function. A
V

N N
E E
P P
A A
L L

284 3/9/2025 6:46 AM


P P
R
E Local Variables R
E
P P
A  Also known as Automatic variables or Internal variables. A
R R
E E
D  They are always declared inside a function or block in which they are to be D

B
used.
B
Y Y
 They are created when the function is called and destroyed
S S
U automatically when the function is exited, hence the name is automatic. U
L L
A A
V  The keyword auto is used for storage class specification although it is
V
optional.
N N
E E
P  Initial value: garbage. P
A  Scope: local to the block where the variable is defined. A
L L
 Lifetime: till the control remains within the block where variable is defined.
285 3/9/2025 6:46 AM
P P
R
E Global Variables R
E
P P
A
R
 Also known as External variables. A
R
E E
D D
 These variables are both alive and active throughout the entire
B B
Y program. Y

S S
U U
L  Unlike local variables, global variables can be accessed by any L
A
V
function in the program. A
V

N N
E  Initial value: zero E
P P
A  Scope: global (i.e. throughout the program) A
L L
 Lifetime: throughout program execution
286 3/9/2025 6:46 AM
P P
R
E Local & Global Variables R
E
P P
A A
R R
E LocalVariable GlobalVariable E
D D
#include<stdio.h> #include<stdio.h>
B #include<conio.h> #include<conio.h>
B
Y void main() int a; Y
{ void main()
S int a; { S
U printf("%d",a); printf("%d",a); U
L getch(); getch(); L
A A
} }
V V

N N
E E
P P
A A
L L

287 3/9/2025 6:46 AM


P P
R
E Local & Global Variables – Example R
E
P P
A #include<stdio.h> A
R R
E
#include<conio.h>
E
D int a=100; D
void function()
B B
Y { Y
printf("%d\n",a);
S S
U a=200;
U
L printf("%d\n",a); L
A } A
V V
void main()
N { N
E E
P printf("%d\n",a);
P
A function(); A
L
getch(); L

288 } 3/9/2025 6:46 AM


P P
R
E Static Variables R
E
P P
A  As the name suggests, the value of static variables persists till the end of the A
R R
E program. E
D D

B  A variable is declared static using the keyword static. i.e. 𝑠𝑡𝑎𝑡𝑖𝑐 𝑖𝑛𝑡 𝑥; B
Y Y

S  A static variable may be either an internal type or an external type, S


U U
L depending on the place of declaration. L
A A
V V
 Initial value: zero (initialized only once)
N N
E  Scope: local (within the function) or global (within the program)
E
P  Lifetime: throughout the program for local as well as global static P
A A
L variables. L

289 3/9/2025 6:46 AM


P P
R
E Static Variables R
E
P P
A #include<stdio.h> #include<stdio.h> A
R #include<conio.h> #include<conio.h> R
E E
D increment() increment()
D
{ {
B int i=1; static int i=1; B
Y Y
printf("%d\t",i); printf("%d\t",i);
S i++; i++; S
U } } U
L L
A
void main() void main()
A
V { { V
increment(); increment();
N N
E
increment(); increment();
E
P increment(); increment(); P
A increment(); increment(); A
L
getch(); getch(); L

290
} } 3/9/2025 6:46 AM
P P
R
E Register Variables R
E
P P
A  Normally, variables are stored in memory. A
R R
E  However, we can instruct the compiler that a variable should be kept in one of the CPU’s registers, E
D instead of keeping in the memory. D

B B
Y  Use: A register access is much faster than a memory access. So keeping frequently accessed variables
(e.g. loop control variables) in the register will help faster execution of the program. Y

S S
U  A register variable is declared using register keyword: i.e. 𝑟𝑒𝑔𝑖𝑠𝑡𝑒𝑟 𝑖𝑛𝑡 𝑥; U
L L
A A
 Register variables are always declared inside a function or block.
V V

N  They are allocated space upon entry to a function; and the storage is freed when the function is N
E exited. E
P P
A  Initial value: garbage A
L L
 Scope: local (within the function)
 Lifetime: until the end of function
291 3/9/2025 6:46 AM
P P
R
E Register Variables R
E
P P
A
R
#include<stdio.h> A
R
E
D
#include<conio.h> E
D

B
void main()
B
Y { Y

S register int i; S
U U
L for(i=0;i<100;i++) L
A A
V { V

N printf("%d\t",i); N
E E
P } P
A A
L getch(); L

292 } 3/9/2025 6:46 AM


P P
R
E Local, Global, & Static Variables R
E
P P
A A
R R
E LocalVariables GlobalVariables StaticVariables
E
DThe variables are declared within Static variables are special case of D
Global variables are defined outside
function or blocks.The scope is only local variables.Thus, static variables
Bwithin the function or block in which the functions so that their scope is
are also defined inside the functions B
Y throughout the program. Y
they are defined. or blocks.
SThe initial value is unpredictable or S
Ugarbage value.
The initial value is zero. The initial value is zero.
U
L L
AThe life time is till the control The life time is till programs A
Vremains within the block or function execution doesn’t come to an end. i.e. Its value persists between different V
in which the variable is defined.The
variables are created when program function calls.Thus life time is same
Nvariable is destroyed when function N
Ereturns to the calling function or
starts and destroyed when program as global variables.
E
Pblock ends. ends.
P
A A
LThe keyword auto is used. The keyword extern is used. The keyword static is used.
L

293 3/9/2025 6:46 AM


P P
R R
E E
P P
A A
R R
E E
D D

B B
Y Y

S
END OF UNIT SEVEN
S
U U
L L
A A
V V

N N
E E
P P
A A
L L

294 3/9/2025 6:46 AM


P P
R R
E E
P P
A A
R R

UNIT 8
E E
D D

B B
Y Y

S STRUCTURE AND UNION S


U U
L L
A A
V V

N N
E E
P P
A A
L L

295 3/9/2025 6:46 AM


P P
R
E Syllabus R
E
P P
A  UNIT 8: Structure and Union A
R R
E o Introduction E
D D

B o Array of structure B
Y Y

S o Passing structure to function S


U U
L L
A o Passing array of structure to function A
V V

N
o Structure within structure (nested structure) N
E E
P P
A
L
o Union A
L

296 3/9/2025 6:46 AM


o Pointer to structure
P P
R
E Structure R
E
P P
A  A structure is a collection of variables under a single name. A
R R
E E
D D
 Arrays allow to define type of variables that can hold several data items of
B the same kind. B
Y Y

S  Similarly structure is another user defined data type available in C that S


U U
L allows to combine data items of different kinds. L
A A
V V
 A structure is a convenient way of grouping several pieces of related
N information together. N
E E
P P
A A
L  A structure is a convenient tool for handling a group of logically related
L
data items.
297 3/9/2025 6:46 AM
P P
R
E Structure R
E
P P
A
R
 For example: name, roll, fee, marks, address, gender, and phone are A
R
E related information of a student. E
D D

B B
Y  To store information about a student, we would require to store the Y

S name of the student which is array of characters, roll of student S


U which is integer type of data and so on. U
L L
A A
V V
 These attributes of students can be grouped into a single entity,
N N
E student. Here, student is known as structure which organizes E
P
A
different data types in a more meaningful way. P
A
L L

298 3/9/2025 6:46 AM


P P
R
E Defining a Structure R
E
P P
A  Syntax: A
R R
E struct structure_name { E
D
data_type member_variable1; D

B data_type member_variable2; B
Y Y
data_type member_variable3;
S ……… S
U U
L ……… L
A data_type member_variableN; A
V V
};
N N
E E
P  Once structure_name is declared as new data type, then variables P
A A
L of that type can be declared as L
struct structure_name structure_variable;
299 3/9/2025 6:46 AM
P P
R
E Defining a Structure R
E
P P
A  So from earlier example, student is new data type and various variables of A
R type struct student can be declared as: R
E E
D struct student st; D

B B
Y  Similarly multiple variables can also be declared:
Y
struct student st1, st2, st3;
S S
U U
L  The member variables are accessed using dot(.) operator. L
A A
V V
 Each member variable of structure has its own copy of member variables.
N N
E E
P  For example: P
A
 st1.name is member variable, name of st1 structure variable. A
L L
 St2.roll is member variable, roll of st2 structure variable.
300 3/9/2025 6:46 AM
P P
R
E Examples R
E
P P
A A
R R
E E
D D
struct employee{
B struct book{ B
int emp_id;
Y struct data{ char title[25]; struct account{ Y
char
int day; char author[20]; int acc_no;
S emp_name[20];
int month; int pages; char acc_type[20]; S
U int age; U
int year; float price; float balance;
L char gender; L
A
}; }b1, b2; };
float salary; A
V }e1, e2; V

N N
E E
P P
A A
L L

301 3/9/2025 6:46 AM


P P
R
E Declaration of a Structure R
E
P P
A #include<stdio.h> A
R #include<conio.h> R
E void main() E
D { D
struct student{
B B
char name[25];
Y Y
int roll_no;
S float marks;
S
U char gender;
U
L long long int phone_no; L
A }; A
V struct student st1={"Ram Thapa", 4, 89.5, 'M', 9841234567}; V
printf("Name:\t\t%s\n", st1.name);
N printf("Roll No.:\t%d\n", st1.roll_no); N
E E
printf("Marks:\t\t%f\n", st1.marks);
P P
A printf("Gender:\t\t%c\n", st1.gender);
A
L printf("Phone No.:\t%lld\n", st1.phone_no);
L
getch();
302 } 3/9/2025 6:46 AM
P P
R
E Declaration of a Structure R
E
P P
#include<stdio.h>
A A
#include<conio.h>
R void main() R
E { E
D struct student
D
{
char name[20];
B int roll; B
Y float marks; Y
char remarks;
S };
S
struct student s;
U U
printf("Enter name:\t");
L gets(s.name); L
A printf("\n Enter roll:\t"); A
V scanf("%d", &s.roll);
V
printf("\n Enter marks:\t");
scanf("%f", &s.marks);
N printf("\n Enter remarks(P for pass or F for fail):\t"); N
E scanf(" %c", &s.remarks); E
P printf("\n Name \t Roll \t Marks \t Remarks\n");
P
A printf("\n...................................\n");
A
printf("\n%s\t%d\t%f\t%c", s.name, s.roll, s.marks, s.remarks);
L getch(); L
}
303 3/9/2025 6:46 AM
P P
R
E Array of Structure R
E
P P
A
R
 A collection of similar type of structure placed in a common variable A
R
E name is called array of structure. E
D D

B B
Y  In our previous structure example, if we want to keep record of 50 Y

S students, we have to make 50 structure variables like st1, st2, …, S


U st50. (which is the worst technique) U
L L
A A
V V
 To tackle this we can use array of structure to store records of 50
N N
E students. E
P P
A A
L L

304 3/9/2025 6:46 AM


P P
R
E Array of Structure R
E
P P
A A
R R
E
D
 An array of structure can be declared in two ways as illustrated E
D
below:
B B
Y Y
struct Employee{ struct Employee{
S char name[20]; char name[20]; S
U U
int empID; int empID;
L L
A float salary; float salary;
A
V } emp[10]; };
V
struct Employee emp[10];
N N
E E
P P
A A
L L

305 3/9/2025 6:46 AM


P P
R
E Array of Structure R
E
P P
A /* C program to store record of 10 books */
A
#include<stdio.h>
R R
#include<conio.h>
E E
struct book
D { D
char name[50];
B float price; B
Y int pages; Y
};
S void main() S
U {
U
L struct book b[10];
L
A int i;
for(i=0; i<10; i++)
A
V V
{
printf("\nEnter name, price and page number:\n");
N scanf("%s%f%d", &b[i].name, &b[i].price, &b[i].pages); N
E } E
P for(i=0; i<10;i++) P
A { A
L printf("\n Name: %s \n Price:%f \n Pages:%d", b[i].name, b[i].price, b[i].pages); L
}
getch(); 3/9/2025 6:46 AM
306
}
P P
R
E Passing Structure to Function R
E
P P
A A
R R
E
D
 Like any ordinary variable, a structure variable can also be passed to E
D
a function.
B B
Y Y

S  One may either pass individual structure elements or the entire S


U
L
structure at once. U
L
A A
V V

N N
E E
P P
A A
L L

307 3/9/2025 6:46 AM


P P
R
E Passing Structure to Function R
E
P P
A #include<stdio.h> A
R #include<conio.h> R
E void display(char[],float,int); E
D struct book D
{
B B
char name[25];
Y Y
float price;
S int pages;
S
U };
U
L void main() L
A { A
V struct book b={"C Program", 345.5, 154}; V
display(b.name, b.price, b.pages);
N } N
E E
void display(char n[25], float pr, int pg)
P P
A {
A
L printf("\nName:\t\t%s\nPrice:\t\t%f\nPage No.:\t%d",n, pr, pg);
L
getch();
308 } 3/9/2025 6:46 AM
P P
R
E Passing Array of Structure to Function R
E
P P
A  It is possible to define an array of structures for example if we are A
R maintaining information of all the students in the college and if 100 R
E
D students are studying in the college. E
D

B B
Y  We need to use an array than single variables.
Y

S S
U  We can define an array of structures as shown in example below:
U
L struct information L
A { A
V V
int id_no;
N char name[25]; N
E char address[25]; E
P P
A
char combination[3];
A
L int age; L
} student[100];
309 3/9/2025 6:46 AM
P P
R
E Passing Array of Structure to Function R
E
P P
#include<stdio.h>
A A
#include<conio.h>
R struct Example R
E { int num1; int num2;} s[10]; E
D void accept(struct Example s[],int n)
D
{
int i;
B for(i=0;i<n;i++) B
Y { Y
printf("\nEnter num1 : "); scanf("%d",&s[i].num1);
S printf("\nEnter num2 : "); scanf("%d",&s[i].num2);
S
}
U U
}
L void print(struct Example s[],int n) L
A { A
V int i;
V
for(i=0;i<n;i++)
{
N printf("\nNum1 : %d",s[i].num1); printf("\nNum2 : %d",s[i].num2); N
E } E
P }
P
A void main()
A
{
L int i; accept(s,3); print(s,3); getch(); L
}
310 3/9/2025 6:46 AM
P
R
Structure within Structure P
R
E
P (Nested Structure) E
P
A  Nested structures are nothing but structure within a structure. A
R struct emp_add R
E E
D
{
D
int house_no;
B char streetname[25]; B
Y }; Y

S S
U  The above definition could be used as a template to declare a variable address in U
L the employee structure. L
A struct employee A
V V
{
N char name[25]; N
E int age; E
P struct emp_add address; P
A char address[50]; A
L L
float salary;
311
}; 3/9/2025 6:46 AM
P
R
Structure within Structure P
R
E
P (Nested Structure) E
P
A #include<stdio.h>
A
#include<conio.h>
R R
struct student_college_detail
E E
{
D int college_id; D
char college_name[25];
B }; B
Y struct student_detail Y
{
S int id; S
U char name[25];
U
L float percentage;
L
A struct student_college_detail clg_data;
}stu_data;
A
V V
void main()
{
N struct student_detail stu_data={1, "Ram", 90.5, 711, "TIC"}; N
E printf("ID is:\t\t%d\n", stu_data.id); E
P printf("Name is:\t%s\n", stu_data.name); P
A printf("Percentage is:\t%f\n", stu_data.percentage); A
L printf("College ID is:\t%d\n", stu_data.clg_data.college_id); L
printf("College Name is:%s\n", stu_data.clg_data.college_name);
getch(); 3/9/2025 6:46 AM
312
}
P P
R
E Union R
E
P P
A
R
 User defined data type such that every union member takes memory A
R
E that contains a variety of objects. E
D D

B B
Y  Union members share space thereby resulting, conserves storage. Y

S S
U U
L  Size required by a union variable is the size required by the member L
A
V
requiring the largest size. A
V

N N
E  Only the last data member defined can be accessed in union. E
P P
A A
L L

313 3/9/2025 6:46 AM


P P
R
E Union R
E
P P
A union item A
R
{ int m; R
E E
D float p; D
char c;
B } code; B
Y Y
 The above code declares a variable code of type union item.
S S
U U
L  Union contains three members each with different data types. L
A A
V V
 However only one of them can be used at a time.
N N
E E
P  Syntax to access variable is similar to structure. i.e. P
A code.m=456; A
L L
code.p=456.78;
314 printf(“%d”, code.m); 3/9/2025 6:46 AM
P P
R
E Union – Example R
E
P P
A #include<stdio.h> #include<stdio.h> A
R R
E #include<conio.h> #include<conio.h> E
D void main() void main() D

B { { B
Y Y
union student union student
S { { S
U U
L int roll; int roll;
L
A float marks; float marks; A
V V
}stud; }stud;
N stud.roll=45; stud.roll=45; N
E E
P printf("Roll No. - \t%d\n", stud.roll); stud.marks=72.5; P
A
stud.marks=72.5; printf("Roll No. - \t%d\n", stud.roll); A
L L
printf("Marks - \t%f", stud.marks); printf("Marks - \t%f", stud.marks);
315 3/9/2025 6:46 AM
} }
P P
R
E Union – Example R
E
P P
#include<stdio.h>
A A
#include<conio.h>
R union student R
E { E
D char name[20];
D
int roll_no;
float marks;
B char section; B
Y }; Y
void main()
S {
S
union student s;
U U
printf("Size of union=%d\n", sizeof(s));
L strcpy(s.name, "Hari"); L
A printf("Name=%s\t", s.name); A
V printf("Location=%u", s.name);
V
s.roll_no=10;
printf("\nRoll=%d \tLocation=%u", s.roll_no, &s.roll_no);
N s.marks=91.5; N
E printf("\nMarks=%.2f \tLocation=%u", s.marks, &s.marks); E
P s.section='A';
P
A printf("\nSection=%c \tLocation=%u", s.section, &s.section);
A
printf("\n\nErroneous output");
L printf("\n\nName\tRoll\tMarks\tSection\n"); L
printf("%s\t%d\t%.2f\t%c", s.name, s.roll_no, s.marks, s.section);
316 getch(); 3/9/2025 6:46 AM
}
P P
R
E Pointer to Structure R
E
P P
A
R
 Pointers can be used also with structure. A
R
E E
D D
 To store address of a structure type variable, we can define a structure
B B
Y type pointer variable as normal way. Y

S S
U U
L  A structure type pointer variable can be declared as: L
A A
V struct book
V
{ char name[20];
N N
E int pages; E
P P
A float price; A
L
}; L

317 Struct book *bptr; 3/9/2025 6:46 AM


P P
R
E Pointer to Structure R
E
P P
A  This declaration for a pointer to structure does not allocate any A
R R
E memory for a structure but allocates only for a pointer. E
D D

B  So to access structures members through pointer bptr, we must B


Y Y
allocate the memory using malloc() function.
S S
U U
L  Now, individual structure members are accessed as: L
A A
V bptr -> name or (*bptr).name V

N
bptr -> pages or (*bptr).pages
N
E bptr -> price or (*bptr).price E
P P
A A
L
 Here, -> is called arrow operator and there must be a pointer to the L

318 structure on the left side of this operator. 3/9/2025 6:46 AM


P P
R
E Pointer to Structure – Example R
E
P P
A #include <stdio.h> A
R struct person R
E { E
D int age; D
float weight;
B B
};
Y Y
int main()
S {
S
U struct person *personPtr, person1;
U
L personPtr = &person1; L
A printf("Enter age: "); A
V scanf("%d", &personPtr->age); V
printf("Enter weight: ");
N scanf("%f", &personPtr->weight); N
E E
printf("\nDisplaying:\n");
P P
A printf("Age: %d\n", personPtr->age);
A
L printf("weight: %f", personPtr->weight);
L
return 0;
319 } 3/9/2025 6:46 AM
P P
R
E Example R
E
P P
A
R
Write a menu driven program which performs the following jobs: A
R
E
D
i. Create a structure named student with member variables: roll, E
D
name, grade, gender, age.
B B
Y ii. Define ten structure variables of type student and using loop take Y

S input for all ten structure variables. S


U
L
iii. Display names of all male students. U
L
A
V
iv. Exit. A
V

N N
E E
P P
A A
L L

320 3/9/2025 6:46 AM


P P
R R
E E
P P
A A
R R
E E
D D

B B
Y Y

S
END OF UNIT EIGHT
S
U U
L L
A A
V V

N N
E E
P P
A A
L L

321 3/9/2025 6:46 AM


P P
R R
E E
P P
A A
R R

UNIT 9
E E
D D

B B
Y Y

S POINTERS S
U U
L L
A A
V V

N N
E E
P P
A A
L L

322 3/9/2025 6:46 AM


P P
R
E Syllabus R
E
P P
A  UNIT 9: Pointers A
R o Introduction
R
E E
D o The & and * operator
D

B o Declaration of Pointer
B
Y Y
o Chain of Pointers

S S
o Pointer Arithmetic
U U
L L
o Pointers and Arrays
A A
V o Array of Pointers V

N o Pointers as Function Arguments N


E E
P o Function Returning Pointers P
A A
L o Pointers and Structures L

323 o Dynamic Memory Allocation 3/9/2025 6:46 AM


P P
R
E Background R
E
P P
A  So before knowing what pointer is let us know about memories. A
R R
E E
D D
 All computer have primary memory, also known as RAM (Random Access
B Memory) B
Y Y

S  RAM holds the programs that the computer is currently running along S
U U
L with the data (i.e. variables) they are currently manipulating. L
A A
V V
 All the variables used in a program reside in the memory when the program
N is executed. N
E E
P P
A A
L  RAM is divided into a number of small units or locations and each location
L
is represented by some unique number known as memory address.
324 3/9/2025 6:46 AM
P P
R
E Background R
E
P P
A  Each memory location is capable of storing small number, which is known as byte. A
R R
E E
D  A char data is one byte in size and hence needs one memory location of the D
memory.
B B
Y Y
 Similarly, integer data is two byte in size and hence needs two memory locations
S of the memory. S
U U
L L
A  Key concept: 8 bits = 1 byte ; 1024 bytes = 1 Kilo Byte (KB) ; 1024 KB = 1 A
V Mega Byte (MB) ; 1024 MB = 1 Giga Bytes (GB) V

N N
E  A computer having 1 GB RAM has 1024*1024*1024 (i.e. 1073741824) bytes
E
P and these 1073741824 bytes are represented by 1073741824 different address. P
A A
L L
 For example: The memory address 65524 represents a byte in memory and it can
325
store data of one byte. 3/9/2025 6:46 AM
P P
R
E Background R
E
P P
A
R
 Every variable in C program is assigned a space in memory. A
R
E E
D D
 When a variable is declared, it tells computer the type of variable
B B
Y and name of the variable. Y

S S
U U
L  According to the type of variable declared, the required memory L
A
V
locations are reserved. A
V

N N
E  For example: int requires two bytes, float requires four bytes and char E
P P
A requires one byte. A
L L

326 3/9/2025 6:46 AM


P P
R
E Background R
E
P P
A A
R R
E E
D D

B B
Y Y

S S
U U
L L
A A
V V

N N
E E
P P
A A
L L

327 3/9/2025 6:46 AM


P
R
A program to display memory location P
R
E
P reserved by a variable E
P
A
R
#include<stdio.h> A
R
E #include<conio.h> E
D D
void main()
B B
Y { Y

S
int a=30;
S
U printf("The address of a is %u\n", &a); U
L L
A printf("The value of a is %d\n", a); A
V V
getch();
N
E
} N
E
P  Output: P
A A
L The address of a is 2752268 L
The value of a is 30 3/9/2025 6:46 AM
328
P
R
A program to illustrate address P
R
E
P reserved by different data types E
P
A #include<stdio.h> A
R #include<conio.h> R
E void main() E
D D
{
B int a=3, b=4; B
Y float c=50.5; Y
char d='A';
S S
printf("The Base Address of a is %u\n", &a);
U U
L printf("The Base Address of b is %u\n", &b);
L
A printf("The Base Address of c is %u\n", &c); A
V printf("The Base Address of d is %u\n", &d); V
getch();
N N
}
E E
P  Output:
P
A The Base Address of a is 2752268
A
L The Base Address of b is 2752264 L
The Base Address of c is 2752260
329 The Base Address of d is 2752259 3/9/2025 6:46 AM
P P
R
E Pointer R
E
P P
A  A pointer is a variable that contains a memory address of variable. A
R R
E E
D  A pointer variable is declared to some type, like any other variable. D

B B
Y  Each pointer variable can point only to one specific type.
Y

S S
U  Pointer is declared in the same fashion like other variables but is always preceded
U
L by ‘*’ (asterisk operator). L
A A
V
 Integer variable declared as: V

N int a; (Here, a is considered as integer variable) N


E E
P P
A
 Similarly,
A
L int *a; L
Now variable a is a pointer variable, it now can store address of integer variable.
330 The address of float variable can not be stored in it. 3/9/2025 6:46 AM
P P
R
E Pointer R
E
P P
A
R
 Valid Example: A
R
E int *p; E
D D
int num;
B B
Y p=&num; Y

S S
U  Invalid Example: U
L L
A int *p; A
V V
float num;
N
E
p=&num; N
E
P P
A A
L L

331 3/9/2025 6:46 AM


P P
R
E Advantages of Pointer R
E
P P
A  Pointers increase the execution speed and thus reduce the program execution A
R time. R
E E
D D
 With the help of pointer we can handle any type of data structure.
B B
Y Y
 By using pointers we can access a data which is available outside a
S S
U function. U
L L
A A
V  By using pointer variable we can implement dynamic memory allocation.
V

N N
E  Pointers provide an efficient tool for manipulating dynamic data structures
E
P such as structures, linked lists, queues, stacks and trees. P
A A
L L
 Pointers reduce length and complexity of programs.
332 3/9/2025 6:46 AM
P P
R
E Pointer Declaration R
E
P P
A
R
 Pointer variable can be declared as follows: A
R
E data_type *variable_name; E
D D

B B
Y  Example: Y

S int *x; //x integer pointer, holds address of any int variable S
U
L
float *y; //y integer pointer, holds address of any float variable U
L
A char *z; //z integer pointer, holds address of any char variable A
V V

N N
E E
P P
A A
L L

333 3/9/2025 6:46 AM


P P
R
E Pointer Declaration R
E
P P
A #include<stdio.h> A
R R
E #include<conio.h> E
D D
void main()
B { B
Y Y
int v=10, *p;
S S
U p=&v;
U
L printf("Address of v is %u\n",&v); L
A A
V printf("Address of v is %u\n",p); V

N printf("Value of v is %d\n",v);
N
E printf("Value of v is %d\n",*p); E
P P
A printf("Address of p is %u\n",&p); A
L
getch(); L

334 } 3/9/2025 6:46 AM


P P
R
E Indirection or Dereference Operator R
E
P P
A  The operator *, used in front of a variable, is called pointer or A
R R
E indirection or dereference operator. E
D D

B  Normal variable provides direct access to their own values whereas B


Y Y
pointer provides indirect access to the values of the variable whose
S address it stores. S
U U
L L
A A
V  When the pointer is declared, the star indicates that it is a pointer, V

N
not a normal variable.
N
E E
P P
A  The indirection operator indicates “the value at the memory location A
L stored in the pointer” or “the content of the location pointed by L

335
pointer variable” 3/9/2025 6:46 AM
P P
R
E Address Operator R
E
P P
A
R
 The operator & is known as address operator. A
R
E E
D D
 &a denotes the address of variable a.
B B
Y Y

S  For example: S
U U
L int a=10, *p; L
A A
V p=&a; V

N N
E E
P P
A A
L L

336 3/9/2025 6:46 AM


P P
R
E Initializing Pointer R
E
P P
A
R
 Address of some variable can be assigned to a pointer variable at the A
R
E time of declaration of the pointer variable. E
D D

B B
Y  For example: Y

S int num; S
U
L
int *ptr=&num; U
L
A A
V V
 These two statements above are equivalent to following statements.
N N
E int num; E
P P
A int *ptr; A
L
ptr=&num; L

337 3/9/2025 6:46 AM


P P
R
E So what actually is bad pointer? R
E
P P
A  When a pointer is first declared, it doesn’t have a valid address. A
R R
E E
D D
 Each pointer must be assigned a valid address before it can support
B
Y
dereference operators. Before that, the pointer is bad and must not be B
Y
used.
S S
U U
L  Every pointer contains garbage value before assignment of some valid L
A A
V address. V

N N
E
 Correct code overwrites the garbage value with a correct reference to E
P P
A an address and thereafter the pointer works fine. A
L L

338  We have to program carefully. 3/9/2025 6:46 AM


P P
R
E Void Pointer R
E
P P
A
R
 Void pointer is a special type of pointer. A
R
E E
D D
 It can point to any data type, from an integer value to a float to a
B B
Y string of characters. Y

S S
U U
L  Using void pointer, the pointed data can not be referenced directly L
A
V
(i.e. * operator can not be used on them) A
V

N N
E  Type casting or assignment must be used to change the void pointer to E
P P
A a concrete data type to which we can refer. A
L L

339 3/9/2025 6:46 AM


P P
R
E Void Pointer in C: Definition R
E
P P
A  Suppose we have to declare integer pointer, character pointer and float pointer then we need to A
R declare 3 pointer variables. R
E E
D  Instead of declaring different types of pointer variable it is feasible to declare single pointer variable D
which can act as integer pointer, character pointer.
B B
Y Y
 Declaration of void pointer:
S void *pointer_name;
S
U U
L  In C, general purpose pointer is called as void pointer. L
A A
V V
 It does not have any data type associated with it.
N N
E  It can store address of any type of variable. E
P P
A  A void pointer is a C convention for a raw address. A
L L

340
 The compiler has no idea what type of object a void pointer really points to. 3/9/2025 6:46 AM
P P
R
E Example R
E
P P
A
R
void *ptr; //declaration of void pointer A
R
E
D
char cnum; //declaration of character type variable E
D

B
int inum; //declaration of integer type variable
B
Y float fnum; //declaration of float type variable Y

S ptr=&cnum; //ptr has address of character data S


U U
L ptr=&inum; //ptr has address of integer data L
A A
V ptr=&fnum; //ptr has address of float data V

N N
E E
P P
A A
L L

341 3/9/2025 6:46 AM


P P
R
E Chain of Pointers R
E
P P
A  A pointer variable can be assigned the address of an ordinary variable.
A
R R
E  Now, this variable itself could be another pointer (i.e. a pointer can contain address of another pointer)
E
D #include<stdio.h>
#include<conio.h> D
void main( )
B { B
Y int i = 100; Y
int *pi;
S int **pii;
S
U pi = &i;
pii = &pi;
U
L L
printf ("Address of i = %u \n", &i);
A printf ("Address of i = %u \n", pi); A
V printf ("Address of i = %u \n", *pii); V
printf ("Address of pi = %u \n", &pi);
N printf ("Address of pi = %u \n", pii);
N
E printf ("Address of pii = %u \n", &pii);
printf ("Value of i = %d \n", i);
E
P P
printf ("Value of i = %d \n", *(&i));
A printf ("Value of i = %d \n", *pi); A
L printf ("Value of i = %d", **pii); L
getch();
342 } 3/9/2025 6:46 AM
P P
R
E Pointer Arithmetic R
E
P P
A  Pointer address can be manipulated by arithmetic operators with integer values. A
R R
E E
D  These arithmetic operators are only + and – and increment and decrement operators.
D

B  The increment and decrement of pointer variable depends on its data types. B
Y Y
#include<stdio.h>
S #include<conio.h>
S
U void main() U
L { L
A int *p; A
V int age=17; V
p=&age;
N N
E
printf("\nValue of age is %d", age);
E
P printf("\n Increment on age is %d", ++age);
P
A printf("\n Address of age is %u", p); A
L printf("\n Increment in pointer is %u", ++p); L
getch();
343 } 3/9/2025 6:46 AM
P P
R
E NULL Pointer R
E
P P
A  A null pointer is a special pointer value that points nowhere or nothing. A
R R
E E
D D
 The predefined constant NULL in stdio.h is used to define null pointer.
B B
Y #include<stdio.h>
Y
#include<conio.h>
S S
U #define NULL 0 U
L void main() L
A A
V { V

N
int *ptr=NULL; //double/float/char/void *ptr=NULL
N
E if(ptr==NULL) E
P P
A printf("Change NULL to 1 and there will be warnings!!!"); A
L getch(); L

344 } 3/9/2025 6:46 AM


P P
R
E Double Pointer (Pointer to Pointer) R
E
P P
A
R
 C allows the use of pointers that point to other pointers and these in A
R
E turn, point to data. E
D D

B B
Y  For pointers to do that, we only need to add asterisk (*) for each level Y

S of reference. For example: S


U int a=20; U
L L
A int *p; A
V V
int **q;
N
E
p=&a; N
E
P q=&p; P
A A
L L

345 3/9/2025 6:46 AM


P P
R
E Double Pointer (Pointer to Pointer) R
E
P P
A #include<stdio.h> A
R R
E #include<conio.h> E
D void main() D

B { B
Y int var=25; Y

S int *ptr; S
U int **pptr; U
L L
A ptr=&var; A
V pptr=&ptr; V

N printf("Value of var = %d\n",var); N


E printf("Value available at *ptr = %d\n",*ptr); E
P P
A printf("Value available at **pptr = %d\n",**pptr); A
L getch(); L

346 } 3/9/2025 6:46 AM


P P
R
E Pointers and Arrays R
E
P P
A  Pointers and arrays are so closely related. A
R R
E E
D  An array declaration such as int arr[5] will lead the compiler to pick an address to store a
sequence of 5 integers, and arr is a name for that address. D

B B
Y  The value is not the first integer in the sequence, nor is it the sequence in its entirety. It is Y
just an address.
S S
U U
L  Now, if arr is a one-dimensional array, then the address of the first array element can be L
A written as &arr[0] or simply arr. A
V V
 Moreover, the address of the second array element can be written as &arr[1] or simply
N N
E
(arr+1).
E
P P
A  It is not possible to assign an arbitrary address to an array name or to an array element. A
L (i.e. expressions such as arr, (arr+i), and arr[i] cannot appear on the left side of an L
assignment statement.
347 &arr[0]=&arr[1]; //invalid 3/9/2025 6:46 AM
P P
R
E Pointers and Arrays R
E
P P
A /*Program that accesses array elements of a one-dimensional array A
R using pointers*/ R
E E
D #include<stdio.h>
D
#include<conio.h>
B
Y
void main() B
Y
{
S int arr[5] = {10, 20, 30, 40, 50}; S
U U
L int i; L
A for (i = 0; i < 5; i++) A
V
{ V

N printf ("i=%d\t arr[i]=%d\t *(arr+i)=%d\t", i, arr[i], *(arr+i)); N


E printf ("&arr[i]=%u\t arr+i=%u\n", &arr[i], (arr+i)); E
P P
A } A
L getch(); L
} 3/9/2025 6:46 AM
348
P P
R
E Pointers and Arrays R
E
P P
A /*WAP to calculate average marks of 5 students in a subject using pointer*/ A
R #include<stdio.h> R
E #include<conio.h> E
D D
void main()
B { B
Y float marks[5],sum=0; Y
int i;
S S
float avg;
U U
L printf("Enter marks of 5 students:\n");
L
A for(i=0;i<5;i++) A
V { V
scanf("%f",marks+i);
N N
sum+=*(marks+i);
E E
P }
P
A avg=sum/5; A
L printf("\nThe average is %f.",avg); L
getch();
349 3/9/2025 6:46 AM
}
P P
R
E Pointers and Character Strings R
E
P P
A  As we have seen in strings, a string in C is an array of characters A
R R
E ending in the null character (written as ‘\0’), which specifies where E
D the string terminates in memory. D

B B
Y Y
 Like in one-dimensional arrays, a string can be accessed via a pointer
S to the first character in the string. S
U U
L L
A A
V  The value of a string is a (constant) address of its first character. V

N N
E
 Thus, it is appropriate to say that a string is a constant pointer. E
P P
A A
L L
 A string can be declared as a character array or a variable of type
350 char *. 3/9/2025 6:46 AM
P P
R
E Pointers and Character Strings R
E
P P
A
R
 The declarations can be done as shown below: A
R
E char country[]=“NEPAL”; E
D D
char *country=“NEPAL”;
B B
Y Y

S  The second declaration creates a pointer variable country that points S


U
L
to the letter N in the string “NEPAL” somewhere in memory. U
L
A A
V V
 Once the base address is obtained in the pointer variable country,
N N
E *country would yield the value at this address, which gets printed E
P
A
through, P
A
L printf(“%s”, *country); L

351 3/9/2025 6:46 AM


P P
R
E Pointers and Character Strings R
E
P P
A #include<stdio.h> A
R #include<conio.h> R
E void main() E
D D
{
B char name[10]="Mr. Nepal"; B
Y int i; Y
printf("String Element\t Address\n");
S S
printf("........................\n");
U U
L for(i=0;i<10;i++)
L
A printf("name[%d]=%c \t %u\n", i, name[i], &name[i]); A
V printf("\n\nString Element\t Address\n"); V
printf("........................\n");
N N
for(i=0;i<10;i++)
E E
P printf("name[%d]=%c \t %u\n", i,*(name + i), name + i);
P
A printf("\n%s \t %s \t %s \t %s\n", name, name+1, name+2, name+3); A
L getch(); L
}
352 3/9/2025 6:46 AM
P P
R
E Array of Pointers to Strings R
E
P P
A #include<stdio.h>
A
#include<conio.h>
R R
void main()
E E
{
D char *names[]= {"jump", "walk", "red", "green", "talk", "move", "look", "feel"}; D
int i, j;
B char *temp; B
Y for(i=0;i<7;i++) Y
{
S for(j=i+1;j<8;j++) S
U {
U
L if(strcmp(names[i],names[j])>0)
L
A {
temp=names[i];
A
V V
names[i]=names[j];
names[j]=temp;
N } N
E } E
P } P
A printf("\nSorted List:\n\n"); A
L for(i=0;i<8;i++) L
puts(names[i]);
getch(); 3/9/2025 6:46 AM
353
}
P P
R
E Array of Pointers R
E
P P
A  The way there can be an array of integers, or an array of float numbers, similarly, there can be array A
R of pointers too. R
E E
D  Since a pointer contains an address, an array of pointers would be a collection of addresses. D

B B
Y  For example, a multidimensional array can be expressed in terms of an array of pointers rather than
a pointer to a group of contiguous arrays. Y

S S
U  Two dimensional array can be defined as a one-dimensional array of integer pointers by writing: U
L int *arr[3]; L
A rather than the conventional array definition, A
V int arr[3][5]; V

N N
E  Similarly, an n-dimensional array can be defined as (n-1)-dimensional array of pointers by writing:
E
P data-type *arr[subscript 1] [subscript 2] … [subscript n-1];
P
A A
L  The subscript 1, subscript 2, …, subscript n-1 indicates the maximum number of elements associated L
with each subscript.
354 3/9/2025 6:46 AM
P P
R
E Pointers as Function Arguments R
E
P P
A
R
 Pointers are often used to pass value on the function. A
R
E E
D D
 The value and address can be passed as an argument or parameter to
B B
Y the function and receive as pointer. Y

S S
U U
L  The value of pointer variable can be manipulated as other functions. L
A A
V V

N N
E E
P P
A A
L L

355 3/9/2025 6:46 AM


P P
R
E Pointers as Function Arguments R
E
P P
A /*WAP to pass pointer variable to function sum them and display after returning it.*/ A
R #include<stdio.h> R
E #include<conio.h> E
D int add(int *i, int *j); D
void main()
B B
{
Y Y
int a,b,c=0;
S printf("\n Enter two numbers:");
S
U scanf("%d%d",&a,&b);
U
L c=add(&a,&b); L
A printf("\nSum of two numbers %d and %d is %d", a,b,c); A
V getch(); V
}
N int add(int *p, int *q) N
E E
{
P P
A int temp;
A
L temp=*p+*q;
L
return(temp);
356 } 3/9/2025 6:46 AM
P P
R
E Function Returning Pointers R
E
P P
A  A function can return a single value by its name or return multiple values through pointer parameters.
A
R R
E  Since pointers are a data type in C, we can force a function to return a pointer to the calling function.
E
D #include<stdio.h>
#include<conio.h> D
int * larger(int *,int *);
B void main() B
Y { Y
int a=100,b=50;
S int *p;
S
U p=larger(&a,&b); //function call
printf("%d",*p);
U
L L
getch();
A } A
V int *larger(int *x,int *y) V
{
N if(*x>*y)
N
E return (x); //address of a
else
E
P P
return (y); //address of b
A } A
L L
 Note: The address returned must be the address of a variable in the calling function. It is an error to return a pointer to a local
357 variable in the called function. 3/9/2025 6:46 AM
P P
R
E Dynamic Memory Allocation (DMA) R
E
P P
A  The process of allocating and freeing memory at run time is known as A
R R
E dynamic memory allocation. E
D D

B  This reserves the memory required by the program and returns B


Y Y
valuable resources to the system once the use of reserved space is
S utilized. S
U U
L L
A A
V  Though arrays can be used for data storage, they are of fixed size. V

N
#define m 5
N
E void main() E
P
A
{ int x[m]; P
A
L printf(“%lu\n”,sizeof(x)); L

358
} 3/9/2025 6:46 AM
P P
R
E Dynamic Memory Allocation (DMA) R
E
P P
A  Consider an array size of 100 to store marks of 100 student. A
R R
E E
D  If the number of students is less than 100 say 10, only 10 memory locations will be used
and rest 90 locations are reserved but will not be used. i.e. wastage of memory will occur. D

B B
Y  In such situation DMA will be useful. Y

S S
U  Since an array name is actually a pointer to the first element within the array, it is U
L possible to define the array as a pointer variable rather than as a conventional array. L
A A
V V
 While defining conventional array, system reserves fixed block of memory at the beginning
N
of program execution which is inefficient but this does not occur if the array is represented
in terms of a pointer variable. N
E E
P P
A  The use of pointer variable to represent an array requires some type of initial memory A
L assignment before the array elements are processed. L

359 3/9/2025 6:46 AM


 This is known as Dynamic Memory Allocation (DMA).
P P
R
E Dynamic Memory Allocation (DMA) R
E
P P
A  At execution time, a program can request more memory from a free memory A
R R
E pool and frees if not required using DMA. E
D D

B  Thus, DMA refers allocating and freeing memory at execution time or run B
Y time. Y

S S
U U
L  There are four library functions for memory management:
L
A  malloc() A
V V
 calloc()
N  free() N
E
P
 realloc() E
P
A A
L L
 These functions are defined within header file stdlib.h and alloc.h
360 3/9/2025 6:46 AM
P P
R
E DMA – malloc() R
E
P P
A
R
 It allocates requested size of bytes and returns a pointer to the first A
R
E byte of the allocated space. E
D D
ptr=(data_type*) malloc(size_of_block);
B B
Y Y

S  Here, ptr is a pointer of type data_type. The malloc() returns a S


U
L
pointer to an area of memory with size size_of_block. U
L
A x=(void*) malloc(100*sizeof(int)); A
V V

N N
E  A memory space equivalent to 100 times the size of an integer is E
P
A
reserved and the address of the first byte of the memory allocated is P
A
L assigned to the pointer x of type int. (i.e. x refers to the first address L

361
of allocated memory). 3/9/2025 6:46 AM
P P
R
E Example – malloc() R
E
P P
#include<stdio.h>
A A
#include<stdlib.h>
R struct Emp R
E { E
D int eno;
D
char ename[20];
float esal;
B }; B
Y void main() Y
{
S struct Emp *ptr;
S
ptr=(struct Emp*)malloc(sizeof(struct Emp));
U U
if(ptr==NULL)
L { L
A printf("Memory allocation failed\n"); A
V }
V
else
{
N printf("Enter Employee Number:\n"); N
E scanf("%d",&ptr->eno); E
P printf("Enter Employee Name:\n");
P
A scanf("%s",ptr->ename);
A
printf("Enter Employee Salary:\n");
L scanf("%f",&ptr->esal); L
printf("\nEmployee Number = %d\nEmployee Name = %s\nEmployee Salary = %f",ptr->eno,ptr->ename,ptr->esal);
362 } 3/9/2025 6:47 AM
}
P P
R
E DMA – calloc() R
E
P P
A  The function provides access to the C memory heap, which is available for dynamic allocation of A
R variable-sized blocks of memory. R
E E
D  Unlike malloc(), it accepts two arguments: no_of_blocks and size_of_each_block specifies the size of D
each item.
B B
Y Y
 The function calloc allocates multiple blocks of storage, each of the same size and then sets all bytes
to zero.
S S
U U
L  Calloc initializes all bytes in the allocated block to zero. L
A ptr=(data_type*) calloc(no_of_blocks, size_of_each_block); A
V V
 For example:
N N
x=(int*) calloc(5,10* sizeof(int)); OR
E E
P
x=(int*) calloc(5,20);
P
A A
L  The above statement allocates contiguous space for 5 blocks, each of size 20 bytes. i.e. we can store 5 L
arrays, each of 10 elements of integer types.
363 3/9/2025 6:47 AM
P P
R
E Example – calloc() R
E
P P
A #include<stdio.h> A
R #include<stdlib.h> R
E void main() E
D { D
int n, *arr, i;
B B
printf("Enter size:\n");
Y Y
scanf("%d",&n);
S arr=(int*)calloc(n,sizeof(int));
S
U if(arr==NULL)
U
L printf("Memory allocation FAILED !!"); L
A else A
V { V
printf("Array addresses are:\n");
N for(i=0;i<n;i++) N
E E
{
P P
A printf("%u\n",arr+i);
A
L }
L
}
364 } 3/9/2025 6:47 AM
P P
R
E DMA – realloc() R
E
P P
A  This function is used to modify the size of previously allocated space. A
R R
E E
D  Sometimes the previously allocated memory is not sufficient; we need additional D

B
space and sometime the allocated memory is much larger than necessary.
B
Y Y

S  We can change memory size already allocated with the help of function realloc().
S
U U
L L
A  If the original allocation is done by the statement: ptr=malloc(size); A
V V

N  Then, reallocation of space may be done by the statement: N


E ptr=realloc(ptr,newsize); E
P P
A A
L  This function allocates a new memory space of size newsize to the pointer variable L
ptr and returns a pointer to the first byte of the new memory block and on failure
365 3/9/2025 6:47 AM
the function return NULL.
P P
R
E Example – realloc() R
E
P P
A #include<stdio.h>
A
R #include<stdlib.h>
R
E void main()
E
D {
D
int n, *arr,i;
B printf("Enter size:\n"); B
Y scanf("%d",&n); Y
arr=(int*)calloc(n,sizeof(int));
S n++; S
U arr=(int*)realloc(arr,n*sizeof(int)); U
L if(arr==NULL) L
A printf("Memory allocation FAILED !!"); A
V else V
{
N printf("Address of array elements are:\n"); N
E for(i=0;i<n;i++) E
P { P
A printf("%u\n",(arr+i)); A
L } L
}
366 } 3/9/2025 6:47 AM
P P
R
E DMA – free() R
E
P P
A
R
 Frees previously allocated space by calloc, malloc or realloc functions. A
R
E E
D D
 The memory dynamically allocated is not returned to the system
B B
Y until the programmer returns the memory explicitly. This can be done Y

S using free() function. S


U U
L L
A
V
 This function is used to release the space when not required. A
V

N N
E  Syntax: E
P P
A free(ptr); A
L L

367 3/9/2025 6:47 AM


P P
R
E Example – free() R
E
P P
A #include<stdlib.h> A
R R
E #include <stdio.h> E
D D
void main()
B { B
Y Y
int* ptr = malloc(10 * sizeof(*ptr));
S S
U if (ptr != NULL)
U
L { L
A A
V *(ptr + 2) = 50; V

N printf("Value of the 3rd integer is %d\n",*(ptr + 2));


N
E } E
P P
A free(ptr); A
L
printf("Value of the 3rd integer is %d",*(ptr + 2)); L

368 } 3/9/2025 6:47 AM


P P
R
E Static Memory & Dynamic Memory R
E
P P
A A
R R
S.N. STATIC MEMORY DYNAMIC MEMORY
E E
D 1. Memory is allocated during compile time. Memory is allocated during run time. D
2. Memory is of fixed size throughout the Memory keeps on changing with the change in
B B
Y program. insertion and deletion of memory.
Y

S S
U U
L L
A A
V V

N N
E E
P P
A A
L L

369 3/9/2025 6:47 AM


P P
R R
E E
P P
A A
R R
E E
D D

B B
Y Y

S
END OF UNIT NINE
S
U U
L L
A A
V V

N N
E E
P P
A A
L L

370 3/9/2025 6:47 AM


P P
R R
E E
P P
A A
R R

UNIT 10
E E
D D

B B
Y Y

S FILE HANDLING IN C S
U U
L L
A A
V V

N N
E E
P P
A A
L L

371 3/9/2025 6:47 AM


P P
R
E Syllabus R
E
P P
A
R
 UNIT 10: File Handling in C A
R
E o Concept of File E
D D

B B
Y o Opening and Closing of File (naming, opening, and closing a file)
Y

S S
U o Input Output Operations in File (reading data from file, writing data to U
L
A
a file) L
A
V V

N o Random Access in File (ftell(), fseek(), rewind()) N


E E
P P
A o Error Handling in Files (feof(), ferror()) A
L L
(Note: address some of the functions associated with file handling, e.g. fopen(), fclose(),
372 fgetc(), fputc(), fprintf(), fscanf()) 3/9/2025 6:47 AM
P P
R
E Introduction R
E
P P
A
R
 The input output functions like printf(), scanf(), getchar(), putchar(), A
R
E etc are known as console oriented I/O functions which always use E
D D
input devices and computer screen or monitor for output devices.
B B
Y Y

S  Using these library function, the entire data is lost when either the
S
U program is terminated or the computer is turned off. U
L L
A A
V V
 This problem invites concept of data files in which data can be stored
N N
E on the disks and read whenever necessary, without destroying data. E
P P
A A
L L

373 3/9/2025 6:47 AM


P P
R
E Introduction R
E
P P
A
R
 A file is a place on the disk where a group of related data is stored. A
R
E E
D D
 The data file allows us to store information and to access and alter
B B
Y the information whenever necessary. Y

S S
U U
L  C has various library functions for creating and processing data files. L
A A
V V

N  Mainly there are two types of data files, one is stream oriented or N
E
P
standard or high level and the other is system oriented or low E
P
A level data files. A
L L

374 3/9/2025 6:47 AM


P P
R
E Introduction R
E
P P
A
R
 The standard data files (i.e. stream oriented) are again subdivided A
R
E into text files and binary files. E
D D

B B
Y  The text files consists of consecutive characters and these characters Y

S are interpreted as individual data item. S


U U
L L
A
V
 The binary files organize data into blocks containing contiguous A
V
bytes of information.
N N
E E
P P
A  For each binary and text files, there are number of formatted and A
L
unformatted library functions in C. L

375 3/9/2025 6:47 AM


P P
R
E Introduction R
E
P P
A A
R R
E E
D D

B B
Y Y

S S
U U
L L
A A
V V

N N
E E
P P
A A
L L

376 3/9/2025 6:47 AM


P P
R
E Opening & Closing File R
E
P P
A  Before a program can write to a file or read from a file, the program A
R R
E must open it. E
D D

B  Opening a file establishes a link between the program and the OS. B
Y Y
This provides OS the name of the file and the mode in which the file
S is to be opened. S
U U
L L
A A
V  While working with high level data file, we need buffer area where V

N
information is stored temporarily in the course of transferring data
N
E between computer memory and data file. E
P P
A A
L  The buffer area is established as: L

377 FILE *ptr_variable; 3/9/2025 6:47 AM


P P
R
E Opening & Closing File R
E
P P
A  The buffer area is established as: A
R R
E FILE *ptr_variable; E
D D

B  Here, FILE is a special structure, declared in header file stdio.h B


Y Y

S  The ptr_variable is a pointer “pointer to the data type FILE” that S


U U
L stores the beginning address of the buffer area allocated after a file has L
A been opened. A
V V

N
 This pointer contains all the information about the file and it is used as a N
E E
P communication link between the system and the program. P
A A
L L
 A data file is opened using syntax:
378 3/9/2025 6:47 AM
ptr_variable = fopen(file_name, file_mode);
P P
R
E Opening & Closing File R
E
P P
A  A data file is opened using syntax: A
R R
E
ptr_variable = fopen(file_name, file_mode);
E
D D

B
 The function fopen returns a pointer to the beginning of the buffer area
B
Y associated with the file. Y

S S
U  A NULL value is returned if the file cannot be opened due to some reasons. U
L L
A A
V  After opening a file, we can process data and finally we close it. V

N N
E  Closing a file ensures that all outstanding information associated with the E
P file is flushed out from the buffers. P
A A
L L
 For example:
379 fclose(ptr_variable); 3/9/2025 6:47 AM
P P
R
E File Opening Modes R
E
P P
A Mode Description A
R R
E r Opens an existing text file for reading purpose.
E
D w Opens a text file for writing. If it does not exist, then a new file is created. Here your program D
will start writing content from the beginning of the file.
B B
Y a Opens a text file for writing in appending mode. If it does not exist, then a new file is Y
created. Here your program will start appending content in the existing file content.
S S
r+ Opens a text file for both reading and writing. If the file exists, loads it into memory and set
U U
L
up a pointer to the first character in it. If the file doesn’t exist it returns null.
L
A w+ Opens a text file for both reading and writing. If file is present, it first destroys the file to A
V zero length, otherwise creates a file if it does not exist. V

N a+ Opens a text file for both reading and writing. It creates the file if it doesn’t exist.The
N
E reading will start from the beginning but writing can only be appended. E
P P
A A
L L

380 3/9/2025 6:47 AM


P P
R
E Input Output Operations – String R
E
P P
A
R
 Using string I/O functions fgets() & fputs(), data can be read from a A
R
E file or written to a file in the form of array of characters. E
D D

B B
Y  fgets() is used to read string from file. Y

S fgets(string, int_value, fp); S


U
L
//here, int_value denotes the no. of characters in the string U
L
A A
V V
 fputs() is used to write string to file.
N N
E fputs(string, fp); E
P P
A A
L L

381 3/9/2025 6:47 AM


P P
R
E Example – I R
E
P P
A /* Program to create a file named test.txt and write some text “I study B.Sc. CSIT” to the file. */
A
R #include<stdio.h>
R
E #include<conio.h>
E
D void main()
D
{
B FILE *fp; B
Y fp=fopen("D:\\test.txt", "w"); Y
if(fp==NULL)
S { S
U printf("\n Cannot create file."); U
L exit(0); L
A } A
V else V
{
N printf("\n File is created."); N
E } E
P fputs("I study B.Sc. CSIT", fp); P
A fclose(fp); A
L getch(); L
}
382 3/9/2025 6:47 AM
P P
R
E Example – II R
E
P P
A /* Program to open the file named test.txt, read its content and display it to screen */
A
#include <stdio.h>
R R
#include<conio.h>
E E
void main()
D { D
FILE *fp;
B char s[100]; B
Y fp=fopen("D:\\test.txt", "r"); Y
if(fp==NULL)
S { S
U printf("\n Cannot open file.");
U
L exit(0);
L
A }
else
A
V V
{
printf("\nFile is opened.");
N } N
E fgets(s,19,fp); E
P printf("\nText from file is: %s", s); P
A fclose(fp); A
L getch(); L
}

383 3/9/2025 6:47 AM


P P
R
E Input Output Operations – Character R
E
P P
A
R
 Using character I/O functions fgetc() & fputc(), data can be read A
R
E from a file or written to a file one character at a time. E
D D

B B
Y  fgetc() is used to read a character from a file. Y

S char_variable = fgetc(fp); S
U U
L L
A
V
 fputc() is used to write a character to a file. A
V
fputc(‘character’ or character_variable, fp);
N N
E E
P P
A A
L L

384 3/9/2025 6:47 AM


P P
R
E End Of File (EOF) R
E
P P
A  EOF is a special character (an integer with ASCII value 26) that indicates that A
R the end-of-file has been reached. R
E E
D D
 This character can be generated from the keyboard by typing Ctrl+Z
B B
Y Y
 It is defined in <stdio.h>
S S
U U
L  When we are creating a file, the special character EOF, is inserted after the last
L
A character of the file by the Operating System. A
V V

N  Thus, the last point of file is detected using EOF while reading data from file.
N
E E
P
 Caution: An attempt to read after EOF might either cause the program to P
A A
L
terminate with an error or result in an infinite loop situation.
L

385 3/9/2025 6:47 AM


P P
R
E Example – I R
E
P P
/*C program to read a file and display file contents character by character using fgetc() */
A #include <stdio.h> A
R #include <conio.h>
R
#include <stdlib.h>
E void main() E
D {
D
/* File pointer to hold reference to our file */
FILE * fPtr;

B char ch;
B
/*Open "test.txt" file in r (read) mode.*/
Y fPtr = fopen("D://test.txt", "r"); Y
/* fopen() return NULL if last operation was unsuccessful */
if(fPtr == NULL)
S { S
U /* Unable to open file hence exit */
U
printf("Unable to open file.\n");
L printf("Please check whether file exists and you have read privilege.\n"); L
A exit(0);
A
}
V /* File open success message */ V
printf("File opened successfully. Reading file contents character by character. \n\n");
do
N { N
E /* Read single character from file */
E
ch = fgetc(fPtr);
P /* Print character read on console */ P
A putchar(ch);
A
} while(ch != EOF); /* Repeat this if last read character is not EOF */
L /* Done with this file, close file to release resource */ L
fclose(fPtr);
getch();

386 } 3/9/2025 6:47 AM


P P
R
E Input Output Operations – Formatted R
E
P P
A
R
 Using formatted I/O functions fprintf() & fscanf(), numbers, A
R
E characters or string can be read from a file or written to a file E
D D
according to our requirement format.
B B
Y Y

S  fprintf() is formatted output function which is used to write integer,


S
U float, char or string value to a file. U
L L
A fprintf(fp,“control_string”, list_of_variables); A
V V

N N
E  fscanf() is formatted input function which is used to read integer, E
P
A
float, char or string value from a file. P
A
L fscanf(fp,“control_string”, &list_of_variables); L

387 3/9/2025 6:47 AM


P P
R
E Example – I R
E
P P
/* Program to create a file named student.txt and write name, roll, address and marks of a student to this file*/
A #include<stdio.h> A
R #include<conio.h>
R
E void main()
E
{
D FILE *fp; D
char name[20];
int roll;
B char address[20]; B
Y float marks;
Y
fp=fopen("D:\\student.txt", "w");
if(fp==NULL)
S { S
U printf("\n File cannot be created or opened.");
U
exit(0);
L } L
A printf("\n Enter name of student:\t");
A
V gets(name);
V
printf("\n Enter roll number of %s:\t", name);
scanf("%d", &roll);

N fflush(stdin);
N
printf("\n Enter address of %s:\t", name);
E gets(address); E
P printf("\n Enter marks of %s:\t", name);
P
A scanf("%f", &marks);
A
printf("\n Now writing data to file...");
L fprintf(fp, "Name=%s\n Roll=%d\n Address=%s\n Marks=%.2f", name, roll, address, marks); L
printf("\n Completed");
fclose(fp);
388 getch(); 3/9/2025 6:47 AM
}
P P
R
E Example – II R
E
P P
/*Given a text file, create another text file deleting the following words “three”, “bad”, and “time”.*/
A #include <stdio.h> A
R #include<conio.h>
R
E void main()
E
{
D FILE *fp,*fpp; D
char c[10];
fp=fopen("D:\\test.txt","r");
B if(fp==NULL) B
Y {
Y
printf("Cannot open file");
exit(0);
S } S
U fpp=fopen("D:\\hello.txt","w");
U
if(fpp==NULL)
L { L
A printf("Cannot create file");
A
V exit(0);
V
}
while(fscanf(fp,"%s",&c)!=EOF)

N {
N
if((strcmp(c,"three")!=0)&&(strcmp(c,"bad")!=0)&&(strcmp(c,"time")!=0))
E { E
P fprintf(fpp,"%s ",c);
P
A }
A
}
L fclose(fp); L
fclose(fpp);
getch();
389 } 3/9/2025 6:47 AM
P P
R
E Random Access in File R
E
P P
A  Till now, reading and writing data from/to a file has been done A
R sequentially. R
E E
D D
 While reading data from a file, the data items are read from the beginning
B B
Y of the file in sequence until the end of file.
Y

S S
U  Also, while writing data to a file, the data items are placed one after the
U
L other in a sequence. L
A A
V V
 This is called sequential access.
N N
E E
P  But we may need to access a particular data item placed in any location P
A without starting from the beginning. A
L L

390  This is called random access or direct access. 3/9/2025 6:47 AM


P
R
Use of file pointer in random access P
R
E
P file E
P
A  A file pointer is a pointer to a particular byte in a file. A
R R
E E
D  While opening a file in write mode, the file pointer is at the beginning of the file, D
and whenever we write to a file, the file pointer moves to the end of the data items
B written so that writing can continue from that point. B
Y Y

S  While opening a file in read mode, the file pointer is at the beginning of the file, S
U and whenever we read from a file, the file pointer moves to the beginning of the U
L next data item so that reading can continue from that point. L
A A
V V
 While opening a file in append mode, the file pointer is at the end of the existing
N file, so that new data items can be written from there onwards. N
E E
P P
A  So, if we are able to move the file pointer according as our need, then any data A
L item can be read from a file or written onto a file randomly. L

391 3/9/2025 6:47 AM


P
R
Functions used in random access P
R
E
P ftell() E
P
A  This function takes a file pointer as an argument and returns a number of A
R R
E type long, that indicates the current position of the file pointer within the E
D file. D

B B
Y  This function is useful in saving the current position of a file, which can be Y

S used later in the program. S


U U
L L
A  Syntax: A
V
n = ftell(fp); V

N N
E E
P  Here, n would give the relative offset (in bytes) of the current position. P
A A
L L
 This means that n bytes have already been read (or written).
392 3/9/2025 6:47 AM
P
R
Functions used in random access P
R
E
P rewind() E
P
A  Do you remember that one way of positioning the file pointer to the beginning of the file A
R is to close the file and then reopen it? R
E E
D D
 The rewind() function can do this without closing the file.
B B
Y  This function takes a file pointer as argument and resets the current position of the file Y
pointer to the start of the file.
S S
U U
L  Syntax: L
A rewind(fp); A
V rewind(fp); V
n=ftell(fp);
N N
E E
P  Here, n would be assigned to 0, because file position has been set to the start of the file by P
A rewind(). A
L L
 The first byte in the file is numbered as 0, second as 1, and so on. 3/9/2025 6:47 AM
393
P
R
Functions used in random access P
R
E
P fseek() E
P
A  This function is used to move the file pointer to a desired position within a A
R file. R
E E
D D
 Syntax:
B B
Y fseek(fp, offset, position); Y
Where, fp is a file pointer, offset is a number or variable data type long, and
S position is an integer number. S
U U
L L
A  The offset specifies the number of positions (bytes) to be moved from the A
V location specified by position. V

N N
E  The position can have one of the following 3 values: E
P P
A  0 – Beginning of file
A
L  1 – Current position L
 2 – End of file
394 3/9/2025 6:47 AM
P P
R
E Error Handling in Files R
E
P P
A
R
 Error situations during I/O operations: A
R
E  Trying to read beyond the end-of-file mark E
D D
 Trying to use a file that has not been opened
B B
Y  Trying to perform an operation on a file, when the file is opened for
Y
another type of operation
S S
U  Opening a file with an invalid filename. U
L L
A A
V V
 Error handling functions:
N
 I/O errors can be detected using two status-inquiry library functions: N
E E
P feof() and ferror(). P
A A
L L

395 3/9/2025 6:47 AM


P P
R
E Error Handling in Files – feof() R
E
P P
A
R
 It is used to test for an end-of-file condition. A
R
E E
D D
 It takes a FILE pointer as its only argument and returns a nonzero
B B
Y integer value if all of the data from the specified file has been read, Y

S and returns zero otherwise. S


U U
L L
A
V
 If fp is a pointer to a file that has just been opened for reading, then A
V
the statement
N N
E if(feof(fp)) E
P
A
printf(“End of data”); P
A
L would display the message “End of data” on reaching the end-of-file L

396
condition. 3/9/2025 6:47 AM
P P
R
E Error Handling in Files – ferror() R
E
P P
A
R
 This function reports the status of the file indicated. A
R
E E
D D
 It takes a FILE pointer as its argument and returns a nonzero integer
B B
Y if an error has been detected up to that point, during processing. Y

S S
U U
L  It returns zero otherwise. L
A A
V V

N  So, the statement N


E
P
if(ferror(fp)!=0) E
P
A printf(“An error has occurred”); A
L L
Would print the error message, if the reading is not successful.
397 3/9/2025 6:47 AM
P P
R
E Error Handling Strategies R
E
P P
A  Addition of new records should take place at the end of existing records in A
R the file. i.e. in the same way new records are added in a register manually. R
E E
D D
 While modifying records, first we must ask the user which record he intends
B B
Y to modify. Instead of asking the record number to be modified, it would be
Y
more meaningful to ask for the name of the student whose record is to be
S modified. On modifying the record, the existing record gets overwritten by S
U the new record. U
L L
A A
V  In deleting records, except for the record to be deleted, rest of the records V

N
must first be written to a temporary file, then the original file must be
deleted, and the temporary file must be renamed back to original. N
E E
P P
A A
L  Displaying all records means displaying the existing records on the screen.
L
Naturally, records should be displayed from first record to last record.
398 3/9/2025 6:47 AM
P P
R R
E E
P P
A A
R R
E E
D D

B B
Y Y

S
END OF UNIT TEN
S
U U
L L
A A
V V

N N
E E
P P
A A
L L

399 3/9/2025 6:47 AM


P P
R R
E E
P P
A A
R R

UNIT 11
E E
D D

B B
Y Y

S INTRODUCTION TO GRAPHICS S
U U
L L
A A
V V

N N
E E
P P
A A
L L

400 3/9/2025 6:47 AM


P P
R
E Syllabus R
E
P P
A
R
 UNIT 11: Introduction to Graphics A
R
E o Concepts of Graphics (graphics.h header file) E
D D

B B
Y o Graphics Initialization and Modes (graphics driver & graphics mode)
Y

S S
U o Graphics Function (Basic functions of graphics.h – example: line(), U
L
A
arc(), circle(), ellipse(), floodfill(), getmaxx(), getmaxy()) L
A
V V

N N
E E
P P
A A
L L

401 3/9/2025 6:47 AM


P P
R
E Introduction R
E
P P
A
R
 There are two modes of the standard output device: A
R
E  Text Mode E
D D
 Graphics Mode
B B
Y Y

S  The programming for video games, animation and multimedia is S


U
L
difficult in text mode as they predominantly work with computer U
L
A graphics. A
V V

N N
E  In graphics mode we work with tiny dots on the screen called pixels E
P
A
(picture elements). P
A
L L

402 3/9/2025 6:47 AM


P P
R
E Introduction R
E
P P
A
R
 The pixels are even present in text mode as they are used to form A
R
E characters that appear on the screen with only difference that they E
D D
are pre-defined pattern of pixels.
B B
Y Y

S  However, the graphics mode provides the ability to manipulate the


S
U individual pixels. U
L L
A A
V V
 To work with graphics, we can use graphics.h header file.
N N
E E
P P
A A
L L

403 3/9/2025 6:47 AM


P P
R
E Graphics Characteristics – Pixels R
E
P P
A
R
 Short for picture element, a pixel is a single point (i.e. dot) in A
R
E graphic image. E
D D

B B
Y  Graphics monitor displays pictures by dividing the display screen into Y

S thousands of pixels arranged in rows and columns. S


U U
L L
A
V
 The pixels are so close together that they appear connected. A
V

N N
E E
P P
A A
L L

404 3/9/2025 6:47 AM


P P
R
E Graphics Characteristics – Pixels R
E
P P
A
R
 The computer screen is a two dimensional; each pixel on the screen A
R
E has some location, illustrated by x and y values. E
D D

B B
Y  x is the horizontal offset from the left side of the screen. Y

S S
U U
L  y is the vertical offset from the top of the screen. L
A A
V V

N  So, x=0 and y=0 represent top left corner of the computer screen. N
E E
P P
A A
L L

405 3/9/2025 6:47 AM


P P
R
E Graphics Characteristics – Resolution R
E
P P
A  The number of pixels used on the screen is called resolution. A
R R
E E
D D
 There are fixed number of rows and each rows contains certain numbers of
B pixels. B
Y Y

S  The frequently used resolutions supported by most of adapters are S


U U
L 640X480, 800X600, 1024X768, 1152X864, 1220X1024, etc. L
A A
V V
 The resolution 640X480 (640 by 480) means that there are 640 pixels
N in horizontal direction (i.e. x-axis) and 480 pixels in vertical direction N
E E
P (i.e. y-axis). P
A A
L L
 In general for higher resolution, the picture is more pleasing.
406 3/9/2025 6:47 AM
P P
R
E Graphics Characteristics – Colors R
E
P P
A
R
 Some graphics modes support more colors than other ranging from 2 A
R
E to million colors. E
D D

B B
Y  A particular mode may support only two colors at a time while other Y

S may support 256 colors. S


U U
L L
A
V
 These groups of colors are known as color palettes. A
V

N N
E E
P P
A A
L L

407 3/9/2025 6:47 AM


P P
R
E Graphics Characteristics – Adapters R
E
P P
A
R
 Video adapters are drivers for display. A
R
E E
D D
 Each video adapter handles graphics in different way.
B B
Y Y

S  Once a video adapter is initialized by the program for particular S


U U
L graphics mode then it can use it to plot various elements as well as to L
A
V
display text in different fonts. A
V

N N
E  Some examples of video adapters are CGA (Color Graphics Adapter), E
P P
A VGA (Video Graphics Array), and EGA (Enhanced Graphics Adapter). A
L L

408 3/9/2025 6:47 AM


P P
R
E Graphics Initialization & Modes R
E
P P
A
R
 Having known about adapters, now let us start knowing on how to A
R
E start switching to graphics mode from text mode. E
D D

B B
Y  In other words, how to start using pixel and resolution concepts. Y

S S
U U
L  This is done by a function called initgraph(). L
A A
V V

N  This initgraph() takes in it 2 main arguments as input namely gd N


E
P
and gm.The 3rd argument specifies the path to driver. E
P
A A
L L
 gd has the number of mode which has the best resolution.
409 3/9/2025 6:47 AM
P P
R
E Graphics Initialization & Modes R
E
P P
A
R
 This is very vital for graphics since the best resolution only gives a A
R
E sharper picture. E
D D

B B
Y  This value is obtained by using the function called as getgraphmode() Y

S in C graphics. S
U U
L L
A
V
 The other argument gm gives insight about the monitor used, the A
V
corresponding resolution of that, the colors that are available since
N N
E this varies based on adapters supported. E
P P
A A
L
 This value is obtained by using the function named as L

410 getmodename() in C graphics. 3/9/2025 6:47 AM


P
R
Auto-Initialization of Graphics P
R
E
P Hardware E
P
A
R
 In the use of initgraph() function we have explicitly told A
R
E initgraph() what graphics driver and mode to use by assigning the E
D D
values to driver and mode arguments.
B B
Y Y

S  It is possible to let the program to find out the video adapter


S
U installed in the computer and use the best driver and mode. i.e. the U
L L
A combination that gives the highest resolution. A
V V

N N
E E
P P
A A
L L

411 3/9/2025 6:47 AM


P
R
Auto-Initialization of Graphics P
R
E
P Hardware E
P
A
R
 There are two approaches for auto initialization of graphics A
R
E hardware. E
D D
 DETECT is used for driver argument. In this method program doesn’t
B know in advance what mode will be used and cannot assume anything B
Y Y
about the resolution. For example:
S int gd, gm; S
U U
L gd=DETECT; L
A initgraph(&gd, &gm,“C:\\TC\\BGI”); A
V V

N N
E  A function called detectgraph() is used that returns value for the best
E
P driver and mode. For example: P
A A
L int gd, gm;
L
detectgraph(&gd, &gm);
412 3/9/2025 6:47 AM
initgraph(&gd, &gm,“C:\\TC\\BGI”);
P P
R
E Closing Graphic Mode R
E
P P
A
R
 Once the program has finished its job using the graphics facilities, A
R
E then it should restore the system to the mode that was previously in E
D D
use.
B B
Y Y

S  If graphics mode is not closed explicitly by the programmer,


S
U undesirable effects may be felt. U
L L
A A
V V
 The closegraph() function is used to restore the screen to the mode
N N
E it was in before we called initgraph() and deallocates all the E
P
A
memory allocated by the graphics system. P
A
L L

413 3/9/2025 6:47 AM


P P
R
E Graphics Function R
E
P P
A A
R R
E
D
 There are numerous graphics functions available in C E
D

B B
Y  But let us see some to have an understanding of how and where a Y

S pixel is placed in each when each of the graphics function gets S


U
L
invoked. U
L
A A
V V

N N
E E
P P
A A
L L

414 3/9/2025 6:47 AM


P P
R
E Graphics Function R
E
P P
A
R
 Function: putpixel(x, y, color); A
R
E  Purpose: E
D D
 The functionality of this function is it puts a pixel or in other words a dot at
B position x, y given in inputted argument. B
Y Y

S  Here one must understand that whole screen is imagined as a graph. S


U U
L L
A  In other words, the pixel at the top left hand corner of the screen represents the A
V value (0, 0). V

N N
E  Here, the color is the integer value associated with colors and when specified the E
P picture element or the dot is placed with the appropriate color associated with that P
A A
L
integer value.
L

415 3/9/2025 6:47 AM


P P
R
E Graphics Function R
E
P P
A
R
 Function: line(x1, y1, x2, y2); A
R
E  Purpose: E
D D
 The functionality of this function is to draw a line from (x1, y1) to (x2, y2).
B B
Y Y
 Here also the coordinates are passed taking the pixel (0, 0) at the top left hand
S corner of the screen as the origin. S
U U
L L
A  And also one must note that the line formed is by using number of pixels placed A
V near each other. V

N N
E E
P P
A A
L L

416 3/9/2025 6:47 AM


P P
R
E Graphics Function R
E
P P
A
R
 Function: getpixel(x, y); A
R
E  Purpose: E
D D
 This function when invoked gets the color of the
B pixel specified. B
Y Y

S  The color got will be integer value associated S


U with that color and hence the function gets an U
L L
integer value as return value.
A A
V V

N N
E E
P P
A A
L L

417 3/9/2025 6:47 AM


P P
R
E Graphics Function R
E
P P
A
R
 Function: circle(x, y, r); A
R
E  Purpose: E
D D
 Draws a circle having center point (x, y) and radius r with current color.
B B
Y Y

S
 Function: ellipse(x, y, startAngle, endAngle, xRadius, yRadius);
S
U  Purpose: U
L L
A  Draws an ellipse with current color. A
V V

N
E
 Function: arc(x, y, startAngle, endAngle, radius); N
E
P  Purpose: P
A A
L  Draws a circular arc in a portion of circle. L

418 3/9/2025 6:47 AM


P P
R
E Graphics Function R
E
P P
A
R
 Function: rectangle(x1, y1, x2, y2); A
R
E  Purpose: E
D D
 Draws rectangle from two end points of a diagonal of the rectangle.
B B
Y Y

S
 Function: getmaxx();
S
U  Returns max x value for current graphics driver and mode. U
L L
A A
V V
 Function: getmaxy();
N N
E  Returns max y value for current graphics driver and mode. E
P P
A A
L L

419 3/9/2025 6:47 AM


P P
R
E Example – 1 R
E
P P
A //Drawing a Circle A
R R
E #include<stdio.h> E
D D
#include<conio.h>
B #include<graphics.h> B
Y Y
void main()
S S
U {
U
L int gd= DETECT, gm; L
A A
V initgraph(&gd,&gm,"c:\\tc\\bgi"); V

N circle(200,100,10);
N
E setcolor(WHITE); E
P P
A getch(); A
L
closegraph(); L

420 } 3/9/2025 6:47 AM


P P
R
E Example – 2 R
E
P P
A //Drawing Lines A
R R
E #include<stdio.h> E
D #include<conio.h> D

B #include<graphics.h> B
Y void main() Y

S { S
U int gd= DETECT, gm; U
L L
A initgraph(&gd,&gm,"c:\\tc\\bgi"); A
V line(90,70,60,100); V

N line(200,100,150,300); N
E setcolor(WHITE); E
P P
A getch(); A
L closegraph(); L

421 } 3/9/2025 6:47 AM


P P
R
E Example – 3 R
E
P P
A //Drawing rectangle A
R R
E #include<stdio.h> E
D D
#include<conio.h>
B #include<graphics.h> B
Y Y
void main()
S S
U {
U
L int gd= DETECT, gm; L
A A
V initgraph(&gd,&gm,"c:\\tc\\bgi"); V

N rectangle(200,100,150,300);
N
E setcolor(WHITE); E
P P
A getch(); A
L
closegraph(); L

422 } 3/9/2025 6:47 AM


P P
R
E Example – 4 R
E
P P
A //Constructing Triangle A
R R
E
#include<stdio.h>
E
D #include<conio.h> D
#include<graphics.h>
B B
Y void main() Y
{
S S
U int gd= DETECT, gm;
U
L initgraph(&gd,&gm,"c:\\tc\\bgi"); L
A line(200,100,10,20); A
V V
line(10,20,50,60);
N line(50,60,200,100); N
E E
P setcolor(WHITE);
P
A getch(); A
L
closegraph(); L

423 } 3/9/2025 6:47 AM


P P
R R
E E
P P
A A
R R
E E
D D

B B
Y Y

S
END OF UNIT ELEVEN
S
U U
L L
A A
V V

N N
E E
P P
A A
L L

424 3/9/2025 6:47 AM


P P
R R
E E
P P
A A
R R
E E
D D

. . . . . BEST WISHES . . . . .
B B
Y Y

S S
U
L KEEP LEARNING, HOW TO CODE ☺ U
L
A A
V V

N N
E E
P P
A A
L L

425 3/9/2025 6:47 AM

You might also like