0% found this document useful (0 votes)
15 views118 pages

Understanding Algorithms and C Programming

The document provides a comprehensive overview of algorithms, flowcharts, and the C programming language, including their definitions, characteristics, and applications. It covers various programming concepts such as data types, operators, decision-making, loops, arrays, pointers, functions, strings, structures, unions, dynamic memory allocation, and file handling. Each section includes detailed explanations, examples, and the significance of these concepts in computer science.

Uploaded by

Giridhar Gowda
Copyright
© Public Domain
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views118 pages

Understanding Algorithms and C Programming

The document provides a comprehensive overview of algorithms, flowcharts, and the C programming language, including their definitions, characteristics, and applications. It covers various programming concepts such as data types, operators, decision-making, loops, arrays, pointers, functions, strings, structures, unions, dynamic memory allocation, and file handling. Each section includes detailed explanations, examples, and the significance of these concepts in computer science.

Uploaded by

Giridhar Gowda
Copyright
© Public Domain
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Contents

1. Introduction to Algorithms. 1
------------------------------------------------------------------------------------------------------------
1.1 Definition of Algorithms. 1
1.2 Characteristics of Algorithms. 1
1.3 Role of Algorithms. 1
1.4 Algorithm Examples. 1
a) Addition of two number. 1
b) Given number is odd or even, 2
c) Largest of three numbers. 2
d) Factorial of a number . 2
e). Given number is prime or not. 2
f). Generate Fibonacci Series. 3
g). Roots of Quadratic equation. 3
h). Palindrome of a given number. 4
i). Search an element using linear search technique. 4
j). Search an element using Binary search technique. 4

2. Introduction to Flowcharts. 6
----------------------------------------------------------------------------------------------------------------
2.1 Importance of Flowchart. 6
2.2 Working of Flowcharts . 6
2.3 Common flowchart symbols. 7
2.4 Key uses of flowcharts. 7
2.5 Writing Flowchart. 8

3. Overview of C Language. 11
----------------------------------------------------------------------------------------------------------------
3.1 History of C Language. 11
3.2 Applications of C Language. 11
3.3 Structure of C Program. 12
3.4 Complete Program to understand C coding structure. 15

4. C Tokens and Data Types. 17


----------------------------------------------------------------------------------------------------------------
4.1 C Tokens. 17
4.2 Data Types in C. 18

5. Variables. Constants and Literals. 21


----------------------------------------------------------------------------------------------------------------
5.1 Variables. 21
5.2 Constants and Literals. 23

6. Input and Output. 28


----------------------------------------------------------------------------------------------------------------
6.1 Input. 28
6.2 Output. 28
6.3 Format Specifiers. 29
7. Operators in C. 31
----------------------------------------------------------------------------------------------------------------
7.1 Arithmetic Operators. 31
7.2 Increment and Decrement Operators. 32
7.3 Assignment Operator (=). 33
7.4 Relational Operators. 34
7.5 Logical Operators. 35
7.6 Bitwise operators. 36
7.7 Comma Operator. 38
7.8 Sizeof Operator. 38
7.9 Operators Precedence in C. 39

8. Decision Making. 41
----------------------------------------------------------------------------------------------------------------
8.1 if Statement. 41
8.2 if…else Statement. 42
8.3 Nested if Statements. 44
8.4 Switch Statement. 45
8.5 Conditional operator (:?). 47

9. Loops. 48
--------------------------------------------------------------------------------------------------------------------------
9.1 Purpose of Loops. 48
9.2 Benefits of Using Loops. 48
9.3 Types of Loops. 48

10. Arrays. 53
----------------------------------------------------------------------------------------------------------------
10.1 Characteristics of an array. 53
10.2 Applications of Arrays. 53
10.3 Declaring Arrays. 54
10.4 Initializing Arrays. 54
10.5 Reading and Printing Array Elements with for Loop. 54
10.6 Accessing Array Elements. 55
10.7 Linear Search. 56
10.8 Binary Search. 57
10.9 Bubble sort. 58
10.10 Two-dimensional Array. 59
10.11 Addition of Two Matrices. 61
10.12 Multiplication of two matrices. 62

11. Pointers. 65
----------------------------------------------------------------------------------------------------------------
11.1 Memory Organization. 65
11.2 Benefits of Using Pointers:. 66
11.3 Declaring a Pointer. 66
11.4 Initialize the Pointer. 67
11.5 Addition of two number using pointers. 68
11.6 Types of Pointers. 69
11.7 Using Pointers on Array. 71
11.8 Accessing Array Elements Using Pointers. 71
11.9 Modifying Array Values with Pointers. 71

12. Functions. 72
---------------------------------------------------------------------------------------------------------------------------
12.1 Benefits of Using Functions. 72
12.2 Types of functions . 72
12.3 Function Definition. 72
12.4 Function Call. 73
12.5 Function return type. 73
12.6 Passing Parameters to Function. 74
12.7 Factorial of number using function. 75
12.8 Recursion. 76
12.9 Passing arrays to Function. 77
12.10 Call by value and Call by Reference. 79
12.11 Passing Multidimensional Arrays. 81

13. Strings. 83
-----------------------------------------------------------------------------------------------------------------------
13.1 Declaration. 83
13.2 Initialization. 83
13.3 Accessing Characters. 84
13.4 Basic Operations on strings.. 85

14. Structures. 88
---------------------------------------------------------------------------------------------------------------------------
14.1 Key Characteristics. 88
14.2 Use Cases. 88
14.3 Defining Structures. 88
14.4 Creating Structure Variables. 89
14.5 Copying Structure. 90
14.6 Nested Structures. 92
14.7 Pointer to a structure. 93

15. Unions. 95
---------------------------------------------------------------------------------------------------------------------------
15.1 Key points of Union. 95
15.2 Key Differences from Structures. 95
15.3 Union Declaration. 95
15.4 Initialization and Accessing. 96
15.5 Comparison between structures and unions. 97

16. Dynamic Memory Allocation. 100


---------------------------------------------------------------------------------------------------------------------------
16.1 Need for Dynamic Memory Allocation. 100
16.2 Benefits of Dynamic Memory Allocation. 100
16.3 Problems with Dynamic Memory Allocation. 100
16.4 Common Functions of Dynamic Memory Allocation. 101
16.5 Programming example for Static Memory Allocation. 101
16.6 Programming example for Dynamic Memory Allocation. 101
17. Handling Files. 106
----------------------------------------------------------------------------------------------------------------
17.1 Types of Files:. 106
17.2 File Modes . 106
17.3 Creating a File in C. 107
17.4 Write to a File. 108
17.5 Reading From a File. 109
17.6 Closing a File. 111
1. Introduction to Algorithms

1.1 Definition of Algorithms


An algorithm is a finite sequence of well-defined instructions for solving a specific problem or
performing a computation. It's a step-by-step procedure that takes input data, processes it, and
produces output. Algorithms are fundamental to computer science, enabling computers to
perform tasks, make decisions, and automate processes.

1.2 Characteristics of Algorithms:


 Finite: An algorithm must terminate after a specific number of steps, meaning it cannot
run forever.
 Well-defined: Each step in the algorithm must be clear, unambiguous, and precisely
defined.
 Input: Algorithms take input data, which can be of any size, and process it to produce
output.
 Output: Algorithms produce a result or outcome based on the input and the steps
executed.
 Correctness: Algorithms should produce the correct output for a given input.
 Efficiency: Good algorithms are efficient in terms of time and memory usage.

1.3 Role of Algorithms:


 Automation: Algorithms enable computers to automate tasks, making them more
efficient and reliable.
 Problem-solving: They provide a structured approach to solving complex
computational problems.
 Decision-making: Algorithms can be used to make decisions based on data and rules.
 Optimization: Algorithms can be designed to optimize processes and improve
performance.

1.4 Algorithm Examples

A). Addition of Two numbers.

Step 1: Start
Step 2: Input first number as P
Step 3: Input second number as Q
Step 4: Set Sum=P+Q
Step 5: Print Sum
Step 6: End.

1
B). Given number is odd or even
Step 1: Start
Step 2: Read a number to N
Step 3: Divide the number by 2 and store the remainder in R.
Step 4: If R = O Then go to Step 6
Step 5: Print “N is odd” go to step 7
Step 6: Print “N is even”
Step 7: End

C). Largest of Three numbers


Step 1: Start
Step 2: Declare variables num1, num2, num3 and maxNum.
Step 3: Read values for num1, num2 and num3
Step 4: Set maxNum = num1
Step 5: If num2 > maxNum, set maxNum = num2
Step 6: If num3 > maxNum, set maxNum = num3
Step 7: Display maxNum as the greatest among the three numbers
Step 8: End

D). Factorial of a number


Step 1 : Start.
Step 2 : Read n.
Step 3 : Initialize counter variable i to 1 and fact to 1.
Step 4 : if i <= n go to step 5 otherwise goto step 7.
Step 5 : calculate fact = fact * i.
Step 6 : increment counter variable i and goto step 4.
Step 7 : Write fact.
Step 8 : Stop.

E). Given number is prime or not.


Step 1 : Start.
Step 2 : Read N.
Step 3 : start the loop from i= 2 to i=N-1
Step 4 : If N is divided by "i" in the loop then break the loop
Step 5 : If(i==N) # that means loop has executed till the end of range so N is not
divided by any number So it is a prime number.
Step 6: Else it is not a prime number.
Step 7: End

2
F). Generate Fibonacci Series
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144….

Step 1 : Start.
Step 2 : Declare variables i, a,b , show
Step 3 : start the loop from i= 2 to i=N-1
Step 4 : Initialize the variables, a=0, b=1, and show =0.
Step 5 : Enter the number of terms of Fibonacci series to be printed.
Step 6: Print First two terms of series
Step 7: Use loop for the following steps
show=a+b
a=b
b=show
increase value of i each time by 1
print the value of show
Step 8: End

G). Roots of Quadratic equation

Step 1 : Start.
Step 2 : Input Coefficients: Obtain the values of the coefficients a,b and c
Step 3 : Calculate Discriminant: Compute the discriminant, 𝐷=𝑏2−4𝑎
Step 4 : Determine Nature of Roots:
* If D>0 (Real and Distinct roots)

 Calculate root1 = (-b+√𝐷)/2a


 Calculate root2 =(-b-√𝐷)/2a
 Display root1 and root2

* If D=0 (Real and Equal root)


* Calculate root1=root2=(-b)/2a
* Display root1 and root2

* If D<0 (Imaginary roots)

 Calculate the real part = (-b)/2a


 Calculate the imaginary part = (-√𝐷)/2a
 Display real part+[Link] part, real [Link]
part
Step 5. End

3
H). Palindrome of a Given number
Step1: Start.
Step2: Input the number n.
Step3: Store the original number in a variable original.
Step4 :Reverse the number:
 Initialize rev = 0.
 While n > 0:
o Get the last digit: digit = n % 10
o Update reverse: rev = rev * 10 + digit
o Remove last digit from n: n = n / 10
Step5: Compare the reversed number rev with the original.
Step6 : If rev == original:
Print It's a palindrome.
else
Print Not a palindrome.
Step 7. End

I). Search an element using linear search technique.

Step1: Start
Step2: An array/list A of size n
Step3: A value target to search for
Step4: Set i = 0 ← (initialize index)
Step5: Repeat while i < n:
If A[i] == target:
Return i ← (target found at index i)
Else:
Increment i by 1
Step6: End of loop: If target was not found in the array
Return -1
Stop7: End

J). Search an element using Binary search technique.


Step1: Start
Step2: An array/list A of size n
Step3: A value target to search for
Step4: Set low = 0 and high = n - 1
Step5: Repeat while low <= high:
Calculate mid = (low + high) // 2 (integer division)
If A[mid] == target:
Return mid ← (target found)
Else if A[mid] < target:
Set low = mid + 1 ← (search in the right half)

4
Else:
Set high = mid - 1 ← (search in the left half)
Step 6: End of loop: If target not found
Return -1
Step7: Stop

5
1. Introduction to Flowcharts

A flowchart is a graphical representation of a process or algorithm, using standardized symbols


and arrows to depict the sequence of steps, decisions, and data flow from beginning to end. It
simplifies complex systems by providing a clear, visual pathway that aids in communication,
documentation, troubleshooting, and process improvement across various fields like project
management, programming, and engineering.

2.1 Importance of Flowchart


