Unit-3 Derived Data Types in C
- Arrays: One Dimensional arrays Declaration, Initialization and Memory representation; Two Dimensional arrays -Declaration, Initialization and Memory representation.
- Strings: Declaring & Initializing string variables; String handling functions, Character handling functions.
Array
An array is a collection or group of elements of the same data types that share a common name and stored in a contiguous memory locations.
An Array can store many no. of elements depend on the given size.
e.g.,
a[5] = {15, 20, 25, 30, 35}
0 1 2 3 4a[0] = 15, a[1] = 20, … a[4] = 35Types of Arrays
- One dimensional Array
- Two dimensional Array
- Multi dimensional Array
- One dimensional Array- The data are organised linearly in only one direction.
- Two dimensional Array- Two dimensional arrays can have only two dimensions & data is shown in the form of matrices.
- Multidimensional- It can have 3 or more dimensions. In 3 dimensional array data can be organised in cube. Cube consist of numerous no. of two dimensional arrays.
One Dimensional
Declaration of Arrays
C provides two different types of arrays based on their size.
- Fixed length array In fixed length array the size of the array is known when the program is written. An array should be declared before using it in our program.
Syntax:
data_type Array_name[size of array];e.g.,
int a[10];- Variable length array In variable length array the size of the array is not known when the program is written. Until now we have used an array that had constant size. e.g.,
int n; => 30
scanf("%d", &n);
int a[n]; => a[30];Storing Values in Array In the declaration we just allocated space for the elements on those locations are empty. To store values in the array we have to follow either of the below ways.
- Initialization
- Inputting values
- Assigning values
Initialization of One Dimensional Arrays
After declaring array it should be initialized with some values. Initialization of Array can be done in two ways.
- Compile time initialization In this we can assign values to array directly in the program using following syntax.
data_type Array_name[size] = {list of values};int a[5] = {1, 2, 3, 4, 5};
0 1 2 3 4Examples
- Basic Initialization
int a[5] = {1, 2, 3, 4, 5};- Initialize without size
int a[] = {1, 2, 3, 4, 5};- Partial Initialization
int a[5] = {1, 2};int a[5] = {10, 20, 2, 4, 7};
for(i = 0; i < 5; i++)
{
printf("%d\t", a[i]);
}
return 0;Output:
10 20 2 4 7- Run time initialization In this we have to assign values during run time.
Write a C program to print the values assigned at the time of compilation time.
#include <stdio.h>
int main()
{
int i, a[5];
printf("Enter Array Values:");
for(i = 0; i < 5; i++) // store
{
scanf("%d", &a[i]);
}
for(i = 0; i < 5; i++) // print
{
printf("%d\t", a[i]);
}
return 0;
}Output:
1
2
3
4
5
1 2 3 4 5Searching
a[6] = {5, 10, 20, 30, 40, 50}
There are two types of searching:
- Linear search
- Binary search
Linear Search To check element one by one.
Steps
- It checks each element one by one from beginning of the array.
- Until the desired element (key) is found.
- Or the array ends.
That’s why it is also called sequential search.
When to use
- Small arrays
- Unsorted arrays
- When data is random
- Easy and fast to write
Example: Write a C program to implement linear search
#include <stdio.h>
int main()
{
int a[5] = {5,10,15,40,30};
int key, i, found = 0;
printf("Enter element to be searched:");
scanf("%d", &key);
for(i = 0; i < 5; i++)
{
if(a[i] == key)
{
found++;
break;
}
}
if(found == 1)
printf("Element found");
else
printf("Element not found");
return 0;
}Output:
Element not foundSorting
Sorting is a process of arranging the elements of an array in a specific order.
Most commonly:
- Ascending order (S to B)
- Descending order (B to S)
Types
- Internal
- External
Internal
- Bubble
- Insertion
- Selection
- Heap sort
External
Extended storage used like USB, hard disk, etc.
- Merge sort
Bubble Sort It is a sorting technique that repeatedly compares adjacent elements and swaps them if they are in wrong order. Example
#include <stdio.h>
int main()
{
int a[5] = {10,5,1,3,15};
int i, j, n = 5, temp;
for(i = 0; i < n - 1; i++)
{
for(j = 0; j < n - 1; j++)
{
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
printf("Sorted elements are:\n");
for(i = 0; i < n; i++)
printf("%d\t", a[i]);
return 0;
}Output:
1 3 5 10 15Updating (Changing an existing element) Change the value stored at a certain index. Example
#include <stdio.h>
int main()
{
int a[6] = {5,10,15,20,30,50};
int n = 6, i;
a[3] = 25;
printf("Updated array is:\n");
for(i = 0; i < n; i++)
printf("%d\t", a[i]);
return 0;
}Output:
5 10 15 25 30 502D Arrays
A 2D array is an array of rows and columns. It looks like a table or matrix.
Example
1 2 3
4 5 6Declaration
data_type array_name[rows][columns]Example
int a[2][3];
float b[2][3];Meaning: a[2][3] has 2 rows and 3 columns and can store 6 elements.
Index Range
- Row index → 0 to rows − 1
- Column index → 0 to columns − 1
Initialization of 2D Arrays
You can initialize a 2D array in two ways:
- Compile-time initialization
- Run-time initialization
Compile-Time Initialization Done when the array is declared. Values are fixed in the source code.
int a[2][3] = {{1,2,3},{4,5,6}};Row-wise Initialization
int a[2][3] = {1,2,3,4,5,6};Run-Time Initialization
#include <stdio.h>
int main()
{
int a[3][4];
int i, j;
for(i = 0; i < 3; i++)
{
for(j = 0; j < 4; j++)
{
scanf("%d", &a[i][j]);
}
}
for(i = 0; i < 3; i++)
{
for(j = 0; j < 4; j++)
{
printf("%d\t", a[i][j]);
}
printf("\n");
}
return 0;
}Output:
1 2 3 4
5 6 7 8
9 10 11 12Memory Representation of 2D Arrays
In C, 2D arrays are stored in row-major order. All elements of row (0) are stored first, then row (1), and so on.
Example
a[3][3]
| Element | Value | Address |
|---|---|---|
| a[0][0] | 10 | 1000 |
| a[0][1] | 20 | 1004 |
| a[0][2] | 30 | 1008 |
| a[1][0] | 40 | 1012 |
| a[1][1] | 50 | 1016 |
| a[1][2] | 60 | 1020 |
| a[2][0] | 70 | 1024 |
| a[2][1] | 80 | 1028 |
| a[2][2] | 90 | 1032 |
Strings in C
A string in C is a 1D array of characters that ends with a null character ('\0').
Example
VINAY\0Declaring String Variables
char string_name[size];Example:
char name[20];Initialization of String Variable
Strings can be initialized in two ways:
- Compile-Time Initialization Using double quotes
" ".
char name[20] = "VINAY";This automatically adds the null character ('\0') at the end.
Another method:
char name[20] = {'V','I','N','A','Y'};- Run-Time Initialization Using
scanf.
char name[20];
scanf("%s", name);String Program (Compile-Time)
#include <stdio.h>
int main()
{
char name[20] = "Vinay";
printf("Your name is: %s", name);
return 0;
}Output:
Your name is: VinayString Program (Run-Time)
#include <stdio.h>
int main()
{
char name[20];
printf("Enter your name:");
scanf("%s", name);
return 0;
}Output:
Enter your name: VinayStandard Library String Functions
- The C library supplies several string handling functions.
- ANSI C uses
string.hheader file to provide the prototypes. - To use these functions you must
#include:
#include <string.h>strlen() – Length of String Finds number of characters before null character ('\0').
strcat() – Join Two Strings Appends source string to the end of destination string.
#include <stdio.h>
#include <string.h>
int main()
{
char s1[] = "Hello";
char s2[] = "World";
char result[20];
strcpy(result, s1);
strcat(result, s2);
printf(result);
return 0;
}Output:
HelloWorldstrcpy() – Copy One String to Another Copies the content of source string into destination string including null character ('\0').
String Comparison and Manipulation Functions in C
strcmp() – Compare Two Strings strcmp() compares two strings character by character. Return Values
0→ Strings are equal< 0→ First string is smaller than second string> 0→ First string is greater than second string
Example Program
#include <stdio.h>
#include <string.h>
int main()
{
char s1[50], s2[50];
printf("Enter first string:\n");
gets(s1);
printf("Enter second string:\n");
gets(s2);
if (strcmp(s1, s2) == 0)
{
printf("Equal");
}
else
{
printf("Not equal");
}
return 0;
}strrev() – Reverse a String This function reverses a string. The first character becomes the last, the second becomes the second last, and so on.
Example Program
#include <stdio.h>
#include <string.h>
int main()
{
char s1[50];
printf("Enter string:\n");
gets(s1);
printf("Reverse string is:\n");
printf("%s", strrev(s1));
return 0;
}Output:
Input : 12345
Output: 54321strupr() – Convert String to Uppercase This function converts all characters of a string into uppercase letters.
Example Program
#include <stdio.h>
#include <string.h>
int main()
{
char s1[50];
printf("Enter string:\n");
gets(s1);
printf("Uppercase string is:\n");
printf("%s", strupr(s1));
return 0;
}strlwr() – Convert String to Lowercase This function converts all characters of a string into lowercase letters.
Example Program
#include <stdio.h>
#include <string.h>
int main()
{
char s1[50];
printf("Enter string:\n");
gets(s1);
printf("Lowercase string is:\n");
printf("%s", strlwr(s1));
return 0;
}Output:
Enter string:
Test1 test2
Lowercase string is:
test1 test2