Module 7 of 13 · C Programming Fundamentals · Beginner

Arrays & Strings

Duration: 55 min

Arrays are collections of elements of the same type stored in contiguous memory. Strings are arrays of characters. Understanding arrays and strings is crucial for working with collections of data in C.

One-Dimensional Arrays

#include <stdio.h>

int main() {
    // Declaration and initialization
    int numbers[5] = {10, 20, 30, 40, 50};
    
    // Access elements (0-indexed)
    printf("First element: %d\n", numbers[0]);
    printf("Last element: %d\n", numbers[4]);
    
    // Modify elements
    numbers[2] = 35;
    
    // Loop through array
    for (int i = 0; i < 5; i++) {
        printf("%d ", numbers[i]);
    }
    printf("\n");
    
    return 0;
}

Two-Dimensional Arrays

#include <stdio.h>

int main() {
    // 3x3 matrix
    int matrix[3][3] = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };
    
    // Access elements
    printf("Element at [1][2]: %d\n", matrix[1][2]);  // 6
    
    // Print entire matrix
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
    
    return 0;
}

Strings (Character Arrays)

#include <stdio.h>
#include <string.h>

int main() {
    // String declaration
    char str1[20] = "Hello";
    char str2[] = "World";
    
    // Print strings
    printf("String 1: %s\n", str1);
    printf("String 2: %s\n", str2);
    
    // String length
    printf("Length of str1: %lu\n", strlen(str1));
    
    // String concatenation
    strcat(str1, " ");
    strcat(str1, str2);
    printf("Concatenated: %s\n", str1);
    
    // String comparison
    if (strcmp(str1, "Hello World") == 0) {
        printf("Strings are equal\n");
    }
    
    return 0;
}

String Functions

#include <stdio.h>
#include <string.h>

int main() {
    char str[50] = "Programming";
    
    // strlen: get length
    printf("Length: %lu\n", strlen(str));
    
    // strcpy: copy string
    char copy[50];
    strcpy(copy, str);
    printf("Copy: %s\n", copy);
    
    // strcat: concatenate
    strcat(str, " in C");
    printf("After concat: %s\n", str);
    
    // strcmp: compare strings
    if (strcmp(str, "Programming in C") == 0) {
        printf("Match!\n");
    }
    
    // strchr: find character
    char *pos = strchr(str, 'g');
    if (pos != NULL) {
        printf("Found 'g' at position: %ld\n", pos - str);
    }
    
    return 0;
}

Null Terminator

#include <stdio.h>

int main() {
    // Strings are terminated with '\0'
    char str[] = "Hello";
    
    // Manually print each character
    for (int i = 0; str[i] != '\0'; i++) {
        printf("%c ", str[i]);
    }
    printf("\n");
    
    // String without null terminator is dangerous
    char buffer[5];
    // strcpy(buffer, "Hello");  // DANGER: buffer overflow!
    
    // Safe approach
    strncpy(buffer, "Hi", 4);
    buffer[4] = '\0';  // Ensure null termination
    printf("Safe string: %s\n", buffer);
    
    return 0;
}

Array of Strings

#include <stdio.h>

int main() {
    // Array of strings
    char fruits[3][20] = {
        "Apple",
        "Banana",
        "Orange"
    };
    
    // Print all strings
    for (int i = 0; i < 3; i++) {
        printf("%s\n", fruits[i]);
    }
    
    return 0;
}

Practical Example: Sum of Array

#include <stdio.h>

int main() {
    int numbers[] = {10, 20, 30, 40, 50};
    int size = 5;
    int sum = 0;
    
    for (int i = 0; i < size; i++) {
        sum += numbers[i];
    }
    
    printf("Sum: %d\n", sum);
    printf("Average: %.2f\n", (float)sum / size);
    
    return 0;
}

Quiz 1: Array Indexing

❓ What is the index of the first element in an array?

Quiz 2: 2D Arrays

❓ How do you access element at row 1, column 2 in a 2D array?

Quiz 3: Strings

❓ What terminates a string in C?

Quiz 4: strlen Function

❓ What does strlen("Hello") return?

Quiz 5: Array Bounds

❓ What happens if you access an array element out of bounds?

← Previous Continue interactively → Next →

Related Courses