Official Study Materials for Computer Science
Access comprehensive study materials and useful resources for Programming in C using the Problem Solving Through C Unit 05 Arrays Notes Set 01. Designed to align with the 2026-27 academic guidelines, these advanced resources help Computer Science students reinforce core concepts beyond standard textbooks.
Advanced Resources for Computer Science
Navigate directly to the advanced Computer Science study materials using the digital viewer below. Each resource pack includes detailed conceptual summaries and solved practice questions, allowing students to instantly reinforce their learning.
This page covers arrays in C: declaring, naming and accessing one-dimensional arrays, linear and binary search, inserting, deleting and finding the largest or smallest element, followed by two-dimensional arrays and matrix addition, multiplication and transpose. It then explains sparse matrices, null-terminated character arrays and the standard string functions such as strcpy, strcat, strlen and strcmp.
Array
An array is a collection of same type of elements which are sheltered under a common name.
An array can be visualized as a row in a table, whose each successive block can be thought of as memory bytes containing one element.
Look at the figure below :
Figure (described): an array num of four elements, num[0] = 2, num[1] = 8, num[2] = 7 and num[3] = 6, labelled Element-1 to Element-4.
An Array of four elements:
The number of 8 bit bytes that each element occupies depends on the type of array.
If type of array is 'char' then it means the array stores character elements.
Since each character occupies one byte so elements of a character array occupy one byte each.
An array is a collection of data storage locations, each having the same data type and the same name.
Each storage location in an array is called an array element.
Arrays are of two types:
Single or One-dimensional arrays.
Multidimensional e.g. two dimensional arrays.
Single-Dimensional Arrays
A single-dimensional array has only a single subscript.
A subscript is a number in brackets that follows an array name.
This number can identify the number of individual elements in the array.
An example should make this clear.
For the company expenditure program, the users could use the following line to declare an array of type float:
float expense[12];
The array is named expense, and it contains 12 elements.
Each of the 12 elements is the exact equivalent of a single float variable.
All of C's data types can be used for creating arrays.
C array elements are always numbered starting at 0, so the 12 elements of expense are numbered 0 through 11.
In the above example, January's expenditure would be stored in expense [0], February's in expense [1], and so on.
When an array is declared, the compiler sets aside a block of memory large enough to hold the entire array.
Individual array elements are stored in sequential memory locations.
Naming and Declaring Arrays
The rules for assigning names to arrays are the same as for variable names.
An array name must be unique in that program which means should not be used for another array or for any other identifier (variable, constant).
The array declaration consists of an array name followed by the number of elements in the array must be enclosed in square brackets.
When the users declare an array, he can specify the number of elements with a literal constant or with a symbolic constant created with the #define directive.
Array elements
Size of array defines the number of elements in an array.
Each element of array can be accessed and used by user according to the need of program.
For example:
int age[5];
Note that, the first element is numbered 0 and so on.
Figure (described): the five boxes age[0], age[1], age[2], age[3] and age[4] side by side.
Here, the size of array age is 5 times the size of int because there are 5 elements.
Suppose, the starting array address of age[0] is 2120d and the size of int be 4 bytes.
Then, the next address (address of a[1]) will be 2124d, address of a[2] will be 2128d and so on.
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 10th element from the array and assign the value to salary variable.
Following is an example which will use three 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;
}When the above code is compiled and executed, it produces the following result:
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.
Searching an Array - Introduction
A common array operation is to search the values of an array for a particular value.
Users may be doing this in order to find and change a value or to find and process information in a corresponding element of a paired or parallel array.
A linear Search of an Array
A linear search starts searching for the value with the first element of the array and continues through the array, one element at a time until the wanted value is found or the user reach the end of the array.
Linear search is also known as sequential search that is suitable for searching a list of data for a particular value.
Listed below is the flow for a linear search in an array.
Figure (described): start; loop i from 0 to the size of the array; if a[i] equals the search key return i, otherwise continue; return -1 if the loop ends without a match; end.
Example
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[50],temp[50],i,j,item,no,t;
char ch;
cout<<"\n ENTER HOW MANY ELMENT TO BE PROCESSED:?\n";
cin>>no;
cout<<"\n ENTER YOUR ELEMENT\n";
for(i=1;i<=no;i++)
{
cin>>arr[i];
}
cout<<"\n THE ARRAY ELEMENTS ARE:...\n";
for(j=1;j<=no;j++)
{
cout<<"\t"<<arr[j]<<endl;
}
int a=1;
do
{
cout<<"\n\n\n\n\n ENTER YOUR ELMENT TO BE SEARCHED ?\n";
cin>>item;
do
{
while(arr[a]!=item)
{
a=a+1;
}
if(a>no)
{
cout<<"\n Search is unsuccess full...\n";
}
else
{
int loc=a;
cout<<"\n Item is found in loation...: "<<loc<<endl;
}
}
while(i<no);
cout<<"\n DO YOU FINISHED...?\n OR YOU WANT TO USE PROGRAM again? (press Y / N)\n";
cin>>ch;
}
while(ch=='y');
}A Binary Search of an Array
A binary search requires that the element values in the array be in order, such as ascending order, from smallest to largest.
A binary search algorithm (or binary chop) is a technique for locating a particular value in a sorted list.
The method makes progressively better guesses, and closes in on the location of the sought value by selecting the middle element in the span (which, because the list is in sorted order, is the median value), comparing its value to the target value, and determining if it is greater than, less than, or equal to the target value.
A guessed index whose value turns out to be too high becomes the new upper bound of the span, and if its value is too low that index becomes the new lower bound.
Now, the user can search the array by:
Finding the center element value.
If this is the wanted value, the users are done.
Otherwise, if they required value is greater than this value and it lies in the top half of the array it eliminates the bottom half of the array and it finds the new center of what is left.
Otherwise, if they wanted value is less than this value and it lies in the bottom half of the array, it eliminates the top of the array and finds the new center of what is left.
This is done until the user find the value or he run out of array.
Listed below is the flow for a binary search in an array:
Figure (described): begin with low = 0 and high = length - 1; while low <= high compute mid = (low + high)/2; if list[mid] equals the key the target is found; if the key is smaller set high = mid - 1, otherwise set low = mid + 1; when low exceeds high the item is not found; end.
Example: Binary Search
#include<stdio.h>
int main(){
int a[10],i,n,m,c=0,l,u,mid;
printf("Enter the size of an array: ");
scanf("%d",&n);
printf("Enter the elements in ascending order: ");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
printf("Enter the number to be search: ");
scanf("%d",&m);
l=0,u=n-1;
while(l<=u){
mid=(l+u)/2;
if(m==a[mid]){
c=1;
break;
}
else if(m<a[mid]){
u=mid-1;
}
else
l=mid+1;
}
if(c==0)
printf("The number is not found.");
else
printf("The number is found.");
return 0;
}Inserting Elements into an Array
Given below is the flow for inserting the values into an array at run time.
Figure (described): the array grows one element at a time as the values 2, 7, 1, 3 and 8 are entered in turn, giving 2; 2 7; 2 7 1; 2 7 1 3; and finally 2 7 1 3 8.
Example
#include <stdio.h>
void main()
{
int array[10];
int i, j, n, m, temp, key, pos;
printf("Enter how many elements \n");
scanf("%d", &n);
printf("Enter the elements \n");
for (i = 0; i < n; i++)
{scanf("%d", &array[i]);}
printf("Input array elements are \n");
for (i = 0; i < n; i++)
{printf("%d\n", array[i]);}
for (i = 0; i < n; i++)
{ for (j = i + 1; j < n; j++)
{ if (array[i] > array[j])
{ temp = array[i];
array[i] = array[j];
array[j] = temp;
} } }
printf("Sorted list is \n");
for (i = 0; i < n; i++)
{ printf("%d\n", array[i]); }
printf("Enter the element to be inserted \n");
scanf("%d", &key);
for (i = 0; i < n; i++)
{ if (key < array[i])
{ pos = i;
break; } }
m = n - pos + 1 ;
for (i = 0; i <= m; i++)
{ array[n - i + 2] = array[n - i + 1] ; }
array[pos] = key;
printf("Final list is \n");
for (i = 0; i < n + 1; i++)
{ printf("%d\n", array[i]); }
}Deleting Elements from an Array
Example
#include <stdio.h>
int main()
{
int array[100], position, c, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d", &array[c]);
printf("Enter the location where you wish to delete element\n");
scanf("%d", &position);
if ( position >= n+1 )
printf("Deletion not possible.\n");
else
{
for ( c = position - 1 ; c < n - 1 ; c++ )
array[c] = array[c+1];
printf("Resultant array is\n");
for( c = 0 ; c < n - 1 ; c++ )
printf("%d\n", array[c]);
}
return 0;
}Find the Largest/Smallest of the Elements of an Array
This time, we are looking for the largest value in the array.
We just set largest to the value of the first element of the array.
Then we compare this value to each of the other elements in the array.
If one is larger, we replace the value in largest with the value and continue to check the rest of the array.
The following the flow to find the largest of the elements of an array.
Figure (described): start; i = 0; Largest = a[0]; while i is not N, if a[i] > Largest then Largest = a[i]; i = i + 1.
Example
#include <stdio.h>
int main()
{
int array[100], maximum, size, c, location = 1;
printf("Enter the number of elements in array\n");
scanf("%d", &size);
printf("Enter %d integers\n", size);
for (c = 0; c < size; c++)
scanf("%d", &array[c]);
maximum = array[0];
for (c = 1; c < size; c++)
{
if (array[c] > maximum)
{
maximum = array[c];
location = c+1;
}
}
printf("Maximum element is present at location %d and it's value is %d.\n", location, maximum);
return 0;
}Find the Smallest of the Elements of an Array
We set smallest to the value of the first element of the array and start comparing it to other elements in the array.
The following is the flow to find the smallest of the elements of an array:
Figure (described): start; i = 0; smallest = a[0]; while i is not N, if a[i] < smallest then smallest = a[i]; i = i + 1.
Example
#include <stdio.h>
int main()
{
int array[100], minimum, size, c, location = 1;
printf("Enter the number of elements in array\n");
scanf("%d", &size);
printf("Enter %d integers\n", size);
for ( c = 0 ; c < size ; c++ )
scanf("%d", &array[c]);
minimum = array[0];
for ( c = 1 ; c < size ; c++ )
{
if ( array[c] < minimum )
{
minimum = array[c];
location = c+1;
}
}
printf("Minimum element is present at location %d and it's value is %d.\n", location, minimum);
return 0;
}Introduction to Multidimensional Arrays - Two-Dimensional Arrays
A two-dimensional array has two subscripts.
A two-dimensional array has a row-and-column structure as shown below:
int matrix[row][column];
Where row can only be an integer.
And column can only be an integer.
What is a Two-dimensional array?
Algebraic notation: B = | 51 52 53 | (Row 1)
| 54 55 56 | (Row 2)
Col 1 Col 2 Col 3
C notation: int b[2][3] = {{51, 52, 53}, {54, 55, 56}};
(array type: int, array name: b, array dimension = 2,
two rows, three columns)Indexes in 2D arrays
Assume that the two dimensional array called val is declared and looks like the following:
| val | Col 0 | Col 1 | Col 2 | Col 3 |
|---|---|---|---|---|
| Row 0 | 8 | 16 | 9 | 52 |
| Row 1 | 3 | 15 | 27 | 6 |
| Row 2 | 14 | 25 | 2 | 10 |
To access the cell containing 6, we reference val[1][3],that is, row 1, column 3.
DECLARATION
How to declare a multidimensional array?
int b[2][3];
The name of the array to be b.
The type of the array elements to be int.
The dimension to be 2 (two pairs of brackets []).
The number of elements or size to be 2*3 = 6.
INITIALIZATION
How to initialize a Two-Dimensional array?
Initialized directly in the declaration statement.
int b[2][3] = {51, 52, 53, 54, 55, 56};
b[0][0] = 51 b[0][1] = 52 b[0][2] = 53Use braces to separate rows in 2-D arrays.
int c[4][3] = { {1,2,3},
{4,5,6},
{7,8,9},
{10,11,12} };
int c[][3] = { {1,2,3},
{4,5,6},
{7,8,9},
{10,11,12} };Implicitly declares the number of rows to be 4.
Input of Two-Dimensional Arrays
Data may be input into two-dimensional arrays using nested for loops interactively or with data files.
A nested for loop is used to input elements in a two dimensional array.
In this way by increasing the index value of the array the elements can be entered in a 2d array.
Output of Two-Dimensional Arrays
The output of two-dimensional arrays should be in the form of rows and columns for readability.
Nested for loops are used to print the rows and columns in row and column order.
By increasing the index value of the array the elements stored at that index value are printed on the output screen.
Example: A program to input elements in a two dimensional array and print it
#include<stdio.h>
int main()
{
int i,j;
// declaring and Initializing array
int arr[2][2] = {10,20,30,40};
/* Above array can be initialized as below also
arr[0][0] = 10; // Initializing array
arr[0][1] = 20;
arr[1][0] = 30;
arr[1][1] = 40; */
for (i=0;i<2;i++)
{
for (j=0;j<2;j++)
{
// Accessing variables
printf("value of arr[%d] [%d] : %d\n",i,j,arr[i][j]);
}
}
}
Output:
value of arr[0] [0] is 10
value of arr[0] [1] is 20
value of arr[1] [0] is 30
value of arr[1] [1] is 40Passing Arrays to Functions
Two-dimensional arrays may by passed by array name.
Because arrays are stored by rows, in order to accurately locate an element, a function must know the length of a row: that is the number of columns.
This must be included in both the function prototype and the header of the function definition.
Passing Fixed Sized Arrays.
Passing Array Elements.
Multidimensional Array
A multidimensional array has more than one subscript.
A two-dimensional array has two subscripts.
A three-dimensional array has three subscripts, and so on.
There is no limit to the number of dimensions a C array can have.
Example
#include <stdio.h>
int main()
{
int i;
int arr[5] = {10,20,30,40,50};
// declaring and Initializing array in C
//To initialize all array elements to 0, use int arr[5]={0};
/* Above array can be initialized as below also
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50; */
for (i=0;i<5;i++)
{
// Accessing each variable
printf("value of arr[%d] is %d \n", i, arr[i]);
}
}
Output:
value of arr[0] is 10
value of arr[1] is 20
value of arr[2] is 30
value of arr[3] is 40
value of arr[4] is 50Addition/Multiplication of Two Matrices
Addition of Two Matrices: Program and Output
#include <stdio.h>
int main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
scanf("%d", &first[c][d]);
printf("Enter the elements of second matrix\n");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
scanf("%d", &second[c][d]);
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
sum[c][d] = first[c][d] + second[c][d];
printf("Sum of entered matrices:-\n");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
printf("%d\t", sum[c][d]);
printf("\n");
}
return 0;
}
Output:
Enter the number of rows and columns of matrix
2
2
Enter the elements of first matrix
1 2
3 4
Enter the elements of second matrix
5 6
2 1
Sum of entered matrices:-
6 8
5 5Multiplication of Two Matrices: Program and Output
#include <stdio.h>
int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
printf("Enter the number of rows and columns of first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
scanf("%d", &first[c][d]);
printf("Enter the number of rows and columns of second matrix\n");
scanf("%d%d", &p, &q);
if ( n != p )
printf("Matrices with entered orders can't be multiplied with each other.\n");
else
{
printf("Enter the elements of second matrix\n");
for ( c = 0 ; c < p ; c++ )
for ( d = 0 ; d < q ; d++ )
scanf("%d", &second[c][d]);
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
{
for ( k = 0 ; k < p ; k++ )
{
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
printf("Product of entered matrices:-\n");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
printf("%d\t", multiply[c][d]);
printf("\n");
}
}
return 0;
}
Output:
Enter the number of rows and columns of first matrix
3
3
Enter the elements of first matrix
1 2 0
0 1 1
2 0 1
Enter the number of rows and columns of second matrix
3
3
Enter the elements of second matrix
1 1 2
2 1 1
1 2 1
Product of entered matrices:-
5 3 4
3 3 2
3 4 5Transpose of Matrix
#include <stdio.h>
int main()
{
int m, n, c, d, matrix[10][10], transpose[10][10];
printf("Enter the number of rows and columns of matrix ");
scanf("%d%d",&m,&n);
printf("Enter the elements of matrix \n");
for( c = 0 ; c < m ; c++ )
{
for( d = 0 ; d < n ; d++ )
{
scanf("%d",&matrix[c][d]);
}
}
for( c = 0 ; c < m ; c++ )
{
for( d = 0 ; d < n ; d++ )
{
transpose[d][c] = matrix[c][d];
}
}
printf("Transpose of entered matrix :-\n");
for( c = 0 ; c < n ; c++ )
{
for( d = 0 ; d < m ; d++ )
{
printf("%d\t",transpose[c][d]);
}
printf("\n");
}
return 0;
}
Output:
Enter the number of rows and columns of matrix
2
3
Enter the elements of matrix
1 2 3
4 5 6
Transpose of entered matrix:-
1 4
2 5
3 6Null Terminated Strings as an Array of Characters
C Standard Library implements a powerful string class, which is very useful to handle and manipulate strings of characters.
However, because strings are in fact sequences of characters, we can represent them also as plain arrays of char elements.
For example, the following array:
char jenny [20];
It's an array that can store up to 20 elements of type char.
Figure (described): a row of 20 empty boxes labelled jenny.
String as an Array of Characters
In the array, we can store sequences of characters up to 20 characters long. But we can also store shorter sequences.
For example, jenny could store at some point in a program either the sequence "Hello" or the sequence "Merry Christmas", since both are shorter than 20 characters.
Therefore, since the array of characters can store shorter sequences than its total length, a special character is used to signal the end of the valid sequence:
The null character, whose literal constant can be written as '\0' (backslash, zero).
Our array of 20 elements of type char, called jenny, can be represented storing the characters sequences "Hello" and "Merry Christmas" as:
Figure (described): the characters H e l l o followed by the null character \0 occupy the first six boxes of jenny, and M e r r y, a space, c h r i s t m a s followed by \0 occupy the first sixteen boxes of the same array; the remaining boxes are unused.
Initialization of Null-terminated Character Sequences
Because arrays of characters are ordinary arrays they follow all their same rules.
For example:
If we want to initialize an array of characters with some predetermined sequence of characters we can do it just like any other array:
char myword [ ] = {'H', 'e', 'l', 'l', 'o', '\0'};
char myword [ ] = "Hello";Representation Sparse Matrices
A matrix is a two-dimensional data object made of m rows and n columns, therefore having m, n values.
When m=n, we call it a square matrix.
The most natural representation is to use two-dimensional array A [m][n] and access the element of i row and j column as A[i][j].
If a large number of elements of the matrix are zero elements, then it is called a sparse matrix.
Representing a sparse matrix by using a two-dimensional array leads to the wastage of a substantial amount of space.
Therefore, an alternative representation must be used for sparse matrices.
One such representation is to store only non- zero elements along with their row positions and column positions.
That means representing every non-zero element by using triples (i, j, value), where i is a row position and j is a column position, and store these triples in a linear list.
It is possible to arrange these triples in the increasing order of row indices, and for the same row index in the increasing order of column indices.
Each triple (i, j, value) can be represented by using a node having four fields as shown in the following:
Figure (described): a node with four fields: row, col, value and link.
A sparse matrix can be represented using a list of such nodes, one per non-zero element of the matrix.
For example, consider the sparse matrix shown in figure.
| 0 | 2 | 0 | 0 | 2 | 0 |
| 0 | 0 | 0 | 1 | 0 | 5 |
| 0 | 0 | 4 | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 |
A sparse matrix
This matrix can be represented using the linked list shown in figure.
(row, col, value) nodes in a linked list ending in NULL: (0, 1, 2) -> (0, 4, 2) -> (1, 3, 1) -> (1, 5, 5) -> (2, 2, 4) -> NULL
Example: Sparse matrix
/*
* C program to determine if a given matrix is a sparse matrix.
* Sparse matrix has more zero elements than nonzero elements.
*/
#include <stdio.h>
void main ()
{
static int array[10][10];
int i, j, m, n;
int counter = 0;
printf("Enter the order of the matrix \n");
scanf("%d %d", &m, &n);
printf("Enter the co-efficients of the matrix \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
if (array[i][j] == 0)
{
++counter;
}
}
}
if (counter > ((m * n) / 2))
{
printf("The given matrix is sparse matrix \n");
}
else
printf("The given matrix is not a sparse matrix \n");
printf("There are %d number of zeros", counter);
}
Output:
Enter the order of the matrix
3 3
Enter the co-efficients of the matrix
10 20 30
5 10 15
3 6 9
The given matrix is not a sparse matrix
There are 0 number of zeros
Enter the order of the matrix
3 3
Enter the co-efficients of the matrix
5 0 0
0 0 5
0 5 0
The given matrix is sparse matrix
There are 6 number of zerosStandard Library String Functions - Strings
C implements the string data structure using arrays of type char.
The users have already used the string extensively.
printf("This program is terminated!\n");
#define ERR_Message "Error!!"Since string is an array, the declaration of a string is the same as declaring a char array.
char string_var[30]; char string_var[20] = "Initial value";
Memory Storage for a String
The string is always ended with a null character '\0'.
The characters after the null character are ignored.
E.g., char str[20] = "Initial value";
Figure (described): the characters I n i t i a l, a space, v a l u e stored in consecutive positions starting at index [0].
Arrays of Strings
An array of strings is a two-dimensional array of characters in which each row is one string.
char names[People][Length];
char month[5][10] = {"January", "February", "March", "April", "May"};Input/Output of a String
The placeholder %s is used to represent string arguments in printf and scanf.
printf("Topic: %s\n", string_var);The string can be right-justified by placing a positive number in the placeholder.
printf("%8s", str);The string can be left-justified by placing a negative number in the placeholder.
printf("%-8s", str);An Example of Manipulating String with scanf and printf
#include <stdio.h>
#define STRING_LEN 10
int main(void)
{
char dept[STRING_LEN];
int course_num;
char days[STRING_LEN];
int time;
printf("Enter department code, course number, days and ");
printf("time like this:\n> COSC 2060 MWF 1410\n> ");
scanf("%s%d%s%d", dept, &course_num, days, &time);
printf("%s %d meets %s at %d\n", dept, course_num, days, time);
return(0);
}Execution of scanf ("%s", dept);
Whenever encountering a white space, the scanning stops and scanf places the null character at the end of the string.
E.g., if the user types "MATH 1234 TR 1800," the string "MATH" along with '\0' is stored into dept.
Figure (described): dept[0] to dept[3] hold M A T H and dept[4] holds the null character; the remaining positions are undefined. Leading white space is skipped and scanning stops at the first white space after MATH.
String Library Functions
The string can not be copied by the assignment operator '='.
E.g., "str = "Test String"" is not valid.
C provides string manipulating functions in the "string.h" library.
The complete list of these functions can be found in Appendix B of the textbook.
Some String Functions from String.h
| Function | Purpose | Example |
|---|---|---|
| strcpy | Makes a copy of a string | strcpy(s1, "Hi"); |
| strcat | Appends a string to the end of another string | strcat(s1, "more"); |
| strcmp | Compare two strings alphabetically | strcmp(s1, "Hu"); |
| strlen | Returns the number of characters in a string | strlen("Hi") returns 2. |
| strtok | Breaks a string into tokens by delimiters. | strtok("Hi, Chao", " ,"); |
Functions strcpy and strncpy
Function strcpy copies the string in the second argument into the first argument.
E.g., strcpy(dest, "test string");
The null character is appended at the end automatically.
If source string is longer than the destination string, the overflow characters may occupy the memory space used by other variables.
Function strncpy copies the string by specifying the number of characters to copy.
The users have to place the null character manually.
E.g., strncpy(dest, "test string", 6); dest[6] = '\0';
If source string is longer than the destination string, the overflow characters are discarded automatically.
Extracting Substring of a String
We can use strncpy to extract substring of one string.
E.g., strncpy(result, s1, 9);
E.g., strncpy(result, &s1[5], 2);
Functions strcat and strlen
Functions strcat and strncat concatenate the first string argument with the second string argument.
strcat(dest, "more.."); strncat(dest, "more..", 3);
Function strlen is often used to check the length of a string (i.e., the number of characters before the first null character).
E.g., char dest[6] = "Hello";
strncat(dest, "more", 5-strlen(dest)); dest[5] = '\0';
Distinction Between Characters and Strings
The representation of a char (e.g., 'Q') and a string (e.g., "Q") is essentially different.
A string is an array of characters ended with the null character.
Figure (described): the character 'Q' occupies one byte, while the string "Q" occupies two bytes: 'Q' followed by the null character.
String Comparison (1/2)
Suppose there are two strings, str1 and str2.
The condition str1 < str2 compare the initial memory address of str1 and of str2.
The comparison between two strings is done by comparing each corresponding character in them.
The characters are compared against the ASCII table.
"thrill" < "throw" since 'i' < 'o';
"joy" < "joyous";
The standard string comparison uses the strcmp and strncmp functions.
String Comparison (2/2)
| Relationship | Returned Value | Example |
|---|---|---|
| str1 < str2 | Negative | "Hello" < "Hi" |
| str1 = str2 | 0 | "Hi" = "Hi" |
| str1 > str2 | Positive | "Hi" > "Hello" |
E.g., we can check if two strings are the same by
if(strcmp(str1, str2) != 0) printf("The two strings are different!");Input/Output of Characters and Strings
The stdio library provides getchar function which gets the next character from the standard input.
"ch = getchar();" is the same as "scanf("%c", &ch);"
Similar functions are putchar, gets, puts.
For IO from/to the file, the stdio library also provides corresponding functions.
getc: reads a character from a file.
Similar functions are putc, fgets, fputs.
Free study material for Computer Science
CBSE Computer Science Study Material: Programming in C
Core Study Kit: Computer Science Programming in C
Review targeted study resources for Programming in C tailored for learners. Utilizing these structured notes and quick-revision tools ensures complete alignment with current CBSE evaluation standards.
NCERT-Aligned Solutions for Programming in C
Designed around the official curriculum, these study guides guarantee standard compliance. Reviewing step-by-step solutions clarifies complex sub-topics within Programming in C and demystifies standard marking schemes for Computer Science evaluations.
Next Steps in Your Exam Preparation
Maximize your grade potential by utilizing Computer Science practice papers in tandem with these structured notes. All printable assignments and interactive mock tests on our platform are updated for the 2026 session and available free of charge.
FAQs
The latest 2026-27 advanced study resources for Computer Science are available for free on StudiesToday.com which includes NCERT Exemplars, high-order thinking skills (HOTS) questions, and deep-dive concept summaries.
Our exhaustive Computer Science package includes chapter wise revision notes, solved practice sheets, important formulas and Concept Maps to help in better understanding of all topics.
Yes. For , our resources have been developed to help you get better marks in school exams and also build fundamental strength needed for entrance tests including Competency Based learning.
in , students should use Active Recall method, read the concept summary, then solve the Important Questions section without looking at the answers and then check your answers.
All Computer Science study materials are provided in mobile-friendly PDF. You can download and save them on your device.
Yes, our team has ensured that all Computer Science materials for are strictly aligned with the National Education Policy (NEP) 2020 and the latest 2026-27 syllabus.