Clarity and Simplification:
They break down complex procedures into easy-to-understand steps, making it easier for
people to interpret and understand the process.
Communication:
Flowcharts provide a universal visual language that helps team members and stakeholders
stay on the same page, reducing miscommunication.
Documentation and Training:
They serve as a clear record of a process, essential for training new employees and
documenting standard operating procedures.
Troubleshooting and Improvement:
By visualizing a workflow, teams can identify bottlenecks, inefficiencies, and opportunities
to optimize the process.
Standardization:
They provide a standardized way to map out processes, ensuring consistency and helping to
maintain quality and compliance.

2.2 Working of Flowcharts


Flowcharts break down a process into its core components and visually map the sequence of
activities.

 Input and initialization: The process starts with a clearly defined beginning and an input of
data.

 Steps and logic: The flow moves through a series of actions (represented by rectangles) and
decision points (diamonds) that determine the path forward.

 Output and termination: The process concludes with a final output or result and a clearly
defined end.

6
2.3 Common flowchart symbols
Universal symbols, standardized by the International Organization for Standardization (ISO),
have specific meanings within a flowchart.

 Oval or rounded rectangle: Represents the start and end of a process.

 Rectangle: Represents a process, action, or operation step.

 Diamond: Indicates a decision point, typically a question with a "yes" or "no" answer, which
leads to different paths.

 Parallelogram: Represents input or output of data.

 Arrows (flowlines): Show the direction of flow and the order of steps.

 Circle (on-page connector): Connects different parts of a flowchart on the same page.

 Pentagon (off-page connector): Indicates that the process continues on another page.

Figure 2.1. Flowchart Symbols

2.4 Key uses of flowcharts


Flowcharts are a versatile tool used across many disciplines, including business, software
engineering, and education.

 Simplifying complex processes: They make it easy to understand and analyze complicated
workflows by providing a clear, visual overview.

7
 Improving efficiency: By visualizing a process, you can identify and eliminate unnecessary or
redundant steps, or find bottlenecks that cause delays.

 Enhancing communication: They provide a standardized, graphical language that all


stakeholders can understand, regardless of their technical background.

 Debugging and documentation: In software development, flowcharts are used to plan program
logic and help developers find errors or document how a system works.

 Training: They can be used as a step-by-step instructional guide for training new employees.

2.5 Writing Flowchart


While flowcharts following points have to be considered:
Defining the problem.
Identify the various steps required to form a solution.
Determine the required input and output parameters.
Get expected input data values and output result.
Determine the various computations and decisions involved.
With this background of flowcharts and flowchart symbols let us now draw some
sample flowcharts. First we shall write the steps to prepare the flowchart for a particular
task and then draw the flowchart.

Example : To prepare a flowchart to add two numbers. The steps are :

[Link].
[Link] two numbers N1 and N2.
[Link] them.
[Link] the result.
[Link].

8
Figure 2. Flow charts for Addition of Two numbers and Biggest of Two Numbers

Figure 3. Flow charts for Celcius to Farenheit and Inches to centimeter Conversion

9
Figure 4. Flow charts for Sum of N numbers and Reading a character until Z character

10
3. Overview of C Language
C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie
to develop the UNIX operating system at Bell Labs. C was origin
ally first implemented on the DEC PDP-11 computer in 1972. In 1978, Brian Kernighan and
Dennis Ritchie produced the first publicly available description of C, now known as the K&R
standard. The UNIX operating system, the C compiler, and essentially all UNIX application
programs have been written in C. C has now become a widely used professional language for
various reasons.
 Easy to learn
 Structured language
 It can handle low-level activities
 It can be compiled on a variety of computer platforms

3.1 History of C Language


C was created by Dennis Ritchie at Bell labs in the early 1970s as a system programming
language, building upon the B language and incorporating features from other languages like
ALGOL AND BCPL. C was designed to be efficient and portable , allowing programmers to
have fine-grained control over hardware while still enabling the creation of complex software.
C became the primary language for developing the Unix operating and its use expanded to
other system software , device drivers, and compilers. To ensure consistency and portability
,the American National Standards Institute (ANSI) standardized C in 1989 and the International
Organization for Standardization (ISO) adopted the standard in 1990, leading to the language
being commonly referred to as “ANSI C” or “ISO C”.C has evolved over time ,with updates
and extensions , such as the introduction of features in C99 and later standards. It remains
foundational language , influencing the development of other programming languages like C++
and Java.

3.2 Applications of C Language


C was initially used for system development work, particularly the programs that make-up the
operating system. C was adopted as a system development language because it produces code
that runs nearly as fast as the code written in assembly language. Some examples of the use of
C might be:

 Operating Systems
 Language Compilers
 Assemblers
 Text Editors
 Print Spoolers
 Network Drivers
 Databases
 Language Interpreters
 Utilities

11
3.3 Structure of C Program
The structure of a C program is the foundation of writing functional and efficient code.
Learning how a C program is organized helps new programmers grasp the flow and purpose of
each part, from including libraries to writing the main() function. Whether you're a beginner or
an experienced coder, knowing the structure of C program with example ensures that your
programs are not only correct but also well-organized. Here, we’ll break down the key
components of a C program and show how they come together to make it work smoothly. The
C program structure consists of the following sections or components shown in Table 1.1
Table 1.1 Sections or components of C
Component Description
Documentation Comments describing the program’s
purpose.
Preprocessor Directives Includes libraries and macros using #include
and #define.
Definition Section Defines constants or macros used in the
program.
Global Declarations Variables declared globally and accessible
by all functions.
main() Function The entry point of the program where
execution begins.
Variable Declarations Local variables declared inside functions.
Statements and Expression Code logic, including operations, loops, and
conditionals.
Sub Programs Additional user-defined sub-programs or
functions.
Return Statement Indicates the end of program execution, often
with a success code.

Let’s understand the structure of C program with example:


