C Programming Tutorial 0/65 lessons ~6 min read Lesson 26

    Strings

    Text in C is a char array terminated by a zero byte.

    Course progress0%
    Focus
    10 guided sections
    Practice signal
    Examples included
    Career prep
    Foundation builder

    Introduction

    Text in C is a char array terminated by a zero byte. Length is not stored — algorithms scan until they see that sentinel.

    Understanding the topic

    String literal char s[] = "text"; — compiler adds '\0'.

    Character access s[i] like an array; stop at '\0' when iterating.

    Input caution Use fgets, not unbounded scanf for strings.

    • String literal — char s[] = "text"; — compiler adds '\0'.
    • Character access — s[i] like an array; stop at '\0' when iterating.
    • Input caution — Use fgets, not unbounded scanf for strings.

    Step-by-step explanation

    1. String literal — char s[] = "text"; — compiler adds '\0'.
    2. Character access — s[i] like an array; stop at '\0' when iterating.
    3. Input caution — Use fgets, not unbounded scanf for strings.

    Syntax reference

    Syntax reference:

    c
    char str[size] = "text";

    Informative example

    Example program:

    c
    #include <stdio.h>
    int main(void) {
    char word[] = "C strings";
    printf("%s\n", word);
    return 0;
    }

    Output

    C strings

    Execution workflow

    1Strings — step by step
    1 / 3

    String literal

    char s[] = "text"; — compiler adds '\0'.

    Best practices

    • Enable warnings: gcc -Wall -Wextra -std=c11 source.c -o app
    • Give every variable a defined value before it is read.
    • Stay inside array bounds — C will not stop you from over-running a buffer.

    Common mistakes

    • Reading uninitialized storage — behavior is undefined.
    • Dismissing compiler warnings instead of fixing root causes.
    • Ignoring NULL returns from malloc, fopen, and similar APIs.

    Hands-on exercise

    Practice problems:

    • Print string char by char
    • Count string length manually

    Summary

    Strings in C — char buffers ending with a null byte.

    Ready to mark this lesson complete?Track your journey across the entire course.