Unit Ii
Unit Ii
Introduction to C Programming: Control Flow, Conditional Branching Statements: if, if-else, if-
else—if, switch. Basic Loop Structures: while, do-while loops, for loop, nested loops, The Break
and Continue Statements, goto statement.
Arrays: Introduction, Operations on Arrays, Arrays as Function Arguments, Two Dimensional
Arrays, Multidimensional Arrays.
Strings: String Fundamentals, String Processing with and without Library Functions, Pointers and
Strings.
Introduction:
Control flow refers to the order in which individual instructions, statements, or function calls are
executed in a program. It determines how the execution progresses from one point in the program
to another, based on conditions, loops, or function calls.
Types of Control Statements in C
The primary types of control statements in C are:
1.Decision-making control statements
Simple if statement
If-else statements
Nested if-else statements
else-if ladder
Switch
While Loop
Do-while Loop
For Loop
3. Jump statements in C
break.
Continue.
Goto
Conditional statements: The conditional statements are
a. Simple if statement:
If the Boolean condition 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.
Syn: if(condition)
{
Statements;
}
Ex:
int main()
{
int m=40,n=40;
if (m == n)
{
printf("m and n are equal");
}
}
b. If..else statement:
In C if else control statement, group of statements are executed when condition is true. If
condition is false, then else part statements are executed.
Syn: if (condition)
{
Statement1;
Statement2;
}
else
{
Statement3;
Statement4;
}
Switch Statement:
the switch statement will allow multi-way branching. Depending on the expression, the
control is transferred to that particular case label and executed the statements under it. If none of
the cases are matched with the switch expression, then the default statement is executed.
The syntax of the switch statement is as given below:
switch(expression)
{
case value1: statement_1;
break;
case value2: statement_2;
break;
Explanation:
The outer loop runs 3 times (i = 1, i = 2, i = 3).
For each iteration of the outer loop, the inner loop runs completely, i.e., 2 times (j = 1, j =
2).
This results in the inner loop being executed a total of 3 * 2 = 6 times.
2. While loop:
The while statement continually executes a block of statements while a particular
condition is true. It is called as entry level loop control structure.
Syntax: while (condition)
{
statement(s);
Increment/decrement;
}
The while statement evaluates condition, which must return a boolean value. If the
condition evaluates to true, the while statement executes the statement(s) in the while block.
The while statement continues testing the condition and executing its block until the condition
evaluates to false.
Ex:
#include <stdio.h>
int main()
{
int i=3;
while(i<10)
{
printf("%d\n",i);
i++;
}
}
2. Do while loop:
In do..while loop control statement, while loop is executed irrespective of the condition for
first time. Then 2nd time onwards, loop is executed until condition becomes false.
It called as exit level loop control structure.
Syn: do {
statements;
}
while (condition);
Ex: #include <stdio.h>
int main() {
int i=1;
do {
printf("Value of i is %d\n",i);
i++;
}while(i<=4 &&i>=2);
}
Difference between while & do while loops in C:
S.no while do while
1 Loop is executed only Loop is executed for first time irrespective of the condition.
when condition is true. After executing while loop for first time, then condition is
checked.
2 Entry level loop control Exit level loop control structure.
structure
goto statement in C:
goto statements is used to transfer the normal flow of a program to the specified label in
the program.
Below is the syntax for goto statement in C.
go to label;
…….
…….
LABEL:
statements;
Example program for goto statement in C:
#include <stdio.h>
int main()
{
int i;
for(i=0;i<10;i++)
{
if(i==5)
{
printf("\nWe are using goto statement when i = 5");
goto GMRIT;
}
printf("%d ",i);
}
GMTIT :printf("\nNow, we are stdying in gmrit \n");
}
Explain Array in C Language:
An array in C is a fixed-size collection of similar data items stored in contiguous memory locations.
It can be used to store the collection of primitive data types such as int, char, float, etc., and also
derived and user-defined data types such as pointers, structures, etc.
Def: An array may be defined as finite ordered set of homogenous elements.i.e limited number
of elements of same data type
Array Declaration
In C, we have to declare the array like any other variable before using it. We can declare an array
by specifying its name, the type of its elements, and the size of its dimensions. When we declare
an array in C, the compiler allocates the memory block of the specified size to the array name.
array_name [index];
One thing to note is that the indexing in the array always starts with 0, i.e., the first element is at
index 0 and the last element is at N – 1 where N is the number of elements in the array.
Operations on arrays:Operations on arrays involve various tasks that manipulate or utilize the
elements within the array. Arrays are widely used for storing data, and operations on them are
fundamental in programming. Below are common operations that can be performed on arrays:
1. Traversal
Traversing means visiting each element of the array. This is often done using loops to access and
possibly modify the elements.
int arr[] = {10, 20, 30, 40};
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
10 20 30 40
2. Insertion
Inserting an element into an array can involve shifting elements if the array has fixed size. For
dynamic arrays, elements can be added easily.
for (int i = n; i > pos; i--) {
arr[i] = arr[i-1]; } // Shift elements to the right
arr[pos] = x; // Insert new element
n++; // Increment the size
3. Deletion
Deleting an element from an array involves shifting elements to fill the gap left by the deleted
element.
for (int i = pos; i < n-1; i++) {
arr[i] = arr[i+1]; // Shift elements to the left
}
n--; // Decrease the size
4. Searching
Searching involves finding the index of an element in the array. There are different searching
techniques like linear search and binary search.
Linear search: Check each element one by one.
Binary search: Used on sorted arrays, divides the array into halves to search.
for (int i = 0; i < n; i++) {
if (arr[i] == key) {
found = i; break; } }
5. Sorting
Sorting arranges the elements of the array in a specific order (ascending or descending). Common
algorithms include Bubble Sort, Selection Sort, Merge Sort, and Quick Sort.
Example
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
} } }
6. Merging
Merging combines two or more sorted arrays into a single sorted array.
7. Reversing
Reversing an array means swapping elements from the beginning and end of the array.
8.Update Array Element
We can update the value of an element at the given index i in a similar way to accessing an
element by using the array subscript operator [ ] and assignment operator =.
array_name[i] = new_value;
Types of Array in C
There are two types of arrays based on the number of dimensions it has. They are as follows:
One Dimensional Arrays (1D Array)
The One-dimensional arrays, also known as 1-D arrays in C are those arrays that have only one
dimension.
Syntax of 1D Array in C
array_name [size];
Example : int arr[5];
// C Program to illustrate the use of 1D array
#include <stdio.h>
int main() {
// Declare and initialize an array
int arr[] = {10, 20, 30, 40, 50};
int n = sizeof(arr) / sizeof(arr[0]); // Calculate the number of elements in the array
// Print each element of the array
printf("Array elements are:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
return 0;
}
Output
Array elements are:
10 20 30 40 50
Array of Characters (Strings)
In C, we store the words, i.e., a sequence of characters in the form of an array of characters
terminated by a NULL character. These are called strings in C language.
// C Program to illustrate strings
#include <stdio.h>
int main() {
char arr[6] = { 'M', 'A', 'D', 'H', 'A', ’v’ ,’I’, '\0' }; // creating array of
character
int i = 0;
while (arr[i]) {
printf("%c", arr[i++]);
} return 0; }
Output
MADHAVI
2. Multidimensional Array in C
Multi-dimensional Arrays in C are those arrays that have more than one dimension. Some of the
popular multidimensional arrays are 2D arrays and 3D arrays. We can declare arrays with more
dimensions than 3d arrays but they are avoided as they get very complex and occupy a large
amount of space.
A. Two-Dimensional Array in C
A Two-Dimensional array or 2D array in C is an array that has exactly two dimensions. They can be
visualized in the form of rows and columns organized in a two-dimensional plane.
Syntax of 2D Array in C
array_name[size1] [size2];
Here, size1: Size of the first dimension. size2: Size of the second dimension.
Example of 2D Array in C: int arr[2][3] = { 10, 20, 30, 40, 50, 60 };
#include <stdio.h>
int main() {
// Define a 2D array with 3 rows and 3 columns
int arr[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", arr[i][j]); // Print the element at position (i, j)
}
printf("\n"); // Move to the next line after printing one row
}
return 0;
}
B. Three-Dimensional Array in C
Another popular form of a multi-dimensional array is Three Dimensional Array or 3D Array. A 3D
array has exactly three dimensions. It can be visualized as a collection of 2D arrays stacked on top
of each other to create the third dimension.
Syntax of 3D Array in C
array_name [size1] [size2] [size3];
C Program to find the largest number in an array using loop
#include <stdio.h>
int main()
{
int size, i, largest;