C Programming - Complete Slide Contents
C Programming - Complete Slide Contents
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
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
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
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
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
S S
U U
L Flow lines have arrows (direction of flow) L
A A
V V
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
B B
Y Placed in the source program before the main function. Y
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
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
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
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
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
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
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
N
is compiled.
N
E E
P P
A The input/output functions are classified into two types: A
L
Formatted functions 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
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
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
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
B
{
B
Y char chr = 'a'; To print char, we use %c format Y
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
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
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
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
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
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
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
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
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
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
N N
E E
P P
A A
L L
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
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
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
N 𝑓𝑙𝑜𝑎𝑡/𝑖𝑛𝑡 = 𝑓𝑙𝑜𝑎𝑡 N
E E
P P
A A
L L
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
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
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
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
B
void main()
B
Y { int num1, num2, larger; 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
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
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
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
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
B
{
B
Y int num1=45, num2=25,AND, OR, XOR; Y
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
B B
Y There are two bitwise shift operators: Y
N N
E E
P P
A A
L L
B B
Y The leftmost n bits in the original bit pattern will be lost and the Y
B B
Y The empty leftmost n bits position will be filled with 0’s, if the Y
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
B B
Y Y
S S
U U
L L
A A
V V
N N
E E
P P
A A
L L
B B
Y Y
S S
U U
L L
A A
V V
N N
E E
P P
A A
L L
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
B B
Y sizeof Operator: Y
N N
E E
P P
A A
L L
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
B
{
B
Y short int a = 0; Y
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
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
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
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
B B
Y Errors may occur unwillingly which may prevent the program to Y
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
S S
U U
L Similarly, it is necessary to perform repeated actions or skip some
L
A statements. A
V 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
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
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
B B
Y If the Boolean expression evaluates to false, then the first set of code 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
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
B B
Y Y
S S
U U
L L
A A
V V
N N
E E
P P
A A
L L
B B
Y C programming language assumes any non-zero and non-null values Y
N N
E E
P P
A A
L 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
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
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
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
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
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
N
terminating the execution of a loop.
N
E E
P P
A A
L L
B B
Y Programming languages provide various control structures that allow 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
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
B B
Y A do…while loop is similar to a while loop, except the fact that it is Y
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
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
N N
E E
P P
A A
L L
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
N printf("%d",i);
N
E printf("\n"); E
P P
A } A
L
getch(); L
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
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
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
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
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
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
B
void main()
B
Y { Y
N getch(); N
E E
P } P
A A
L L
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
B B
Y Multi-dimensional arrays are defined in the same way as one Y
B B
Y Here, dim1, dim2, … , dimN are positive valued integer expressions Y
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
B B
Y Syntax: Y
B B
Y 𝑖𝑠 𝑒𝑞𝑢𝑖𝑣𝑎𝑙𝑒𝑛𝑡 𝑡𝑜 Y
S S
U U
L L
A 𝑚𝑎𝑡𝑟𝑖𝑥 0 0 = 2; 𝑚𝑎𝑡𝑟𝑖𝑥 1 0 = 8; A
V V
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
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
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
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
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
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
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
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
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
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
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
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
S S
U U
L You can divide up your code into separate functions. L
A A
V 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
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
N N
E E
P P
A A
L L
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
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
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
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
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
B B
Y The job of return statement is to hand over some value given by Y
B B
Y A function may or may not return a value. If a function doesn’t Y
N N
E E
P P
A A
L L
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
variable_name=function_name();
N
E E
P P
A A
L L
N N
E E
P P
A A
L L
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
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
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
B B
Y Here, the value of each actual argument is copied into corresponding Y
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
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
B A variable is declared static using the keyword static. i.e. 𝑠𝑡𝑎𝑡𝑖𝑐 𝑖𝑛𝑡 𝑥; B
Y Y
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
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
UNIT 8
E E
D D
B B
Y Y
N N
E E
P P
A A
L L
B o Array of structure B
Y Y
N
o Structure within structure (nested structure) N
E E
P P
A
L
o Union A
L
B B
Y To store information about a student, we would require to store the Y
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
B B
Y In our previous structure example, if we want to keep record of 50 Y
N N
E E
P P
A A
L L
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
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
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
N N
E E
P P
A A
L L
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
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
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
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
B B
Y Y
S S
U U
L L
A A
V V
N N
E E
P P
A A
L L
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
S S
U Invalid Example: U
L L
A int *p; A
V V
float num;
N
E
p=# N
E
P P
A A
L L
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
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
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
B B
Y For example: Y
S int num; S
U
L
int *ptr=# 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=# L
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
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
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
N N
E E
P P
A A
L L
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
B B
Y For pointers to do that, we only need to add asterisk (*) for each level Y
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
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
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
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
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
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
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 N
E Syntax: E
P P
A free(ptr); A
L L
S S
U U
L L
A A
V V
N N
E E
P P
A A
L L
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
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
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
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
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
B B
Y The text files consists of consecutive characters and these characters Y
B B
Y Y
S S
U U
L L
A A
V V
N N
E E
P P
A A
L L
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
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
B B
Y fgets() is used to read string from file. Y
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
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
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();
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
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
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
B B
Y This function is useful in saving the current position of a file, which can be Y
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
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
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
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
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
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
B B
Y Graphics monitor displays pictures by dividing the display screen into Y
N N
E E
P P
A A
L L
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
B B
Y A particular mode may support only two colors at a time while other Y
N N
E E
P P
A A
L L
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
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
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
N N
E E
P P
A A
L L
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
B B
Y But let us see some to have an understanding of how and where a Y
N N
E E
P P
A A
L L
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
N N
E E
P P
A A
L L
N N
E E
P P
A A
L L
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
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
N circle(200,100,10);
N
E setcolor(WHITE); E
P P
A getch(); A
L
closegraph(); L
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
N rectangle(200,100,150,300);
N
E setcolor(WHITE); E
P P
A getch(); A
L
closegraph(); L
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
. . . . . 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