Semester-1 B.Tech.
-ESC 103 Introduction
to UNIX and C Programming
Dr. Prakash Kumar
SARALA BIRLA UNIVERSITY
Semester-1 B.Tech.-ESC 103 Introduction to UNIX and C Programming
Module 4
What is Array in C?
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.
C Array Initialization
// C Program to demonstrate array initialization
#include <stdio.h>
int main()
{
// array initialization using initialier list
int arr[5] = { 10, 20, 30, 40, 50 };
// array initialization using initializer list without
// specifying size
int arr1[] = { 1, 2, 3, 4, 5 };
// array initialization using for loop
int arr2[5];
for (int i = 0; i < 5; i++)
{
arr2[i] = i * 2;
}
return 0;
}
Display array elements:
#include <stdio.h>
int main()
{
// array declaration and initialization
int arr[5] = { 10, 20, 30, 40, 50 };
// traversing array using for loop
printf("Elements in Array: ");
for (int i = 0; i < 5; i++)
{
Semester-1 B.Tech.-ESC 103 Introduction to UNIX and C Programming
printf("%d ", arr[i]);
}
return 0;
}
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()
{
// creating array of character
char arr[6] = { 'S', 'i', 'n', 'g', 'h', '\0' };
// printing string
int i = 0;
while (arr[i])
{
printf("%c", arr[i++]);
}
return 0;
}
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];
// C Program to illustrate 2d array
#include <stdio.h>
int main()
{
// declaring and initializing 2d array
int arr[2][3] = { 10, 20, 30, 40, 50, 60 };
printf("2D Array:\n");
// printing 2d array
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
Semester-1 B.Tech.-ESC 103 Introduction to UNIX and C Programming
printf("%d ",arr[i][j]);
}
printf("\n");
}
return 0;
}
Output
2D Array:
10 20 30
40 50 60
Arrays and Pointers
// C Program to demonstrate the relation between arrays and
// pointers
#include <stdio.h>
int main()
{
int arr[5] = { 10, 20, 30, 40, 50 };
int* ptr = &arr[0];
printf("Address Stored in Array name: %p\nAddress of "
"1st Array Element: %p\n",
arr, &arr[0]);
// printing array elements using pointers
printf("Array elements using pointer: ");
for (int i = 0; i < 5; i++) {
printf("%d ", *ptr++);
}
return 0;
}
Passing an Array to a Function in C
An array is always passed as pointers to a function in C. Whenever we try to pass an array to a function,
it decays to the pointer and then passed as a pointer to the first element of an array.
// C Program to pass an array to a function
#include <stdio.h>
void printArray(int arr[])
{
printf("Size of Array in Functions: %d\n", sizeof(arr));
Semester-1 B.Tech.-ESC 103 Introduction to UNIX and C Programming
printf("Array Elements: ");
for (int i = 0; i < 5; i++) {
printf("%d ",arr[i]);
}
}
// driver code
int main()
{
int arr[5] = { 10, 20, 30, 40, 50 };
printf("Size of Array in main(): %d\n", sizeof(arr));
printArray(arr);
return 0;
}
C Program to find the largest number in the array.
// C Program to find the largest number in the array.
#include <stdio.h>
// function to return max value
int getMax(int* arr, int size)
{
int max = arr[0];
for (int i = 1; i < size; i++) {
if (max < arr[i]) {
max = arr[i];
}
}
return max;
}
// Driver code
int main()
{
int arr[10]= { 135, 165, 1, 16, 511, 65, 654, 654, 169, 4 };
printf("Largest Number in the Array: %d",getMax(arr, 10));
return 0;
}
Semester-1 B.Tech.-ESC 103 Introduction to UNIX and C Programming
Matrix Addition:
#include <stdio.h>
int main()
{
int a[2][2]= { 1, 2, 3, 4 };
int b[2][2]= { 1, 2, 3, 4 };
int c[2][2];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
c[i][j] = a[i][j] + b[i][j];
}
}
// Print the result
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
printf("%d ", c[i][j]);
}
printf("\n");
}
return 0;
}
Matrix Multiplication
#include <stdio.h>
int main()
{
int a[2][2]= { 1, 2, 3, 4 };
int b[2][2]= { 1, 2, 3, 4 };
int c[2][2];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
c[i][j]=0;
for(int k=0;k<2;k++)
c[i][j] = c[i][j]+a[i][k] * b[k][j];
Semester-1 B.Tech.-ESC 103 Introduction to UNIX and C Programming
}
}
// Print the result
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
printf("%d ", c[i][j]);
}
printf("\n");
}
return 0;
}
Strings in C
A String in C programming is a sequence of characters terminated with a null character ‘\0’. The C String
is stored as an array of characters. The difference between a character array and a C string is the string
is terminated with a unique character ‘\0’.
You can initialize strings in a number of ways.
char c[] = "abcd";
char c[50] = "abcd";
char c[] = {'a', 'b', 'c', 'd', '\0'};
char c[5] = {'a', 'b', 'c', 'd', '\0'};
#include <stdio.h>
int main()
{
char name[20];
printf("Enter name: ");
scanf("%s", name);
printf("Your name is %s.", name);
return 0;
}
Semester-1 B.Tech.-ESC 103 Introduction to UNIX and C Programming
#include <stdio.h>
int main()
{
char name[30];
printf("Enter name: ");
fgets(name, sizeof(name), stdin); // read string
printf("Name: ");
puts(name); // display string
return 0;
}
Basic string functions:
1. strlen(): This function returns the length of a string (the number of characters) excluding the null-
terminator.
2. strcpy(): Copies the content of one string to another.
3. strcat(): Concatenates (appends) one string to the end of another.
4. strcmp(): Compares two strings lexicographically. It returns an integer value indicating whether
the strings are equal, less than, or greater than each other.
5. strncpy(): Copies a specified number of characters from one string to another.
Program in C to find length of a string:
#include <stdio.h>
int main() {
char s[] = "Programming";
int i;
for (i = 0; s[i] != '\0'; ++i);
printf("Length of the string: %d", i);
return 0;
}
Program in C to check whether the string is palindrome or not
#include <stdio.h>
#include <string.h>
// Function to check if a string is a palindrome
int isPalindrome(const char *str) {
int left = 0; // Index for the start of the string
int right = strlen(str) - 1; // Index for the end of the string
Semester-1 B.Tech.-ESC 103 Introduction to UNIX and C Programming
while (left < right) {
if (str[left] != str[right]) {
return 0; // Not a palindrome
}
left++;
right--;
}
return 1; // Palindrome
}
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
if (isPalindrome(str)) {
printf("'%s' is a palindrome.\n", str);
} else {
printf("'%s' is not a palindrome.\n", str);
}
return 0;
}
______eof_Module4______