Unit 3 · Lesson 618 minAcademic review pending

A string is an array with one extra rule

C has no string type. A string is a char array that ends with a hidden zero byte, and almost every string bug comes from that byte going missing.

Choose explanation

After this lesson

You should be able to

  • Declare a string and explain the role of the null terminator.
  • Size a char array correctly for the text it must hold.
  • Read a whole line, including spaces, instead of a single word.
01

The invisible last character

char name[] = "Kavya"; looks like five characters but occupies six. C appends '\0', the null character, so that functions can find the end. Nothing records the length — every string function walks forward until it meets that zero.

So a char array of size 10 holds at most 9 letters. Forget the terminator's byte and a function will walk past the end of your array looking for a zero that is not there.

Five letters, six bytes
char name[] = "Kavya";   /* size 6: K a v y a \0        */
char town[20] = "Warangal";
char blank[20] = "";      /* empty string: just \0       */

printf("%s\n", name);    /* %s prints up to the \0      */
printf("%c\n", name[0]); /* K — one character           */
02

Reading a word versus reading a line

scanf("%s", name) stops at the first space, so entering "Kavya Reddy" stores only "Kavya". For a full line use fgets, which reads until the newline or until the buffer is full — and unlike plain scanf it will not overrun your array.

fgets keeps the newline character if it fits, which surprises people comparing strings. Strip it by replacing it with '\0'.

Whole-line input, safely bounded
#include <stdio.h>
#include <string.h>

char name[50];

printf("Full name: ");
fgets(name, sizeof name, stdin);

/* remove the trailing newline, if fgets kept one */
name[strcspn(name, "\n")] = '\0';

printf("Hello, %s!\n", name);
03

Walking a string yourself

Because the end is marked rather than counted, you can loop until you meet the terminator: for (int i = 0; text[i] != '\0'; i++). That is exactly what strlen does internally, which is also why calling strlen inside a loop condition re-walks the whole string on every pass.

Compute the length once before the loop. On a long string that turns a slow program into a fast one, without changing what it computes.

Try it yourself

Read a full name including spaces and report its length in characters, ignoring the spaces.

Need a hint?

Loop until the terminator and count only the characters that are not spaces.

Check the worked solution

sizeof name inside fgets is what makes the read safe — it can never write more bytes than the array holds. The loop stops at '\0' rather than at a length, which is the string convention; note that strlen is called once into a variable rather than in the loop condition, so the string is walked twice in total instead of once per character.

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

int main(void)
{
    char name[50];
    int  letters = 0;

    printf("Full name: ");
    fgets(name, sizeof name, stdin);
    name[strcspn(name, "\n")] = '\0';

    for (int i = 0; name[i] != '\0'; i++)
        if (name[i] != ' ')
            letters++;

    printf("\"%s\" has %d letters and %zu characters\n",
           name, letters, strlen(name));

    return 0;
}

Quick check

How many bytes does char s[] = "Hyd"; occupy?

Select an answer to check your thinking.

Why this lesson exists

Syllabus mapping

String Basics · Longer Strings: Concatenation and Whole-Line Input

Maps to course outcome CO5.