a. Documentation
This section contains comments explaining the purpose of the program. It's not executed by the
compiler, but it helps other programmers (or your future self) understand what the code does.
It’s a good habit to include documentation, especially for larger or collaborative projects.
Example:
// Program to calculate the factorial of a number
b. Preprocessor Directives
Preprocessor directives are instructions given to the compiler before actual compilation
begins. For example, #include is used to include libraries that provide essential functions
like printf( from the standard input-output library (<stdio.h>).

12
Example
#include <stdio.h> // Includes the standard input-output library

c. Definition Section
This section in the C program structure defines constants or macros that can be used throughout
the [Link] like #define are used to assign names to constant values, so if you need to
change the value later, you only update it in one place. For instance, #define MAX 10 assigns
MAX the value of 10, and this value remains the same throughout the program.

Example :

#define MAX 10 // Defines a constant MAX value

d. Global Declarations
Global variables are declared outside any function, usually at the top of the program. These
variables can be accessed and modified by any function in the program. However, global
variables in C should be used cautiously, as they can lead to errors if not handled properly.

Example :

int global_var; // Declares a global variable

e. main() Function

The main() function is the entry point of every C program. It defines where the program starts
executing and returns an integer value (0 means successful execution). Inside the main()
function, you declare variables, define the logic of the program, and call other functions if
necessary.

Example :

int main() {
int n; // Declare a local variable
printf("Enter a number: ");
scanf("%d", &n); // Get user input
printf("Factorial of %d is %d\n", n, factorial(n)); // Call the factorial function
return 0;
}
f. Variable Declarations
Variables in C are used to store data, and they must be declared before they are used. The data
type of the variable (e.g., int, float, char) tells the compiler what type of data the variable will
hold. For instance, declaring int age = 25; assigns the integer value 25 to the variable age.

Example :

13
int num = 5; // Declare and initialize a variable

g. Statements and Expressions


This is where the core logic of the program resides. It includes calculations, control flow
statements (such as if-else, for, while), and function calls. These statements and expressions
form the working part of the program, processing data and providing outputs.
Example :
for(int i = 1; i <= num; i++) {
result *= i; // Multiply result with each number
}
h. Sub Programs (Functions)
Functions, also known as sub-programs, are blocks of code designed to perform specific tasks.
You can create your own functions to make your code modular and reusable. For example, a
factorial function calculates the factorial of a number, and it can be called from the main()
function or other parts of the program.
Example :
int factorial(int n) {
int fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
return fact; // Return the calculated factorial
}
i. Return Statement
The return statement in the main() function signifies the end of the program. Typically, return
0; is used to indicate that the program has been completed successfully. In other functions,
return can be used to return a value to the calling function.
Example :
return 0; // Return 0 indicating successful execution

Below is the general structure of C program with block diagram:

14
Figure 3.1 Basic Structure of C Program

3.4 Complete Program to understand C coding structure


Based on the structure of the C program we discussed above, here is the final and complete
program:
// Program to calculate the factorial of a number
#include <stdio.h> // Preprocessor directive
#define MAX 10 // Definition of a constant
int factorial(int n); // Function declaration
int main() {
int n; // Variable declaration
printf("Enter a number: ");
scanf("%d", &n); // Taking input from user
printf("Factorial of %d is %d\n", n, factorial(n)); // Function call
return 0; // Return statement
}

15
// Function to calculate factorial
int factorial(int n) {
int fact = 1; // Initialize factorial
for (int i = 1; i <= n; i++) { // Loop to calculate factorial
fact *= i;
}
return fact; // Return calculated factorial
}

Output :
Enter a number: 5
Factorial of 5 is 120

16
4. C Tokens and Data Types
You have seen the basic structure of a C program, so it will be easy to understand other basic
building blocks of the C programming language.

4.1 C Tokens
A C program consists of various tokens and a token is either a keyword, an identifier, a
constant, a string literal, or a symbol. For example, the following C statement consists of five
tokens:
printf("Hello, World! \n");
The individual tokens are:
printf
(
"Hello, World! \n"
)
;
Semicolons
In a C program, the semicolon is a statement terminator. That is, each individual statement
must be ended with a semicolon. It indicates the end of one logical entity.
Given below are two different statements:
printf("Hello, World! \n");
return 0;
Comments
Comments are like helping text in your C program and they are ignored by the compiler. They
start with /* and terminate with the characters */ as shown below:
/* my first program in C */
You cannot have comments within comments and they do not occur within a string or character
literals.
Identifiers
A C identifier is a name used to identify a variable, function, or any other userdefined item. An
identifier starts with a letter A to Z, a to z, or an underscore ‘_’ followed by zero or more letters,
underscores, and digits (0 to 9). C does not allow punctuation characters such as @, $, and %
within identifiers. C is a case-sensitive programming language. Thus, Manpower and
manpower are two different identifiers in C. Here are some examples of acceptable identifiers:
tngb raj abc move_name a_123
myname50 _temp j a23b9 retVal

17
Keywords
The following list shows the reserved words in C. These reserved words may not be used as
constants or variables or any other identifier names.
Auto else long switch
Break enum register typedef
case extern return union
char float short unsigned
const for signed void
continue goto sizeof volatile
default if static while
do int struct _Packed double
Whitespace in C
A line containing only whitespace, possibly with a comment, is known as a blank line, and a C
compiler totally ignores it. Whitespace is the term used in C to describe blanks, tabs, newline
characters and comments. Whitespace separates one part of a statement from another and
enables the compiler to identify where one element in a statement, such as int, ends and the
next element begins. Therefore, in the following statement:
int age;
there must be at least one whitespace character (usually a space) between int and age for the
compiler to be able to distinguish them. On the other hand, in the following statement:
fruit = apples + oranges; // get the total fruit
no whitespace characters are necessary between fruit and =, or between = and apples, although
you are free to include some if you wish to increase readability.

4.2 Data Types in C


Data types in C program help define the kind of data a variable can store, like numbers or
letters. Each data type tells the compiler how much memory to allocate and how to interpret
the data. For instance, numbers without decimal points use a type like int, while numbers with
decimal points use float or double. Characters, which store single letters or symbols, use the
char type.. Data types are essential for organizing information efficiently and ensuring your
program works as expected by managing memory properly.
Types of Data Type in C Language
C program data types are classified into three main categories:
1. Basic Data Types: These include fundamental types like int (for integers), char (for
characters), float (for floating-point numbers), and double (for double-precision floating-point
numbers).

18
2. Derived Data Types: These are derived from basic data types and include arrays, pointers,
and functions.
3. User-Defined Data Types: These are created by the programmer and include structures
(struct), unions (union), and enumerations (enum).
Basic Data Types Derived Data Types User-Defined Data Types
int Arrays struct
char Pointers union
float Functions enum
double typedef
void

Basic Data Types with size and range


Different data types in C consume varying amounts of memory. For example, an int uses 2 or
4 bytes, a char uses 1 byte, a float uses 4 bytes, and a double uses 8 bytes. The size of these
data types can vary depending on the system architecture (32-bit vs. 64-bit).The table below
shows the data types in C and their size along with range:
Data Type Memory Size Signed Range Unsigned Range
char 1 byte -128 to 127 0 to 255
short 2 bytes -32,768 to 32,767 0 to 65,535
int 4 bytes -2,147,483,648 to 0 to 4,294,967,295
2,147,483,647
long 4 bytes -2,147,483,648 to
2,147,483,647 0 to 4,294,967,295
float 4 bytes 3.4E-38 to 3.4E+38 N/A
double 8 bytes 1.7E-308 to N/A
1.7E+308
long double 10 byte 3.4E-4932 to N/A
1.1E+4932

Followings are some of the examples


char grade = 'A';
int age = 25;
float temperature = 36.6;
double pi = 3.14159265359;
signed int num = -10; // Stores both positive and negative numbers.
unsigned int num = 300; // Stores only positive values.
short int num = 1000;
long int bigNumber = 1000000;

19
The void Type
The void type specifies that no value is available. It is used in three kinds of situations:
Function returns as void
There are various functions in C which do not return any value or you can say they return void.
A function with no return value has the return type as void. For example, void exit (int status).
Function arguments as void
There are various functions in C which do not accept any parameter. A function with no
parameter can accept a void. For example, int rand(void).
Pointers to void
A pointer of type void * represents the address of an object, but not its type. For example, a
memory allocation function void *malloc(size_t size); returns a pointer to void which can be
casted to any data type.

20
5. Variables. Constants and Literals
5.1 Variables
A variable is nothing but a name given to a storage area that our programs can manipulate.
Each variable in C has a specific type, which determines the size and layout of the variable's
memory; the range of values that can be stored within that memory; and the set of operations
that can be applied to the variable. The name of a variable can be composed of letters, digits,
and the underscore character. It must begin with either a letter or an underscore. Upper and
lowercase letters are distinct because C is case-sensitive. Based on the basic types explained in
the previous chapter, there will be the following basic variable types:
Type Description
char Typically a single octet (one byte). This is an integer type.
int The most natural size of integer for the machine.
float A single-precision floating point value
double A double-precision floating point value.
void Represents the absence of type.

Defining Variables
A variable definition tells the compiler where and how much storage to create for the variable.
A variable definition specifies a data type and contains a list of one or more variables of that
type as follows:
type variable_list;
Here, type must be a valid C data type including char, w_char, int, float, double, bool, or any
user-defined object; and variable_list may consist of one or more identifier names separated by
commas. Some valid declarations are shown here:
int i, j, k;
char c, ch;
float f, salary;
double d;
The line int i, j, k; declares and defines the variables i, j and k; which instruct the compiler to
create variables named i, j, and k of type int.
Variables can be initialized (assigned an initial value) in their declaration. The initializer
consists of an equal sign followed by a constant expression as follows:
type variable_name = value;
Some examples are:
extern int d = 3, f = 5; // declaration of d and f.
int d = 3, f = 5; // definition and initializing d and f.

21
byte z = 22; // definition and initializes z.
char x = 'x'; // the variable x has the value 'x'.
For definition without an initializer: variables with static storage duration are implicitly
initialized with NULL (all bytes have the value 0); the initial value of all other variables are
undefined.
Variable Declaration in C
A variable declaration provides assurance to the compiler that there exists a variable with the
given type and name so that the compiler can proceed for further compilation without requiring
the complete detail about the variable. A variable declaration has its meaning at the time of
compilation only, the compiler needs actual variable declaration at the time of linking the
program. A variable declaration is useful when you are using multiple files and you define your
variable in one of the files which will be available at the time of linking the program. You will
use the keyword extern to declare a variable at any place. Though you can declare a variable
multiple times in your C program, it can be defined only once in a file, a function, or a block
of code.
Example
Try the following example, where variables have been declared at the top, but they have been
defined and initialized inside the main function:
#include <stdio.h>
// Variable declaration:
extern int a, b;
extern int c;
extern float f;

int main ()
{
/* variable definition: */
int a, b;
int c;
float f;

/* actual initialization */
a = 10;
b = 20;

22
c = a + b;

printf("value of c : %d \n", c);


f = 70.0/3.0;
printf("value of f : %f \n", f);
return 0;
}
When the above code is compiled and executed, it produces the following result:
value of c : 30
value of f : 23.333334
The same concept applies on function declaration where you provide a function name at the
time of its declaration and its actual definition can be given anywhere else. For example:
// function declaration
int func();
int main()
{
// function call
int i = func();
}
// function definition
int func()
{
return 0;
}

5.2 Constants and Literals


Constants refer to fixed values that the program may not alter during its execution. These fixed
values are also called literals. Constants can be of any of the basic data types like an integer
constant, a floating constant, a character constant, or a string literal. There are enumeration
constants as well. Constants are treated just like regular variables except that their values cannot
be modified after their definition.
Integer Literals
An integer literal can be a decimal, octal, or hexadecimal constant. A prefix specifies the base
or radix: 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal. An integer literal can

23
also have a suffix that is a combination of U and L, for unsigned and long, respectively. The
suffix can be uppercase or lowercase and can be in any order.
Here are some examples of integer literals
212 /* Legal */
215u /* Legal */
0xFeeL /* Legal */
078 /* Illegal: 8 is not an octal digit */
032UU /* Illegal: cannot repeat a suffix */
Following are other examples of various types of integer literals:
85 /* decimal */
0213 /* octal */
0x4b /* hexadecimal */
30 /* int */
30u /* unsigned int */
30l /* long */
30ul /* unsigned long */
Floating-point Literals
A floating-point literal has an integer part, a decimal point, a fractional part, and an exponent
part. You can represent floating point literals either in decimal form or exponential form. While
representing decimal form, you must include the decimal point, the exponent, or both; and
while representing exponential form, you must include the integer part, the fractional part, or
both. The signed exponent is introduced by e or E.
Here are some examples of floating-point literals:
3.14159 /* Legal */
314159E-5L /* Legal */
510E /* Illegal: incomplete exponent */
210f /* Illegal: no decimal or exponent */
.e55 /* Illegal: missing integer or fraction */
Character Constants
Character literals are enclosed in single quotes, e.g., 'x' can be stored in a simple variable of
char type. A character literal can be a plain character (e.g., 'x'), an escape sequence (e.g., '\t'),
or a universal character (e.g., '\u02C0'). There are certain characters in C that represent special
meaning when preceded by a backslash, for example, newline (\n) or tab (\t). Here, you have a
list of such escape sequence codes:

24
Escape sequence Meaning
\\ \ character
\' ' character
\" " characte
\? ? character
\a Alert or bell
\b Backspace
\f Form feed
\n Newline
\r Carriage return
\t Horizontal tab
\v Vertical tab
\ooo Octal number of one to three digits
\xhh . . . Hexadecimal number of one or more digits

Following is the example to show a few escape sequence characters:


#include <stdio.h>
int main()
{
printf("Hello\tWorld\n\n");
return 0;
}
When the above code is compiled and executed, it produces the following result:
Hello World
String Literals
String literals or constants are enclosed in double quotes "". A string contains characters that
are similar to character literals: plain characters, escape sequences, and universal characters.
You can break a long line into multiple lines using string literals and separating them using
whitespaces. Here are some examples of string literals. All the three forms are identical strings.
"hello, dear"
"hello, dear"
"hello, " "d" "ear"

25
Defining Constants
There are two simple ways in C to define constants:

 Using #define pre-processor


 Using const keyword
The #define Preprocessor
Given below is the form to use #define preprocessor to define a constant:
#define identifier value
The following example explains it in detail:
#include <stdio.h>
#define LENGTH 10
#define WIDTH 5
#define NEWLINE '\n'
int main()
{
int area;
area = LENGTH * WIDTH;
printf("value of area : %d", area);
printf("%c", NEWLINE);
return 0;
}
When the above code is compiled and executed, it produces the following result:
value of area : 50
The const Keyword
You can use const prefix to declare constants with a specific type as follows:
const type variable = value;
The following example explains it in detail:
#include <stdio.h>
int main()
{
const int LENGTH = 10;
const int WIDTH = 5;

26
const char NEWLINE = '\n';
int area;
area = LENGTH * WIDTH;
printf("value of area : %d", area);
printf("%c", NEWLINE);
return 0;
}
When the above code is compiled and executed, it produces the following result:
value of area : 50
Note that it is a good programming practice to define constants in CAPITALS

27
6. Input and Output
In C programming, input refers to data a program receives, and output is the data a program
displays or generates. The standard library provides functions for both, with printf for
outputting formatted text and scanf for receiving formatted input. The following diagram
depicts the same.

6.1 Input
Purpose :
Input allows a program to interact with the user or external sources by receiving data.
Examples:
Reading data entered by the user through the keyboard.
Reading data from a file
Key Function:
scanf() is a primary function for reading formatted input from the standard input stream
(typically the keyboard).

6.2 Output
Purpose:
Output allows a program to communicate results, messages, or errors to the user or external
destinations.
Examples:
Displaying text on the console (screen).
Writing data to a file.
Key Function:
printf() is a primary function for displaying formatted output to the standard output stream
(typically the screen).

28
Programming Example :

#include <stdio.h>
int main() {
int age;
char name[50];
// Input (using scanf)
printf("Enter your age: ");
scanf("%d", &age);
printf("Enter your name: ");
scanf("%s", name); // Note: scanf("%s", name) reads only up to the first space
// Output (using printf)
printf("Your name is: %s\n", name);
printf("Your age is: %d\n", age);
return 0;
}

Explanation:
printf("Enter your age: ");: Prints a prompt message to the screen, asking the user to enter their
age.
scanf("%d", &age);: Reads an integer value from the standard input (keyboard) and stores it in
the age variable. The %d is a format specifier indicating an integer. The & (ampersand)
provides the memory address of the age variable, where the input value should be stored
printf("Enter your name: ");: Prints a prompt message to the console, asking the user to enter
their name

6.3 Format Specifier


In C, format specifiers are used in printf() and scanf() to determine how data is read or
displayed. They're placeholders within a string that tell the compiler the data type (e.g., integer,
floating-point, string). For example, %d represents an integer, %f represents a floating-point
number, and %s represents a string.

29
Explanation:
Purpose: Format specifies ensure that data is displayed or read correctly based on its
type. Without them, the compiler wouldn't know how to interpret the variable values.
Syntax: A format specifies starts with a % followed by a character indicating the data type.
Common Specifies:
%d or %i: Integer.
%f: Floating-point number.
%s: String.
%c: Character.

Example :
# include <stdio.h>
int main() {
int age = 30;
float price = 19.99;
char name[] = "Alice";
printf("My age is %d.\n", age); // Outputs: My age is 30.
printf("The price is %.2f.\n", price); // Outputs: The price is 19.99.
printf("My name is %s.\n", name); // Outputs: My name is Alice.
return 0;
}

30
7. Operators in C
In C programming, operators are symbols that instruct the compiler to perform specific
operations on variables and data. They are used to manipulate data, perform calculations, make
decisions, and control the flow of a program. C provides a wide variety of operators, including
arithmetic, relational, logical, bitwise, and more.

7.1 Arithmetic Operators


The following table shows all the arithmetic operators supported by the C language.
Operator Description
+ Addition or unary plus
- Subtraction or unary minus
* Multiplication
/ Division
% Remainder after division (modulo
division)

Example
// Working of arithmetic operators
#include <stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}

31
Output :
a+b=13
a-b=5
a*b=36
a/b=2
Remainder when a divided by b =1

7.2 Increment and Decrement Operators


C programming has two operators increment ++ and decrement -- to change the value of an
operand by [Link] ++ increases the value by 1 whereas decrement -- decreases the value
by 1. These two operators are unary operators, meaning they only operate on a single operand.
Example :
// Working of increment and decrement operators
#include <stdio.h>
int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;
printf("++a = %d \n", ++a);
printf("--b = %d \n", --b);
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);
return 0;
}
Output :
++a=11
- -b=99
++c=11.500000
--d=99.500000

32
7.3 Assignment Operator (=)
An assignment operator is used for assigning a value to a variable. The most common
assignment operator is =
Operator Example Same as
= a=b a=b
+= a += b a = a+b
-= a -= b a -= b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b

Example :
// Working of assignment operators
#include <stdio.h>
int main()
{
int a = 5, c;
c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c);
c -= a; // c is 5
printf("c = %d\n", c);
c *= a; // c is 25
printf("c = %d\n", c);
c /= a; // c is 5
printf("c = %d\n", c);
c %= a; // c = 0
printf("c = %d\n", c);
return 0;
}
Output :
c=5
c=10
c=5

33
c=25
c=5
c=0

