C Arrays and Strings
Work with collections of data using arrays, strings, and character manipulation.
Introduction to Arrays
An array is a collection of elements of the same data type stored in contiguous memory locations. Key characteristics: - All elements are of the same type - Elements are stored in consecutive memory locations - Each element can be accessed using an index - Array index starts from 0 - Arrays have a fixed size (in C) Declaration syntax: ```c data_type array_name[size]; // Examples int numbers[10]; // Array of 10 integers char name[50]; // Array of 50 characters float grades[5]; // Array of 5 floats ```
Array Initialization
Arrays can be initialized in several ways:
1. Declaration with initialization:
```c
int arr[] = {1, 2, 3, 4, 5}; // Size determined automatically
int numbers[5] = {10, 20, 30, 40, 50}; // Explicit size
```
2. Partial initialization:
```c
int arr[5] = {1, 2}; // First two elements initialized, rest are 0
```
3. Zero initialization:
```c
int arr[5] = {0}; // All elements initialized to 0
```
4. Runtime initialization:
```c
int arr[5];
for (int i = 0; i < 5; i++) {
arr[i] = i * 2;
}
```Accessing Array Elements
Array elements are accessed using square brackets with an index:
Syntax: `array_name[index]`
Important points:
- Index starts from 0
- Last valid index is (size - 1)
- Accessing out-of-bounds elements causes undefined behavior
- Arrays are zero-indexed
Example:
```c
int arr[5] = {10, 20, 30, 40, 50};
printf("%d\n", arr[0]); // 10 (first element)
printf("%d\n", arr[2]); // 30 (third element)
printf("%d\n", arr[4]); // 50 (last element)
// Modify elements
arr[1] = 25; // Changes second element to 25
```Strings in C
Strings in C are arrays of characters terminated by a null character ('\0').
String declaration:
```c
char str[50]; // Can hold up to 49 characters + null terminator
```
String initialization:
```c
char str1[] = "Hello"; // Size determined automatically
char str2[10] = "World"; // Explicit size
char str3[] = {'H', 'i', '\0'}; // Character by character
```
String input/output:
```c
char name[50];
printf("Enter your name: ");
scanf("%s", name); // Reads until whitespace
printf("Hello, %s!\n", name);
```Multi-dimensional Arrays
C supports multi-dimensional arrays (arrays of arrays).
2D Array declaration:
```c
int matrix[3][4]; // 3 rows, 4 columns
```
2D Array initialization:
```c
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
```
Accessing 2D array elements:
```c
printf("%d\n", matrix[0][0]); // First row, first column
printf("%d\n", matrix[1][2]); // Second row, third column
```Practice Examples
Try these examples in our online C compiler to reinforce your learning:
Basic Array Operations
Demonstrates basic array declaration, initialization, and traversal.
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
printf("Array elements:\n");
for (int i = 0; i < 5; i++) {
printf("arr[%d] = %d\n", i, arr[i]);
}
// Calculate sum
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += arr[i];
}
printf("\nSum of all elements: %d\n", sum);
return 0;
}String Manipulation
Shows basic string operations using standard library functions.
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello";
char str2[50] = "World";
char result[100];
printf("String 1: %s\n", str1);
printf("String 2: %s\n", str2);
printf("Length of str1: %zu\n", strlen(str1));
// Concatenate strings
strcpy(result, str1);
strcat(result, " ");
strcat(result, str2);
printf("Concatenated: %s\n", result);
return 0;
}2D Array - Matrix Operations
Demonstrates 2D array operations and matrix manipulation.
#include <stdio.h>
int main() {
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
printf("Matrix:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
// Calculate diagonal sum
int diagonalSum = 0;
for (int i = 0; i < 3; i++) {
diagonalSum += matrix[i][i];
}
printf("\nDiagonal sum: %d\n", diagonalSum);
return 0;
}Ready for the Next Step?
Continue your C programming journey with our next tutorial.