Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 7 of 7

Practice & Quiz - Arrays & Strings

Consolidate your C array and string skills - 1D/2D arrays, null termination, string.h safety, and buffer overflow prevention.

10 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Practical Exercise

Practical Exercise
/* strbuilder.c - safe string operations with snprintf */
#include <stdio.h>
#include <string.h>

#define BUF 128

int main(void) {
    char out[BUF];

    /* snprintf: always null-terminates, returns chars that WOULD be written */
    int n = snprintf(out, sizeof(out),
        "Name: %-8s | Age: %d | Score: %.2f", "Alice", 28, 98.5);
    printf("%s
", out);
    if (n >= (int)sizeof(out)) printf("WARNING: truncated!
");

    /* strrchr: find filename in path */
    const char *path = "/home/alice/projects/main.c";
    const char *fname = strrchr(path, '/');
    printf("File: %s
", fname ? fname + 1 : path);

    /* strtok on a COPY (never on a literal!) */
    char line[] = "GET /index.html HTTP/1.1";
    printf("Method : %s
", strtok(line, " "));
    printf("URL    : %s
", strtok(NULL, " "));
    printf("Proto  : %s
", strtok(NULL, " "));

    /* Safe strncpy pattern */
    char dst[16];
    strncpy(dst, "Hello, World!", sizeof(dst) - 1);
    dst[sizeof(dst) - 1] = '';   /* always null-terminate manually */
    printf("Safe copy: %s
", dst);

    return 0;
}
/* gcc -Wall -Wextra -std=c99 strbuilder.c -o strbuilder && ./strbuilder */

Module Quiz

Module Quiz
Question 1 of 5

For `char arr[] = "hello"`, what does `sizeof(arr)` return vs `strlen(arr)`?

Tip

Tip

Practice Practice Quiz Arrays Strings in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Process virtual address space organizes code, static variables, heap, and call stack

Practice Task

Note

Practice Task - (1) Write a working example of Practice Quiz Arrays Strings from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Common Mistake

Warning

A common mistake with Practice Quiz Arrays Strings is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.

Topics in This Module

0/7 done