7.4 Relational Operators


A relational operator checks the relationship between two operands. If the relation is true, it
returns 1; if the relation is false, it returns value 0. Relational operators are used in decision
making and loops.
Operator Meaning of Operator Example
== Equal to 5 == 3 is evaluated to 0
> Greater than 5 > 3 is evaluated to 1

< Less than 5 < 3 is evaluated to 0


!= Not equal to 5 != 3 is evaluated to 1
>= Greater than or equal to 5 >= 3 is evaluated to 1
<= Less than or equal to 5 <= 3 is evaluated to 0

Example :
// Working of relational operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;
printf("%d == %d is %d \n", a, b, a == b);
printf("%d == %d is %d \n", a, c, a == c);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d < %d is %d \n", a, c, a < c);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d != %d is %d \n", a, c, a != c);
return 0;
}
Output :
5==5 is 1

34
5==10 is 0
5>5 is 0
5>10 is 0
5<5 is 0
5<10 is 1
5!=5 is 0
5!=10 is 1

7.5 Logical Operators


An expression containing logical operator returns either 0 or 1 depending upon whether
expression results true or false. Logical operators are commonly used in decision making in C
programming.
Operator Meaning Example
&& Logical AND. True only if If c = 5 and d = 2 then,
all operands are true expression ((c==5) &&
(d>5)) equals to 0.
|| Logical OR. True only if If c = 5 and d = 2 then,
either one operand is true expression ((c==5) ||
(d>5)) equals to 1.
! Logical NOT. True only if If c = 5 then, expression
the operand is 0 !(c==5) equals to 0.

Example
// Working of logical operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;
result = (a == b) && (c > b);
printf("(a == b) && (c > b) is %d \n", result);
result = (a == b) && (c < b);
printf("(a == b) && (c < b) is %d \n", result);
result = (a == b) || (c < b);
printf("(a == b) || (c < b) is %d \n", result);

35
result = (a != b) || (c < b);
printf("(a != b) || (c < b) is %d \n", result);
result = !(a != b);
printf("!(a != b) is %d \n", result);
result = !(a == b);
printf("!(a == b) is %d \n", result);
return 0;
}
Output :
(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
(a != b) is 1
!(a == b) is 0

7.6 Bitwise operators


During computation, mathematical operations like: addition, subtraction, multiplication,
division, etc are converted to bit-level which makes processing faster and saves power. Bitwise
operators are used in C programming to perform bit-level operations. The following table gives
bitwise operators are available in C language.
Operator Meaning of Operator
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
<< Left shift
>> Right shift

Bitwise operators allow precise manipulation of bits, giving you control over hardware
operations. Let’s look at the truth table of the bitwise operators
X Y X&Y X|Y X^Y
0 0 0 0 0
0 1 0 1 0
1 0 0 1 1
1 1 1 1 0

36
Example
// C Program to demonstrate use of bitwise operators
#include <stdio.h>
int main()
{
// a = 5 (00000101 in 8-bit binary), b = 9 (00001001 in
// 8-bit binary)
unsigned int a = 5, b = 9;
// The result is 00000001
printf("a = %u, b = %u\n", a, b);
printf("a&b = %u\n", a & b);
// The result is 00001101
printf("a|b = %u\n", a | b);
// The result is 00001100
printf("a^b = %u\n", a ^ b);
// The result is 11111111111111111111111111111010
// (assuming 32-bit unsigned int)
printf("~a = %u\n", a = ~a);
// The result is 00010010
printf("b<<1 = %u\n", b << 1);
// The result is 00000100
printf("b>>1 = %u\n", b >> 1);
return 0;
}
Output :
a=5 ,b=9
a&b=1
a|b=13
a^b=12

37
~a=4294967290
b<<1=18
b>>1=4
Interesting Facts About Bitwise Operators
The left-shift and right-shift operators should not be used for negative numbers.
The bitwise OR of two numbers is simply the sum of those two numbers if there is no carry
involved; otherwise, you add their bitwise AND.
The Bitwise operators should not be used in place of logical operators.
The left-shift and right-shift operators are equivalent to multiplication and division by 2
respectively.

7.7 Comma Operator


Comma operators are used to link related expressions together. For example int a,b=20, c;

7.8 Sizeof Operator


The sizeof is a unary operator that returns the size of data (constants, variables, array,
structure, etc).
Example :
#include <stdio.h>
int main()
{
int a;
float b;
double c;
char d;
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));
return 0;
}
Output :
Size of int = 2 bytes

38
Size of float = 4 bytes
Size of double=8 bytes
Size of char= 1 byte

7.9 Operators Precedence in C


Operator precedence determines the grouping of terms in an expression and decides how an
expression is evaluated. Certain operators have higher precedence than others; for example, the
multiplication operator has a higher precedence than the addition operator. For example, x = 7
+ 3 * 2; here, x is assigned 13, not 20 because operator * has a higher precedence than +, so it
first gets multiplied with 3*2 and then adds into 7.
Here, operators with the highest precedence appear at the top of the table, those with the lowest
appear at the bottom. Within an expression, higher precedence operators will be evaluated first.
Category Operator Associativity
Postfix () [] -> . ++ - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof Right to left
Multiplicative */% Left to right
Additive +- Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to righ
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= Right to left
|=
Comma , Left to right

Example
The following example to understand operator precedence in C:
#include <stdio.h>
main()
{
int a = 20;
int b = 10;
int c = 15;

39
int d = 5;
int e;
e = (a + b) * c / d; // ( 30 * 15 ) / 5
printf("Value of (a + b) * c / d is : %d\n", e );
e = ((a + b) * c) / d; // (30 * 15 ) / 5
printf("Value of ((a + b) * c) / d is : %d\n" , e );
e = (a + b) * (c / d); // (30) * (15/5)
printf("Value of (a + b) * (c / d) is : %d\n", e );
e = a + (b * c) / d; // 20 + (150/5)
printf("Value of a + (b * c) / d is : %d\n" , e );
return 0;
}

Output :
Value of (a + b) * c / d is : 90
Value of ((a + b) * c) / d is : 90
Value of (a + b) * (c / d) is : 90
Value of a + (b * c) / d is : 50

40
8. Decision Making
Decision-making structures require that the programmer specifies one or more conditions to be
evaluated or tested by the program, along with a statement or statements to be executed if the
condition is determined to be true, and optionally, other statements to be executed if the
condition is determined to be false. C programming language assumes any non-zero and non-
null values as true, and if it is either zero or null, then it is assumed as false value.
C programming language provides the following types of decision-making statements.

Statement Description
if statement An if statement consists of a boolean
expression followed by one or more
statements.
if...else statement An if statement can be followed by an optional
else statement, which executes when the
Boolean expression is false.
nested if statement You can use one if or else if statement inside
another if or else if statement(s).
switch statement A switch statement allows a variable to be
tested for equality against a list of values.
nested switch statements You can use one switch statement inside
another switch statement(s).

8.1 if Statement
An if statement consists of a Boolean expression followed by one or more statements.
Syntax
The syntax of an ‘if’ statement in C programming language is

If the Boolean expression evaluates to true, then the block of code inside the ‘if’ statement will
be executed. If the Boolean expression evaluates to false, then the first set of code after the end
of the ‘if’ statement (after the closing curly brace) will be executed.C programming language

41
assumes any non-zero and non-null values as true and if it is either zero or null, then it is
assumed as false value.

Example :
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 10;
/* check the boolean condition using if statement */
if( a < 20 )
{
/* if condition is true then print the following */
printf("a is less than 20\n" );
}
printf("value of a is : %d\n", a);
return 0;
}
Output :
a is less than 20;
value of a is : 10

8.2 if…else Statement


An if statement can be followed by an optional else statement, which executes when the
Boolean expression is false.
The syntax of an if...else statement in C programming language is

42
{
/* statement(s) will execute if the boolean expression is false */
}
If the Boolean expression evaluates to true, then the if block will be executed, otherwise, the
else block will be executed.
Example
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 100;
/* check the boolean condition */
if( a < 20 )
{
/* if condition is true then print the following */
printf("a is less than 20\n" );
}
else
{
/* if condition is false then print the following */
printf("a is not less than 20\n" );
}
printf("value of a is : %d\n", a);
return 0;

43
}
Output :
a is not less than 20;
value of a is : 100

8.3 Nested if Statements


It is always legal in C programming to nest if-else statements, which means you can use one if
or else if statement inside another if or else if statement(s).
Syntax

Example
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 100;
int b = 200;
/* check the boolean condition */
if( a == 100 )
{

44
/* if condition is true then check the following */
if( b == 200 )
{
/* if condition is true then print the following */
printf("Value of a is 100 and b is 200\n" );
}
}
printf("Exact value of a is : %d\n", a );
printf("Exact value of b is : %d\n", b );
return 0;
}
Output
Value of a is 100 and b is 200
Exact value of a is : 100
Exact value of b is : 200

8.4 Switch Statement


In C, switch statement is a control flow structure that allows you to execute one of many
code blocks based on the value of an expression. It is often used in place of if-else statement
when there are multiple conditional codes.
Syntax
switch (expression) {
case constant-expression1:
// Statements to execute if expression == constant-expression1
break;
case constant-expression2:
// Statements to execute if expression == constant-expression2
break;
// ... more cases
default:
// Statements to execute if no case matches
}

45
The expression is evaluated, and its value is compared against the constant-expression values
of each case. If a match is found, the code block associated with that case is
executed. The break statement is typically used to exit the switch statement after a
matching case is processed. If no break is present, execution continues to the next case, which
is known as "fall-through." The default case is optional and is executed if no
other case matches the expression's value.
Example :

// Output:

46
Wednesday
In this example, the switch statement checks the value of the day variable and prints the
corresponding day of the week. Since day is 3, the output is "Wednesday".

8.5 Conditional operator (:?)

The conditional operator in C, also known as the ternary operator, provides a concise way to
express conditional logic. It serves as a shorthand for the if-else statement, allowing for more
compact code. The syntax of the conditional operator is as follows:
variable = (condition) ? expression1 : expression2;
The condition is evaluated first. It can be any expression that results in a boolean value (true
or false). If the condition is true, expression1 is evaluated and its result is returned. If
the condition is false, expression2 is evaluated and its result is returned.

Example :
int time = 20;
if (time < 18)
{
printf("Good day.");
}
else
{
printf("Good evening.");
}
The above program can be written using conditional operator as
int time = 20;
(time < 18) ? printf("Good day.") : printf("Good evening.");

47
9. Loops
Loops in programming are used to execute a block of code repeatedly until a specific condition
is met. This allows for efficient repetition of tasks, reducing code redundancy and improving
program organization. By using loops, programmers can automate repetitive actions without
having to write the same code multiple times

9.1 Purpose of Loops


Consider the following series
Sum = 1+2+3+4+5+6+7+8+9+10+………………+n
We need to add all numbers up to range N (any integer value), here addition task is repeatedly
applied to sum all numbers from 1 to N. Instead of taking each variable for every number , we
can use loops to write generalized program that perform addition task repetitively.

9.2 Benefits of Using Loops


Code Reusability: Once a loop is written, it can be used to repeat the same code in different
parts of a program or even in other programs.
Reduced Errors: Using loops instead of repeating code manually reduces the risk of typos and
other errors.
Improved Readability: Loops make code more concise and easier to understand
In essence, loops are a fundamental programming concept that enables efficient and organized
repetition of tasks, making programming more powerful and versatile.

9.3 Types of Loops


C programming has three types of loops.
while loop
do...while loop
for loop
while Loop
The while loop in C programming is a control flow statement that repeatedly executes a block
of code as long as a specified condition remains true. It's categorized as an entry-controlled
loop because the condition is checked before entering the loop body.

48
Syntax :

 The condition inside the parentheses is evaluated.


 If the condition is true, the code block within the curly braces is executed.
 After executing the code block, the condition is evaluated again.
 Steps 2 and 3 are repeated as long as the condition remains true.
 When the condition becomes false, the loop terminates, and the program continues with the
code following the loop..

Example
Sum = 1+2+3+4+5+6+7+8+9+10+………………+n

#include <stdio.h>
int main() {
int i = 1,sum=0,n=5;
while (i <= n) {
sum=sum+i;
i++;
}
printf(“Sum of first n numbers is =%d”, sum);
return 0;
}

Output :
Sum of first n numbers is =15

do-while loop

49
The do while loop in C is a post-test loop, meaning that the loop's body is executed at least
once before the condition is checked. The syntax of the do while loop is as follows

Syntax

do
{
//true
Code to be executed when condition is true
//repeats
} while (condition); // true or false

