0% found this document useful (0 votes)
1 views

array

Arrays are used to store multiple values of the same type, allowing for efficient data management and indexed access. The document covers the declaration, initialization, and memory storage of both one-dimensional and two-dimensional arrays, as well as string handling in C, including various standard library functions for string manipulation. Key functions discussed include strlen, strcpy, strcat, and strcmp.

Uploaded by

technovision22co
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

array

Arrays are used to store multiple values of the same type, allowing for efficient data management and indexed access. The document covers the declaration, initialization, and memory storage of both one-dimensional and two-dimensional arrays, as well as string handling in C, including various standard library functions for string manipulation. Key functions discussed include strlen, strcpy, strcat, and strcmp.

Uploaded by

technovision22co
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

.

1 Concept and Need of Arrays


Why Do We Need Arrays?

 Variables store single values, but when we need to store multiple related values, we use arrays.
 Arrays help in efficient data management and allow indexed access to elements.

4.1.1 Declaration, Initialization, and Memory Storage

Declaration of an Array

Syntax:

data_type array_name[size];

Example:

int numbers[5]; // Declares an integer array of size 5

Initialization of an Array

int numbers[5] = {10, 20, 30, 40, 50}; // Declaring and initializing an array

OR

int numbers[] = {10, 20, 30, 40, 50}; // Size is determined automatically

Memory Storage of Arrays

 Array elements are stored in contiguous memory locations.


 Example:

numbers[0] -> Address: 1000


numbers[1] -> Address: 1004
numbers[2] -> Address: 1008 (if int takes 4 bytes)

Displaying Array Elements

#include <stdio.h>
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
return 0;
}
4.2 Two-Dimensional Arrays
A 2D array is an array of arrays, useful for matrices, tables, etc.

4.2.1 Initializing a 2D Array

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

4.2.2 Adding Elements to a 2D Array

We can take input from the user and store it in a 2D array:

#include <stdio.h>
int main() {
int matrix[2][2];

printf("Enter elements for 2x2 matrix:\n");


for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
scanf("%d", &matrix[i][j]);
}
}

return 0;
}

4.2.3 Displaying 2D Array Elements

#include <stdio.h>
int main() {
int matrix[2][2] = {{1, 2}, {3, 4}};

printf("Matrix Elements:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}

4.3 Introduction to Strings


A string is a sequence of characters ending with a null character (\0).

4.3.1 Declaration, Initialization, and Display of a String


char name[10] = "Hello"; // String initialization

OR

char name[] = "Hello"; // Compiler automatically adds '\0'

Displaying a String:

#include <stdio.h>
int main() {
char name[] = "Hello";
printf("%s", name);
return 0;
}

OR using gets():

char name[20];
printf("Enter your name: ");
gets(name);
printf("Your name is: %s", name);

4.4 Standard Library String Functions


These functions are defined in string.h.

4.4.1 strlen() - String Length

#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello";
printf("Length of string: %d", strlen(str));
return 0;
}

4.4.2 strcpy() - Copy String

#include <stdio.h>
#include <string.h>
int main() {
char src[] = "Hello";
char dest[10];
strcpy(dest, src);
printf("Copied String: %s", dest);
return 0;
}

4.4.3 strcat() - Concatenate Strings


#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello, ";
char str2[] = "World!";
strcat(str1, str2);
printf("Concatenated String: %s", str1);
return 0;
}

4.4.4 strcmp() - Compare Strings

#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Apple";
char str2[] = "Banana";
if (strcmp(str1, str2) == 0)
printf("Strings are equal");
else
printf("Strings are different");
return 0;
}

Summary
 Arrays store multiple values of the same type.
 1D and 2D Arrays are used to handle structured data efficiently.
 Strings are character arrays terminated by \0.
 String functions (strlen, strcpy, strcat, strcmp) help in string manipulation.

You might also like