Skip to content
Strings
step 1/5

Reading — step 1 of 5

Learn

~3 min readArrays and Strings

Strings

C does not have a string type. Read that again, because it explains everything strange about C strings: a "string" is an array of char ending with a zero byte, plus fifty years of library functions that all trust that convention completely. The zero byte — '\0', the null terminator — is the entire system.

The convention

char name[] = "Ada";

What memory actually holds: 'A' 'd' 'a' '\0'four bytes for a three-letter word. Every string function walks bytes until it hits '\0'; that's how they know where the string ends, because (as you know from Arrays) nothing else stores a length.

char word[20] = "hi";    /* room for 19 real chars + the terminator */

Sizing rule: an array of N chars holds at most N−1 characters of text. Forget the terminator's seat and functions walk past your data into the void — the string version of last lesson's overflow.

Reading and printing

char word[64];
scanf("%63s", word);      /* reads one whitespace-delimited token */
printf("%s\n", word);

Details that matter: %s with scanf reads a single word (stops at any whitespace — for grader input of one token per read, exactly right). Note there's no & before word — an array's name already is its address (the pointers chapter makes this rigorous). And the 63 is a width limit: at most 63 chars read, leaving room for '\0' in a 64-byte buffer. Unbounded %s into a fixed buffer is the historical security hole; the width costs three keystrokes.

The library: string.h

#include <string.h>

strlen("hello")           /* 5 — counts up to, not including, '\0' */
strcmp(a, b)              /* 0 if EQUAL; <0 / >0 for ordering */
strcpy(dst, src);         /* copy src into dst (dst must have room!) */
strcat(dst, src);         /* append src onto dst (ditto) */

Two of these carry famous traps:

  • strcmp returns 0 for equal strings — so the equality test is strcmp(a, b) == 0. Writing if (strcmp(a, b)) means "if different," and if (a == b) doesn't compare text at all — it compares addresses (are these the same bytes in memory, not the same letters). Every C programmer has burned an afternoon on one of these; you now get the afternoon back.
  • strcpy/strcat check nothing — destination too small is an overflow, quietly. Real codebases reach for bounded variants (snprintf being the modern favorite); this course's exercises size buffers generously so you can learn the concepts before the paranoia.

Walking a string by hand

The terminator makes manual iteration natural — loop until the zero byte:

int len = 0;
while (word[len] != '\0') {
    len++;
}

That is strlen, implemented in three lines. Characters are just small integers, so transformations are arithmetic: word[i] - 'a' + 'A' uppercases a lowercase letter (<ctype.h>'s toupper() does it portably).

Your exercise: String Length

Read a word, print its length — and the assignment is to feel the convention, so consider computing it both ways: strlen(word) for the one-liner, then the manual while loop above, and confirm they agree. When they do, you've verified the null terminator is really there, really at the end, and really the thing every string function navigates by. That mental model — bytes, then a zero — is the whole of C strings; the rest is library.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…