// Code to be executed when condition is false

 The code inside the do block is executed.


 The condition is evaluated.
 If the condition is true, the loop continues from step 1.
 If the condition is false, the loop terminates.

Example

Sum = 1+2+3+4+5+6+7+8+9+10+………………+n

#include <stdio.h>
int main() {
int i = 1,sum=0,n=5;
do {
sum=sum+i;
i++;
} while (i <= n)
printf(“Sum of first n numbers is =%d”, sum);
return 0;
}
Output :
Sum of first n numbers is =15

for loop

In C, the for loop is a control flow statement that allows code to be executed repeatedly for a
predetermined number of times. It is particularly useful when the number of iterations is known

50
in advance. The for loop consists of three parts: initialization, condition, and
increment/decrement, all defined within the loop's parentheses.

Syntax

 Initialization:
This statement is executed only once at the beginning of the loop. It is typically used to
declare and initialize a loop counter variable.
 Condition:
This expression is evaluated before each iteration. If the condition is true, the loop body is
executed. If it is false, the loop terminates.
 Increment/Decrement:
This statement is executed after each iteration. It is commonly used to update the loop counter
variable.

Example

Sum = 1+2+3+4+5+6+7+8+9+10+………………+n

#include <stdio.h>
int main() {

51
int i,n=5,sum=0;
for (i = 1; i <= n; i++)
{
sum=sum+i;
}
printf(“Sum of first n numbers is =%d”, sum);
return 0;
}

Output :
Sum of first n numbers is =15

52
10. Arrays
Arrays a kind of data structure that can store a fixed-size sequential collection of elements of
the same type. An array is used to store a collection of data, but it is often more useful to think
of an array as a collection of variables of the same type. Instead of declaring individual
variables, such as number0, number1, ..., and number99, you declare one array variable such
as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual
variables. A specific element in an array is accessed by an index.

10.1 Characteristics of an array


Homogeneous Elements:
All elements within an array must be of the same data type (e.g., all integers, all characters, or
all floats).
Contiguous Memory:
Array elements are stored in adjacent memory locations. This allows for efficient access to any
element using its index.
Fixed Size:
The size of an array is determined at the time of declaration and cannot be changed during
runtime.
Indexed Access
Elements are accessed using an index, which starts from 0 for the first element.
Array elements are stored in contiguous memory locations. Each element is identified by an
index starting with "0".The lowest address corresponds to the first element and the highest
address to the last element.

10.2 Applications of Arrays


Arrays are commonly used for:
Storing lists of data (e.g., student scores, product prices).
Implementing tables or matrices.
Working with strings (character arrays).
Performing operations on collections of data.

53
10.3 Declaring Arrays
To declare an array in C, a programmer specifies the type of the elements and the number of
elements required by an array as follows:
type arrayName [ arraySize ];
This is called a single-dimensional array. The arraySize must be an integer constant greater
than zero and type can be any valid C data type. For example, to declare a 10-element array
called balance of type double, use this statement:
double balance[10];
Here, balance is a variable array which is sufficient to hold up to 10 double numbers.

10.4 Initializing Arrays


You can initialize an array in C either one by one or using a single statement as follows:
double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};
The number of values between braces { } cannot be larger than the number of elements that we
declare for the array between square brackets [ ]. If you omit the size of the array, an array just
big enough to hold the initialization is created. Therefore, if you write:
double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0};
You will create exactly the same array as you did in the previous example.

10.5 Reading and Printing Array Elements with for Loop


#include <stdio.h>
int main() {
int arr[5]; // Array of size 5
int size = sizeof(arr) / sizeof(arr[0]);

printf("Enter 5 integers:\n");
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]); // Read input from the user and store it
}

printf("You entered:\n");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]); // Print the entered elements

54
}
printf("\n");

return 0;
}

10.6 Accessing Array Elements


An element is accessed by indexing the array name. This is done by placing the index of the
element within square brackets after the name of the array. For example:
double salary = balance[9];
The above statement will take the 10th element from the array and assign the value to salary
variable. The following example shows how to use all the three above-mentioned concepts viz.
declaration, assignment, and accessing arrays:
#include <stdio.h>
int main ()
{
int n[ 10 ]; /* n is an array of 10 integers */
int i,j;
/* initialize elements of array n to 0 */
for ( i = 0; i < 10; i++ )
{
n[ i ] = i + 100; /* set element at location i to i + 100 */

}
/* output each array element's value */
for (j = 0; j < 10; j++ )
{
printf("Element[%d] = %d\n", j, n[j] );
}
return 0;
}

55
Output :
Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104
Element[5] = 105
Element[6] = 106
Element[7] = 107
Element[8] = 108
Element[9] = 109

10.7 Linear Search


#include <stdio.h>
int main() {
int arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 23;
int found = 0;
for (int i = 0; i < n; i++) {
if (arr[i] == key) {
printf("Element %d found at index: %d\n", key, i);
found = 1;
break;
}
}
if (!found) {
printf("Element %d not found in the array.\n", key);
}
return 0;

56
}

In this code, the program iterates through each element of the array arr. If the current
element arr[i] matches the key, the program prints the index i and sets the found flag to 1, then
exits the loop. If the loop completes without finding the key, it prints that the element is not
found.

10.8 Binary Search


#include <stdio.h>
int main() {
int arr[100], n, i, key;
int low, high, mid;
// Input the number of elements
printf("Enter number of elements: ");
scanf("%d", &n);
// Input the sorted array
printf("Enter %d sorted elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Input the key to search
printf("Enter the element to search: ");
scanf("%d", &key);
// Binary search logic
low = 0;
high = n - 1;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] == key) {
printf("Element found at index %d\n", mid);
break;

57
} else if (arr[mid] < key) {
low = mid + 1;
} else {
high = mid - 1;
}
}
if (low > high) {
printf("Element not found.\n");
}
return 0;
}

10.9 Bubble sort


#include<stdio.h>
int main()
{
int a[10],i,j,temp,n;
printf("\n Enter the max [Link] Elements to Sort: \n");
scanf("%d",&n);
printf("\n Enter the Elements : \n");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
for(i=0; i<n; i++)
for(j=i+1; j<n; j++)
{
if(a[i]>a[j])
{
temp=a[i];

58
a[i]=a[j];
a[j]=temp;
}
}
for(i=0; i<n; i++)
{
printf("%d\t",a[i]);
}
return 0;
}

Output :

Enter the max [Link] Elements to Sort:


5
Enter the Elements :
45
21
89
78
99
Sorted array is
21 45 78 89 99

10.10 Two-dimensional Array


In C, a 2D array (two-dimensional array) is essentially an array of arrays. It can be visualized
as a table or a matrix with rows and columns. Each element is accessed using two indices: one
for the row and one for the column.
Syntax

data_type array_name[rows][columns];

Example

59
int matrix[3][4]; // A 3x4 matrix (3 rows and 4 columns)

Initializing a 2D Array
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};

Accessing Elements
matrix[0][1]; // Accesses the element in the 1st row, 2nd column (value = 2)

Reading and Printing 2D array


#include <stdio.h>
int main() {
int rows, cols;
// Ask the user for dimensions
printf("Enter the number of rows and columns: ");
scanf("%d %d", &rows, &cols);
int array[100][100]; // Maximum size limit (you can adjust)
// Reading values into the array
printf("Enter the elements of the array:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("Element [%d][%d]: ", i, j);
scanf("%d", &array[i][j]);
}
}

// Printing the array


printf("\nThe 2D array is:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", array[i][j]);

60
}
printf("\n");
}

return 0;
}

10.11 Addition of Two Matrices


#include <stdio.h>
int main() {
int rows, cols, i, j;
// Input the size of the matrices
printf("Enter the number of rows and columns: ");
scanf("%d %d", &rows, &cols);
// Declare matrices
int matrix1[100][100], matrix2[100][100], sum[100][100];
// Input elements of the first matrix
printf("\nEnter elements of first matrix:\n");
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
printf("Element [%d][%d]: ", i+1, j+1);
scanf("%d", &matrix1[i][j]);
}
}

// Input elements of the second matrix


printf("\nEnter elements of second matrix:\n");
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
printf("Element [%d][%d]: ", i+1, j+1);

61
scanf("%d", &matrix2[i][j]);
}
}

// Add the matrices


for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}

// Display the result


printf("\nSum of the two matrices:\n");
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
printf("%d\t", sum[i][j]);
}
printf("\n");
}

return 0;
}

10.12 Multiplication of two matrices


#include <stdio.h>
#define ROW1 2 // Number of rows in first matrix
#define COL1 3 // Number of columns in first matrix
#define ROW2 3 // Number of rows in second matrix
#define COL2 2 // Number of columns in second matrix
int main() {

62
int matrix1[ROW1][COL1] = {
{1, 2, 3},
{4, 5, 6}
};
int matrix2[ROW2][COL2] = {
{7, 8},
{9, 10},
{11, 12}
};
int result[ROW1][COL2];

// Initialize the result matrix to 0


for (int i = 0; i < ROW1; i++) {
for (int j = 0; j < COL2; j++) {
result[i][j] = 0;
}
}
// Matrix multiplication
for (int i = 0; i < ROW1; i++) {
for (int j = 0; j < COL2; j++) {
for (int k = 0; k < COL1; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
// Print the result
printf("Resultant Matrix:\n");
for (int i = 0; i < ROW1; i++) {
for (int j = 0; j < COL2; j++) {
printf("%d\t", result[i][j]);

63
}
printf("\n");
}

return 0;
}

11. Pointers

64
In C programming, a pointer is a variable that stores the memory address of another variable. It
allows you to directly access and manipulate the data stored at that memory location. Pointers
are used to access the memory of a variable and manipulate their addresses within a program.

11.1 Memory Organization


Memory Address:

Every variable in a program occupies a specific memory location, identified by a unique


address.

Storing Addresses:
Pointers are designed to store the addresses of memory locations in hexadecimal integer.
Example : 0x00000004
Accessing Data:
Once a pointer holds the address of a variable, it can be used to access and modify the data
stored at that address.

65
Dereferencing:
Using the dereference operator (e.g., * in C/C++) to access the data at the memory address
stored in a pointer.
Address-of Operator:

Using the address-of operator (e.g., & in C/C++) to obtain the memory address of a
variable.

11.2 Benefits of Using Pointers:


Memory Management:
Pointers are crucial for dynamically allocating memory (e.g., using new and delete in C++).
Passing by Reference:
Pointers enable functions to modify the original variables passed to them, rather than just
working with copies.
Efficiency:
Using pointers can be more efficient than passing large data structures by value, as only the
address needs to be copied.
Data Structures:

Pointers are fundamental for creating dynamic data structures like linked lists, trees, and
graphs.

11.3 Declaring a Pointer


A pointer is declared by specifying its name and type, just like simple variable declaration
but with an asterisk (*) symbol added before the pointer's name.

data_type* name

Here, data_type defines the type of data that the pointer is pointing to. An integer type
pointer can only point to an integer. Similarly, a pointer of float type can point to a floating-
point data, and so on.

Example:

int *ptr; // ptr holds some address and *ptr means value at address ptr

In the above statement, pointer ptr can store the address of an integer. It is pronounced as
pointer to integer.

66
11.4 Initialize the Pointer
Pointer initialization means assigning some address to the pointer variable. In C, the (&)
addressof operator is used to get the memory address of any variable. This memory
address is then stored in a pointer variable.
Example:
int var = 10;
// Initializing ptr
int *ptr = &var;
In the above statement, pointer ptr store the address of variable x which was determined
using address-of operator (&).

Dereference a Pointer

#include <stdio.h>
int main() {
int var = 10;
// Store address of var variable
int* ptr = &var;
// Directly accessing ptr
printf("%d", ptr);
return 0;
}
Output :
0x00000004

This hexadecimal integer (starting with 0x) is the memory [Link] have to
first dereference the pointer to access the value present at the memory address. This is done
with the help of dereferencing operator(*) (same operator used in declaration).

#include <stdio.h>
int main() {
int var = 10;

// Store address of var variable

67
int* ptr = &var;
// Dereferencing ptr to access the value
printf("%d", *ptr);
return 0;
}
Output : 10

11.5 Addition of two number using pointers


#include <stdio.h>
int main() {
int num1, num2, sum;
int *ptr1, *ptr2;
// Input numbers
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);

// Pointing to the variables


ptr1 = &num1;

68
ptr2 = &num2;
// Adding values using pointers
sum = *ptr1 +*ptr2;
printf("Sum = %d\n", sum);
return 0;
}

11.6 Types of Pointers


There are 4 special types of pointers that used or referred to in different contexts
NULL Pointer
The NULL Pointers are those pointers that do not point to any memory location. They can
be created by assigning NULL value to the pointer. A pointer of any type can be assigned
the NULL value.
#include <stdio.h>
int main() {
// Null pointer
int *ptr = NULL;
return 0;
}
NULL pointers are generally used to represent the absence of any address. This allows us to
check whether the pointer is pointing to any valid memory location by checking if it is equal
to NULL.

Void Pointer

The void pointers in C are the pointers of type void. It means that they do not have any
associated data type. They are also called generic pointers as they can point to any type and
can be typecasted to any type.

#include <stdio.h>

int main() {
// Void pointer

69
void *ptr;

return 0;
}

Wild Pointers

The wild pointers are pointers that have not been initialized with something yet. These types
of C-pointers can cause problems in our programs and can eventually cause them to crash. If
values are updated using wild pointers, they could cause data abort or data corruption.
#include <stdio.h>
int main() {
// Wild Pointer
int *ptr;
reurn 0;
}
Dangling Pointer

A pointer pointing to a memory location that has been deleted (or freed) is called a dangling
pointer. Such a situation can lead to unexpected behavior in the program and also serve as a
source of bugs in C programs.

#include <stdio.h>
#include <stdlib.h>
int main() {
int* ptr = (int*)malloc(sizeof(int));
// After below free call, ptr becomes a dangling pointer
free(ptr);
printf("Memory freed\n");
// removing Dangling Pointer
ptr = NULL;
return 0;
}

70
11.7 Using Pointers on Array
int arr[5] = {10, 20, 30, 40, 50}; // arr is initialized.
printf("%d\n", *arr); // Output: 10 (same as arr[0])
printf("%d\n", *(arr+1)); // Output: 20 (same as arr[1])

11.8 Accessing Array Elements Using Pointers


#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int *ptr = arr; // or &arr[0]
for (int i = 0; i < 5; i++) {
printf("Element %d: %d\n", i, *(ptr + i));
}
return 0;
}

11.9 Modifying Array Values with Pointers


*(ptr + 2) = 100; // Changes arr[2] to 100

12. Functions

71
A function C is a set of statements that, when called, perform some specific tasks. It is the
basic building block of a C program that provides modularity and code reusability. They are
also called subroutines or procedures in other languages.

12.1 Benefits of Using Functions


Modularity: Breaks code into smaller chunks
Reusability: Use the same code multiple times
Ease of Testing and Debugging
Improved Readability

12.2 Types of functions


Built-in (Library) Functions
Predefined in C header files(stdio.h, conio.h, string.h,math.h)
Examples: printf(), scanf(), strlen(), sqrt()
User-Defined Functions
Created by the programmer to perform specific tasks

12.3 Function Definition


A function definition informs the compiler about the function's name, its return type, and
what it does. It is compulsory to define a function before it can be called.

return_type name () {
// Body of function
};
where,
return_type: type of value the function return.
name: Name of the function
Body of function: Statements inside curly brackets { } are executed when function call.

Example:

72
void hello(){
printf("Hello AIET");
}
In the above code, hello is the name assigned to the function and its return type is void.

12.4 Function Call


After defining a function, you can use it anywhere in the program by simply calling it with its
name followed by parentheses ().
Example:
#include <stdio.h>
// Function definition
void hello() {
printf("Hello AIET");
}
int main() {
// Function call
hello();
return 0;
}
Output :
Hello AIET

12.5 Function return type


A function can return a value to its caller as a result. It is called the return value, and the
type of this value is called return type of the function. The function only returns one value,
The return keyword is used to return some values from the function.

73
Output : 3

In this program, the getThree() function is of integer type, meaning that when you call this
function, it returns an integer value, which is 3.

12.6 Passing Parameters to Function


A function can be provided some values by its caller. These values are called arguments and
are provided at the time of function call. In the function definition, we use the placeholder
variables inside the parentheses () to receive these values. These placeholders are
called parameters.
// Parameters in the function definition
return_type func(type1 name1, type2 name2, ...){
// ....
}
name1, name2, .... are the names given to the parameters of the function. They will be
referred with the same name inside the function.
Example

74
12.7 Factorial of number using function

#include <stdio.h>
int main() {
int N = 5;
int fact = factorial(N);
printf("Factorial of %d is %d", N, fact);
return 0;
}
int factorial(int N) {
int fact = 1, i;
// Loop from 1 to N to get the factorial
for (i = 1; i <= N; i++) {
fact *= i;
}
return fact;
}

75
12.8 Recursion
Recursion is the process of a function calling itself repeatedly till the given condition is
satisfied. A function that calls itself directly or indirectly is called a recursive function and such
kind of function calls are called recursive calls.
Key Concepts in Recursion
Base Case:
A crucial part of recursion is the base case, which is a condition that stops the function from
calling itself infinitely. Without a base case, a recursive function would result in a stack
overflow error.
Recursive Step:
This is the part where the function calls itself with a modified input, working towards the base
case.
Function Call Stack:
Each recursive call adds a new frame to the call stack, storing the function's local variables and
return address. When the base case is reached, the function returns, and the stack unwinds.

Advantages of Recursion
Can make code more concise and easier to read for certain problems.
Well-suited for problems that can be naturally expressed in terms of smaller, self-similar
subproblems (e.g., tree traversals, sorting algorithms).

Disadvantages of Recursion
Can be less efficient than iterative solutions due to the overhead of function calls and stack
management.
May lead to stack overflow errors if the recursion depth is too large.
Can sometimes be harder to debug and understand compared to iterative code.

Recursion Example
// Factorial of a number using recursion
#include <stdio.h>
long factorial(int n) {
if (n == 0) {
return 1;

76
} else {
return n * factorial(n - 1);
}
}
int main() {
int number;
printf("Enter a non-negative integer: ");
scanf("%d", &number);

if (number < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
printf("Factorial of %d = %ld\n", number, factorial(number));
}
return 0;
}

12.9 Passing arrays to Function

Passing an array to a function allows the function to directly access and modify the original
array. In this article, we will learn how to pass arrays to functions in C.
In C, arrays are always passed to function as pointers. They cannot be passed by value
because of the array decay due to which, whenever array is passed to a function, it decays
into a pointer to its first element. However, there can be different syntax of passing the arrays
as pointers.
The easiest way to pass array to a function is by defining the parameter as the undefined
sized array. Let’s take a look at an example:

#include <stdio.h>

int main() {

int arr[] = {1, 2, 3, 4, 5};

// Pass array to function

printArr(arr, 5);

77
return 0;

// Array passed as an array without the size of its dimension

void printArr(int arr[], int n) {

for (int i = 0; i < n; i++)

printf("%d ", arr[i]);

In this example, we pass an integer array arr to the function printArr(). In the function
prototype, we define the array as the array of integers with no size [Link], you
may also notice that we have passed the size of the array as second parameter to the function.
It is recommended to pass the size of the array to the function as another parameter,
otherwise we won’t know how many elements to process.
Array passed as an array with the size of its dimension

#include <stdio.h>

int main() {

int arr[] = {1, 2, 3, 4, 5};

// Pass array to function

printArr(arr, 5);

return 0;

// Array passed as an array with the size of its dimension

void printArr(int arr[5], int n) {

for (int i = 0; i < n; i++)

printf("%d ", arr[i]);

78
Passing Array as Pointer Notation

Instead of using array notation arr[] in the function parameter, we can directly use pointer
notation int *arr. All are equivalent, as arrays in C are treated as pointers to the first element
of the array. This method is more flexible when working with dynamically allocated arrays.

#include <stdio.h>

// Array passed as an array with the size of its

// dimension

void printArr(int* arr, int n) {

for (int i = 0; i < n; i++)

printf("%d ", arr[i]);

int main() {

int arr[] = {1, 2, 3, 4, 5};

// Pass array to function

printArr(arr, 5);

return 0;

12.10 Call by value and Call by Reference

In "call by value," a copy of the argument's value is passed to a function, so any changes made
to the argument inside the function do not affect the original variable. In "call by reference,"
the address (or reference) of the argument is passed, so any modifications made to the argument
inside the function directly affect the original variable.
Call by Value
How it works: A copy of the variable's value is created and passed to the function.
Impact on the original variable: Changes made to the variable within the function do not affect
the original variable outside the function.
Example: If you pass a number (like 5) to a function, the function will receive a copy of that
number, and any changes made to that copy will not alter the original number.

79
#include <stdio.h>

int add(int a, int b) {

int sum = a + b;

return sum;

int main() {

int num1 = 5;

int num2 = 10;

int result;

result = add(num1, num2);

printf("Sum: %d\n", result);

printf("num1: %d\n", num1);

printf("num2: %d\n", num2);

return 0;

}
Call by Reference
How it works:
The function receives a reference (or pointer) to the original variable's memory location, not a
copy of its value.
Impact on the original variable:
Modifications made to the variable within the function directly impact the original variable.
Example:
If you pass a variable (like x = 5) to a function by reference, and the function changes x to 10,
then the original variable x outside the function will also be 10
#include <stdio.h>
int add(int &a, int &b) {
int sum = *a + * b;
return sum;

80
}
int main() {
int *num1 = 5;
int *num2 = 10;
int result;
result = add(&num1, &num2);
printf("Sum: %d\n", result);
printf("num1: %d\n", *num1);
printf("num2: %d\n", n*um2);
return 0;
}

12.11 Passing Multidimensional Arrays


Multidimensional arrays, such as 2D arrays, can also be passed to functions in C. They will
also be treated as pointers but there are a few differences while passing them as they have a
few extra dimensions that the compiler has to know about
A 2D array is essentially an array of arrays, where each element of the main array holds another
array. In this article, we will see how to pass a 2D array to a function.
The simplest and most common method to pass 2D array to a function is by specifying the
parameter as 2D array with row size and column size. One thing to note is that we always have
to pass the size of the array's dimensions separately.

#include <stdio.h>
int main() {
int arr[3][3] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};

// Passing the array along with the size of rows and columns

print(arr, 3, 3);

return 0;

// Funtion that takes 2d array as parameter

81
void print(int arr[3][3], int n, int m) {

for (int i = 0; i < n; i++) {

for (int j = 0; j < m; j++)

printf("%d ", arr[i][j]);

printf("\n");

82
13. Strings
A String in C programming is a sequence of characters terminated with a null character '\0'.
The C String is work as an array of characters. The difference between a character array and
a C string is that the string in C is terminated with a unique character '\0'.

13.1 Declaration
Declaring a string in C is as simple as declaring a one-dimensional array of character type.
Below is the syntax for declaring a string.
char string_name[size];
In the above syntax string_name is any name given to the string variable and size is used to
define the length of the string, i.e. the number of characters strings will store.
Like array, we can skip the size in the above statement:
char array_name[];

13.2 Initialization
We can initialize a string either by specifying the list of characters or string literal.
// Using character list
char str[] = {'H', 'e', 'l', 'l', 'o', '\0'};
// Using string literal
char str[] = "Hello";

83
13.3 Accessing Characters
We can access any character of the string by providing the position of the character, like in
array. We pass the index inside square brackets [] with the name of the string.
#include <stdio.h>
int main() {
char str[] = "Hello";
// Access first character of string
printf("%c", str[0]);
return 0;
}
Output : H
#include <stdio.h>
int main() {
char str[] = " Hello ";
// Update the first
// character of string
str[0] = 'R';
printf("%c", str[0]);
return 0;
}
Output : R

13.4 Basic Operations on strings.


Length:
strlen(str) calculates the length of a string, excluding the null terminator.
#include <stdio.h>
#include <string.h>
int main() {
char s[] = "Gfg";
// Finding and printing length of string s
printf("%lu", strlen(s));

84
return 0;
}
Output : 3
Copy:
strcpy(dest, src) copies the string src to dest. strncpy(dest, src, n) copies at most n characters
of src to dest.
#include <stdio.h>
#include <string.h>
int main() {
char src[] = "Hello";
char dest[20];
// Copies "Hello" to dest
strcpy(dest, src);
printf("%s", dest);
return 0;
}
Output : Hello
Concatenation:
strcat(dest, src) appends the string src to the end of dest. strncat(dest, src, n) appends at
most n characters of src to dest.
#include <stdio.h>
#include <string.h>
int main() {
char s1[30] = "Hello, ";
char s2[] = "AIET!";
// Appends "AIET!" to "Hello, "
strcat(s1, s2);
printf("%s", s1);
return 0;
}
Output : Hello AIET1

85
Comparison:
strcmp(str1, str2) compares two strings lexicographically. It returns 0 if the strings are equal,
a negative value if str1 is less than str2, and a positive value if str1 is greater
than str2. strncmp(str1, str2, n) compares at most n characters of the strings.

#include <stdio.h>
#include <string.h>
int main() {
char s1[] = "Apple";
char s2[] = "Applet";
// Compare two strings and print result
int res = strcmp(s1, s2);
if (res == 0)
printf("s1 and s2 are same");
else if (res < 0)
printf("s1 is lexicographically smaller than s2");
else
printf("s1 is lexicographically "
"greater than s2");
return 0;
}
Output : s1 is lexicographically smaller than s2
Character Search:
strchr(str, ch) finds the first occurrence of character ch in str. strrchr(str, ch) finds the last
occurrence of character ch in str.
#include <stdio.h>
#include <string.h>
int main() {
char s[] = "Hello, World!";
// Finding the first occurence of 'o' in string s

86
char *res = strchr(s, 'o');
if (res != NULL)
printf("Character found at: %ld index", res - s);
else
printf("Character not found");
return 0;
}

Output : Character found at: 4 index


Substring Search:

strstr(str1, str2) finds the first occurrence of the substring str2 in str1.

#include <stdio.h>

#include <string.h>

int main() {

char s[] = "Hello, AIET!";

// Find the occurence of "AIET!" in string s

char *pos = strstr(s, "AIET!");

if (pos != NULL)

printf("Found");

else

printf("Not Found");

return 0;

Output : Found

87
14. Structures

Structures in C are user-defined data types that allow you to group together variables of
different data types under a single name. These variables are called members of the structure
and can be of any valid C data type, including other structures.

14.1 Key Characteristics


Structures are a way to organize related data into a single unit.
Members can be of different data types.
Structures are stored contiguously in memory, with each member occupying the amount of
memory required by its data type.
Structures can be passed to functions by value or by reference.

14.2 Use Cases


Structures are commonly used to represent real-world entities, such as employees, students,
books, etc.
They can be used to create complex data structures like linked lists and trees.
Structures are useful for organizing and managing data in a program.

14.3 Defining Structures


Structures are defined using the struct keyword, followed by a name for the structure and a list
of member variables enclosed in curly braces.
Each member variable has its own data type and name.
The structure definition ends with a semicolon.

Example :

struct Student {
char name[50];
int rollNumber;
float gpa;

88
};

In this example, Student is the name of the structure, and it contains three
members: name, rollNumber, and gpa.

14.4 Creating Structure Variables


once a structure is defined, you can create variables of that structure type.
This is done by using the struct keyword followed by the structure name and the variable name.
struct Student student1;
Here, student1 is a variable of the Student structure type.
Accessing Structure Members
Members of a structure are accessed using the dot operator (.).
[Link] = 101;
Example :
#include <stdio.h>
// Definng a structure to represent a student
struct Student {
char name[50];
int age;
float grade;
};

int main() {

// Declaring and initializing a structure variable


struct Student s1 = {"Rahul",20, 18.5};
// Designated Initializing another stucture
struct Student s2 = {.age = 18, .name ="Vikas", .grade = 22};

// Accessing structure members


printf("%s\t%d\t%.2f\n", [Link], [Link],[Link]);

89
printf("%s\t%d\t%.2f\n", [Link], [Link], [Link]);

return 0;
}

14.5 Copying Structure


Copying structure is simple as copying any other variables. For example, s1 is copied into s2 using
assignment operator.
s2=s1
But this method only creates a shallow copy of s1 i.e. if the structure s1 have some dynamic resources
allocated by malloc, and it contains pointer to that resource, then only the pointer will be copied
to s2. If the dynamic resource is also needed, then it has to be copied manually (deep copy).

#include <stdio.h>
#include <stdlib.h>
struct Student {
int id;
char grade;
};
int main() {
struct Student s1 = {1, 'A'};
// Create a copy of student s1 struct Student s1c = s1;
printf("Student 1 ID: %d\n", [Link]);
printf("Student 1 Grade: %c", [Link]);
return 0;
}
Output :

Student 1 ID : 1
Student 1 Grade: A
Passing Structure to Functions

90
Structure can be passed to a function in the same way as normal variables. Though, it is
recommended to pass it as a pointer to avoid copying a large amount of data.

#include <stdio.h>

// Structure definition
struct A {
int x;
};

// Function to increment values


void increment(struct A a, struct A* b) {
a.x++;
b->x++;
}

int main() {
struct A a = { 10 };
struct A b = { 10 };

// Passing a by value and b by pointer


increment(a, &b);

printf("a.x: %d \tb.x: %d", a.x, b.x);


return 0;
}

Output
a.x:10 b.x=13

91
14.6 Nested Structures

In C, a nested structure refers to a structure that contains another structure as one of its
members. This allows you to create more complex data types by grouping multiple structures
together, which is useful when dealing with related data that needs to be grouped within a larger
structure.
There are two ways in which we can nest one structure into another:
Embedded Structure Nesting: The structure being nested is also declared inside the parent
structure.
Separate Structure Nesting: Two structures are declared separately and then the member
structure is nested inside the parent structure.
Accessing Nested Members
We can access nested Members by using the same ( . ) dot operator two times as shown:

str_parent.[Link]

Example

#include <stdio.h>

// Child structure declaration


struct child {
int x;
char c;
};

// Parent structure declaration


struct parent {
int a;
struct child b;
};

92
int main() {
struct parent p = { 25, 195, 'A' };

// Accessing and printing nested members


printf("p.a = %d\n", p.a);
printf("p.b.x = %d\n", p.b.x);
printf("p.b.c = %c", p.b.c);
return 0;
}

p.a=25
p.b.x=195
p.b.c=A

In this code, the structure parent contains another structure child as a member.
The parent structure is then initialized with values, including the values for the child
structure's members.

14.7 Pointer to a structure


A pointer to a structure allows us to access structure members using the ( -> ) arrow
operator instead of the dot operator.

#include <stdio.h>

// Structure declaration
struct Point {
int x, y;
};

int main() {
struct Point p = { 1, 2 };

93
// ptr is a pointer to structure p
struct Point* ptr = &p;
// Accessing structure members using structure pointer
printf("%d %d", ptr->x, ptr->y);

return 0;
}

Output
12

94
15. Unions

Unions in C are a user-defined data type that allows different data types to share the same
memory location. This means that only one member of a union can store a value at any given
time.

15.1 Key points of Union


Memory Allocation:
Unlike structures, where each member has its own memory space, all members of a union
share the same memory location. The size of the union is determined by the size of its largest
member.
Data Storage:
Only one member of a union can hold a value at a time. When a value is assigned to one
member, any previous value stored in another member is overwritten.
Usage:
Unions are useful when you need to store different types of data in the same memory
location, but only one type of data is needed at a time. This can help in saving memory and
managing data efficiently.

15.2 Key Differences from Structures:


Structures allocate memory for each member, allowing them to hold values simultaneously.
Unions share the same memory location for all members, meaning only one member can hold
a value at a time.

15.3 Union Declaration


A union is declared similarly to a structure. Provide the name of the union and define its
member variables:
union union_name{
type1 member1;
type2 member2;
type3 member3;
. .
. .
}

95
After declaration of a union then create a variable of union like below:
union_name variable_name

We can also declare a variable at the declaration of union


union union_name{
type1 member1;
type2 member2;
type3 member3;
. .
. .
} variable_name;

15.4 Initialization and Accessing

The value of a union variable can be accessed using the dot (.) operator. A value can be
assigned to the union variable using the assignment operator (=).
In a union, all the variables share the same memory, so only one variable can store a value at
a time. If we try to access the value of another variable, the behavior will be undefined.

Example

#include <stdio.h>

// Define a union with


// different data types
union Student {
int rollNo;
float height;
char firstLetter;
};

96
int main() {
// Declare a union variable
union Student data;
// Assign and print the roll number
[Link] = 21;
printf("%d\n", [Link]);
[Link] = 5.2;
printf("%.2f\n", [Link]);
[Link] = 'N';
printf("%c", [Link]);
return 0;
}
Output
21
5.20
N

15.5 Comparison between structures and unions


Parameter Structure Union
Definition A structure is a user-defined data A union is a user-defined data type
type that groups different data types that allows storing different data types
into a single entity at the same memory location.
Keyword The keyword struct is used to define The keyword union is used to define a
a structure union
Size The size is the sum of the sizes of all The size is equal to the size of the
members, with padding if necessary. largest member, with possible
padding.
Memory Each member within a structure is Memory allocated is shared by
Allocation allocated unique storage area of individual members of union.
location.
Data No data overlap as members are Full data overlap as members shares
Overlap independent. the same memory.
Accessing Individual member can be accessed Only one member can be accessed at a
Members at a time. time.

97
Example
#include <stdio.h>
struct Student {
char name[50];
int age;
float grade;
};
int main() {
// Create a structure variable
struct Student s1 = {"Geek", 20, 85.5};
// Access structure members address
printf("%u\n", [Link]);
printf("%u\n", [Link]);
printf("%.u\n", [Link]);
return 0;
}

Output : ( Occupies different memory location for each variable )


0x00001000
0x00001015
0x00001020

#include <stdio.h>
union Data {
int i;
double d;
char c;
};
int main() {

98
// Create a union variable
union Data data;
// Store an integer in the union
data.i = 100;
data.d=12.9909;
data.c=’G’;
printf("%u\n", data.i);
printf("%u\n", data.d);
printf("%.u\n", data.c);
return 0;
}

Output : (Same memory location for all union variables )

0x00001025
0x00001025
0x00001025

99
16. Dynamic Memory Allocation
Dynamic memory allocation is a technique that allows programs to allocate memory during
runtime, rather than at compile time. This flexibility is crucial for handling data structures like
linked lists or arrays where the size is not known beforehand, or when memory needs to be
adjusted as the program runs. It involves using functions like malloc(), calloc(), realloc(),
and free() to request and release memory blocks..

16.1 Need for Dynamic Memory Allocation


Static memory allocation, where memory is allocated at compile time, can lead to either
wasting memory (if the allocated size is larger than needed) or running out of memory (if the
allocated size is insufficient).
Dynamic allocation provides a more flexible and efficient way to manage memory, especially
when dealing with data structures or data whose size is determined during program execution.

16.2 Benefits of Dynamic Memory Allocation

Flexibility: Programs can allocate memory only when they need it, avoiding unnecessary
memory usage.
Efficiency: Memory can be optimized by allocating only as much as is required.
Handles Data Structures: Dynamic memory allocation is essential for implementing data
structures like linked lists, stacks, queues, and trees.

16.3 Problems with Dynamic Memory Allocation

Memory Leaks:
Failing to free() allocated memory can lead to memory leaks, where the memory is no longer
accessible to the program but is also not released for other use.
Fragmentation:
Repeated allocation and deallocation can lead to memory fragmentation, where the heap is
broken up into small, non-contiguous blocks, making it difficult to find large blocks of
contiguous memory.

Working of Dynamic Memory Allocation


Allocation:
Functions like malloc() and calloc() allocate a block of memory from the heap (a region of
memory available to all programs).

100
Reallocation:
realloc() allows you to resize an existing memory block, either increasing or decreasing its
size.
Deallocation:
free() releases the allocated memory back to the heap, making it available for other
programs to use.

16.4 Common Functions of Dynamic Memory Allocation


malloc(): Allocates a single block of memory of a specified size.
calloc(): Allocates a block of memory and initializes all bytes to zero.
realloc(): Changes the size of an existing memory block.
free(): Deallocates a memory block, releasing it back to the heap.

16.5 Programming example for Static Memory Allocation


// All the variables in below program are statically allocated.
void fun()
{
int a;
}
int main()
{
int b;
int c[10]
}

16.6 Programming example for Dynamic Memory Allocation


int main()
{
// Below variables are allocated memory dynamically.
int *ptr1 = new int;
int *ptr2 = new int[10];

101
// Dynamically allocated memory is deallocated
delete ptr1;
delete [] ptr2;
}

malloc()
Purpose: Allocates a block of memory of a specified size.
Syntax: void* malloc(size_t size);
Behavior:
 Takes the size of the memory block (in bytes) as an argument.
 Returns a pointer to the beginning of the allocated memory.
 The memory is not initialized; it contains garbage values.

Returns NULL if allocation fails.
Use Case: Allocating memory for a single object or a block of data when initial values are
not critical.

Example
#include <stdlib.h>
int *ptr = (int *)malloc(10 * sizeof(int)); // Allocates space for 10 integers

calloc()
Purpose: Allocates a block of memory for an array and initializes it to zero.
Syntax: void* calloc(size_t num, size_t size);
Behavior:
 Takes the number of elements and the size of each element as
arguments.
 Returns a pointer to the beginning of the allocated memory.
 The allocated memory is initialized with all bits set to zero.

Returns NULL if allocation fails.
Use Case: Allocating memory for arrays or structures where initialization to zero is required.
Example

102
#include <stdlib.h>
int *ptr = (int *)calloc(10, sizeof(int)); // Allocates space for 10 integers and initializes to 0

realloc()
Purpose: Resizes a previously allocated memory block.
Syntax: void* realloc(void* ptr, size_t size);
Behavior:
 Takes a pointer to an allocated memory block and a new size as
arguments.
 Attempts to resize the memory block pointed to by ptr.
 If the new size is smaller, the memory block is truncated; if larger,
additional memory is allocated.
 The contents of the original memory block are preserved up to the
smaller of the old and new sizes.
 If ptr is NULL, it behaves like malloc().
 Returns a pointer to the beginning of the resized memory (may be a
different address).

Returns NULL if resizing fails.
Use Case: Dynamically adjusting the size of an allocated memory block as needed.

Example
#include <stdlib.h>
int *ptr = (int *)malloc(5 * sizeof(int)); // Initial allocation
ptr = (int *)realloc(ptr, 10 * sizeof(int)); // Resizes to 10 integers

free()
Purpose: Releases previously allocated memory, making it available for reuse.
Syntax: void free(void* ptr);
Behavior:
 Takes a pointer to the beginning of a dynamically allocated memory
block.
 Deallocates the memory block.
 The pointer ptr becomes invalid after calling free().
 Does not return a value.

103
Use Case
Releasing memory when it is no longer needed to avoid memory leaks.

Example
#include <stdlib.h>
int *ptr = (int *)malloc(10 * sizeof(int));
free(ptr); // Deallocates the memory

// Program to calculate the sum of n numbers entered by the user

// malloc() and free()

#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
ptr = (int*) malloc(n * sizeof(int));
// if memory cannot be allocated
if(ptr == NULL) {
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements: ");
for(i = 0; i < n; ++i) {
scanf("%d", ptr + i);
sum += *(ptr + i);
}
printf("Sum = %d", sum);
// deallocating the memory

104
free(ptr);
return 0;
}
// Program to calculate the sum of n numbers entered by the user

// (calloc() and free())

#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
ptr = (int*) calloc(n, sizeof(int));
if(ptr == NULL) {
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements: ");
for(i = 0; i < n; ++i) {
scanf("%d", ptr + i);
sum += *(ptr + i);
} printf("Sum = %d", sum);
free(ptr);
return 0;
}

105
17. Handling Files

Files in C are essential for storing data persistently. They allow programs to read from and
write to external storage, enabling data to be preserved even after program termination.

17.1 Types of Files:


Text Files: Store data in human-readable ASCII format.
Binary Files: Store data in binary format (0s and 1s), often used for storing compiled programs
or complex data structures.
Key Operations:
Creation: Creating new files.
Opening: Establishing a connection to an existing file.
Closing: Terminating the connection to a file.
Reading: Extracting data from a file.
Writing: Inserting or modifying data in a file.
File Pointer:
In C, a FILE pointer is used to handle file operations. It is declared using FILE *ptr;,
where ptr is a variable that will hold the address of the file in memory.

17.2 File Modes


File opening modes or access modes specify the allowed operations on the file to be opened.
They are passed as an argument to the fopen() function. Some of the commonly used file access
modes are listed below

Modes Description
r Searches file. If the file is opened successfully fopen( ) loads it into
memory and sets up a pointer that points to the first character in it.
If the file cannot be opened fopen( ) returns NULL.
rb Open for reading in binary mode. If the file does not exist, fopen(
) returns NULL.
w Open for writing in text mode. If the file exists, its contents are
overwritten. If the file doesn’t exist, a new file is created. Returns
NULL, if unable to open the file.
wb Open for writing in binary mode. If the file exists, its contents are
overwritten. If the file does not exist, it will be created.
a Searches file. If the file is opened successfully fopen( ) loads it into
memory and sets up a pointer that points to the last character in it.
It opens only in the append mode. If the file doesn’t exist, a new
file is created. Returns NULL, if unable to open the file.

106
ab Open for append in binary mode. Data is added to the end of the
file. If the file does not exist, it will be created.
r+ Searches file. It is opened successfully fopen( ) loads it into
memory and sets up a pointer that points to the first character in it.
Returns NULL, if unable to open the file.
w+ Searches file. If the file exists, its contents are overwritten. If the
file doesn’t exist a new file is created. Returns NULL, if unable to
open the file.
wb+ Open for both reading and writing in binary mode. If the file exists,
its contents are overwritten. If the file does not exist, it will be
created.
a+ Searches file. If the file is opened successfully fopen( ) loads it into
memory and sets up a pointer that points to the last character in it.
It opens the file in both reading and append mode. If the file doesn’t
exist, a new file is created. Returns NULL, if unable to open the
file.
ab+ Open for both reading and appending in binary mode. If the file
does not exist, it will be created.

17.3 Creating a File in C


The fopen() function can not only open a file but also can create a file if it does not exist
already. For that, we have to use the modes that allow the creation of a file if not found such
as w, w+, wb, wb+, a, a+, ab, and ab+.

#include <stdio.h>
#include <stdlib.h>

int main() {

// File pointer
FILE* fptr;

// Creating file using fopen()


// with access mode "w"
fptr = fopen("[Link]", "w");

// checking if the file is created


if (fptr == NULL)

107
printf("The file is not opened.");
else
printf("The file is created Successfully.");
return 0;
}

Output : The file is created Successfully

17.4 Write to a File


The file write operations can be performed by the functions fprintf() and fputs(). C
programming also provides some other functions that can be used to write data to a file such
as

Function Description
fprintf() Similar to printf(), this function uses formatted string and variable
arguments list to print output to the file
fputs() Prints the whole line in the file and a newline at the end.
fputc() Prints a single character into the file.
fputw() Prints a number to the file.
fwrite() This function writes the specified number of bytes to the binary file.

Example :
#include <stdio.h>
#include <stdlib.h>

int main() {

// File pointer
FILE* fptr;

// Get the data to be written in file


char data[50] = “ Computer Science & Engineering ";

// Creating file using fopen()

108
// with access mode "w"
fptr = fopen("[Link]", "w");

// Checking if the file is created


if (fptr == NULL)
printf("The file is not opened.");
else{
printf("The file is now opened.\n");
fputs(data, fptr);
fputs("\n", fptr);

// Closing the file using fclose()


fclose(fptr);
printf("Data successfully written in file [Link]\n");
printf("The file is now closed.");
}
return 0;
}

Output :
The file is now opened
Data successfully written in file [Link]
The file is now closed

17.5 Reading From a File

The file read operation in C can be performed using functions fscanf() or fgets(). Both the
functions performed the same operations as that of scanf() and gets but with an additional
parameter, the file pointer. There are also other functions we can use to read from a file. Such
functions are listed below

109
Function Description
fscanf() Use formatted string and variable arguments list to take input from a file.
fgets() Input the whole line from the file.
fgetc() Reads a single character from the file.
fgetw() Reads a number from a file.
fread() Reads the specified bytes of data from a binary file.

Example
#include <stdio.h>
#include <string.h>
int main() {
FILE* fptr;
// Declare the character array for the data to be read from file
char data[50];
fptr = fopen("[Link]", "r");

if (fptr == NULL) {
printf("[Link] file failed to open.");
}
else {
printf("The file is now opened.\n");

// Read the data from the file


// using fgets() method
while (fgets(data, 50, fptr)
!= NULL) {

// Print the data


printf("%s", data);
}
// Closing the file using fclose()

110
fclose(fptr);
}
return 0;
}

Output :

The file is now opened


Computer Science & Engineering

17.6 Closing a File

The fclose() function is used to close the file. After successful file operations, you must
always close a file to remove it from the memory.

Syntax:

Fclose(file_pointer);

Moving File Pointer


File pointer generally points to the position according to the mode or last read/write
operation. We can manually move this pointer to any position in the file
using fseek() function.

Syntax:

fseek(fptr, offset, pos);

where, pos is the position from where offset is counted and offset is the number of positions
to shift from pos (it can be negative or positive).

111
Example:
While writing to a file opened in rw+ mode, the file pointer moves to the end of the file. In
case where we want to replace a word, then first we have to move the file pointer to the
position where that word starts.

#include <stdio.h>
#include <stdlib.h>

int main() {

// File pointer
FILE* fptr;

// Get the data to be written in file


char data[50] = "Computer Science & Engineering ";

// Creating file using fopen()


// with access mode "w"
fptr = fopen("[Link]", "w");

// Checking if the file is created


if (fptr == NULL)
printf("The file is not opened.");
else{
printf("The file is now opened.\n");
fputs(data, fptr);
fputs("\n", fptr);
fseek(fptr, -6, SEEK_END);

fputs("Hello", fptr);

112
// Closing the file using fclose()
fclose(fptr);
printf("Data successfully written in file "
"[Link]\n");
printf("The file is now closed.");
}
return 0;
}

Output :
The file is now opened
Data successfully written in file [Link]
The file is now closed

113

Common questions

Powered by AI

File modes in C specify the type of operations allowed on an opened file, such as reading, writing, or appending data. These modes guide how the file is accessed or modified. For instance, 'r' opens a file for reading, while 'w' opens it for writing. The significance of file modes lies in their ability to dictate the interaction with files, ensuring the correct type of file access and preventing erroneous operations, such as editing a non-existent file .

The role of a for loop in a C program is to repeat a block of code a known number of times, providing an efficient way to automate repetitive tasks like summing a series of numbers. It consists of three parts: initialization, condition, and increment/decrement, which together control the loop's execution. The initialization sets up the loop variable, the condition tests whether the loop should continue, and the increment/decrement updates the loop variable. In the context of summing a series, the for loop iterates through each number, accumulating the sum in a variable until the condition is no longer met .

Arrays in C differ from individual variable declarations in that they allow the storage of multiple values of the same data type using a single identifier. Syntax-wise, arrays are declared with a type, a name, and a size, while variables are declared with just a type and a name. Usage-wise, arrays enable efficient management of collections of data, facilitating operations across all elements with indexed access, whereas variables represent single data items, limiting their use to single-value scenarios .

Defining a function in C involves specifying the function's return type, name, parameters, and body. The body contains statements that execute when the function is called. To call a function, you use its name followed by parentheses, optionally passing arguments. One advantage of using functions is improved code reusability; functions encapsulate specific tasks, which can be reused in different parts of a program or shared across different projects, reducing duplication .

Modularity, achieved through user-defined functions in C, is crucial because it breaks down a program into smaller, manageable chunks. This facilitates easier debugging, testing, and maintenance. Each function performs a specific task, making the code more readable and reducing redundancy. Functions enhance reusability, allowing the same code to be used multiple times in a program or across different projects, which streamlines development and ensures consistency .

File operations in C enhance data persistence by allowing programs to read from and write to external storage media, ensuring data is available even after the program terminates. The key operations involved in file handling include creating files, opening existing files, reading data from files, writing data to files, and closing files. These operations utilize a FILE pointer to interact with files, enabling efficient data management and long-term storage of program output or user input .

Pointers can be used to modify values in an array by directly accessing memory locations. By obtaining the address of an array element, a pointer can be used to change the value stored in that memory location. This approach has several advantages: it allows for more efficient memory usage as it avoids the need to copy data, facilitates the implementation of dynamic data structures, and enables complex operations like pointer arithmetic, which can lead to more concise and faster code execution .

In a for loop, initialization sets the starting point by establishing the initial value of the loop counter. The condition checks before each iteration to determine if the loop should continue, ensuring the loop executes only as long as the condition is true. Increment/decrement updates the loop counter after each iteration, moving the loop closer to termination. Together, these components control the iteration process, providing a structured way to repeat actions until a specific condition is met .

Arrays in C provide efficient data storage and access by storing elements in contiguous memory locations, which allows direct access to any element using its index. This contiguous memory allocation enhances performance by making access times predictable and fast. However, one trade-off is the fixed size of arrays; once declared, the size cannot be dynamically adjusted, which can lead to inefficient memory usage if the allocated array size is not fully utilized .

The return statement in C signifies the end of a function and can be used to return a value to the calling function. In the main function, return 0 typically indicates successful execution of the program. In other functions, the return statement is used to send a specific value back to the caller, enabling functions to produce output based on their logical operations. This facilitates data flow between functions and enhances modularity .

You might also like