Table of Contents

IronForgeCyber

Courses

Resources

Blog

Log In



IronForgeCyber

AboutPrivacyTerms

© 2026 IronForgeCyber. All rights reserved.

  1. courses
  2. c-programming
  3. strings
6 min read

Strings

C has no built-in string type the way Python or JavaScript does. A string in C is just a char array with one convention attached: it ends with a \0 byte — the null terminator, a byte whose value is zero. Almost everything that feels strange about C strings follows from that single rule, so we start there.

Strings Are Char Arrays

When you write a string literal in double quotes, C stores each character in order and adds the \0 for you. So "hello" is not five bytes — it is six:

0h 1e 2l 3l 4o 5\0 5 characters, 6 bytes — the \0 terminator is counted

That terminator is how every string function knows where the text stops: there is no stored length, just "keep going until you hit a zero byte." You usually create a string as a char array, letting the compiler size it:

c
char s[] = "hello"; // a 6-byte array you can modify

The distinction between length and size is the root of a whole class of bugs, so let's see it in the debugger. strlen walks to the terminator and counts the characters before it; sizeof reports the whole array including the \0:

str.c
1#include <stdio.h>
2#include <string.h>
3
4int main(void)
5
gdb
$ gcc -g str.c -o strgef➤ set output-radix 10gef➤ break 8Breakpoint 1 at 0x115e: file str.c, line 8.gef➤ runBreakpoint 1, main () at str.c:8gef➤ p s$1 = "hello"gef➤ p sizeof(s)$2 = 6gef➤ p (int) strlen(s)$3 = 5gef➤ p s[5]$4 = 0 '\000'gef➤ x/6xb s0x7fffffffe1da: 0x68    0x65    0x6c    0x6c    0x6f    0x00

strlen(s) is 5, sizeof(s) is 6, and s[5] is the 0 terminator — you can even see it in the raw bytes, where 0x68 0x65 0x6c 0x6c 0x6f (the ASCII for hello) is capped by 0x00. If that terminator ever goes missing, string functions keep reading into whatever memory follows, which is both a common crash and a common vulnerability.

The string.h Toolkit

The <string.h> header gives you the standard tools, and every one of them depends on that \0:

  • strlen(s) — length up to the terminator.
  • strcpy(dst, src) — copy src into dst, terminator included.
  • strcat(dst, src) — append src onto the end of dst.
  • strcmp(a, b) — compare; returns 0 when the strings are equal.
  • memcpy(dst, src, n) — copy exactly n bytes (works on any data, not just strings).
c
1char name[16];
2strcpy(name, "forge");
3if (strcmp(name,
strcpy will walk off the end of your buffer
strcpy copies until it hits the source's \0 — it has no idea how big dst is. If the source is longer than the destination, it writes straight past the end, corrupting memory. This is one of the oldest buffer-overflow bugs in existence. Use snprintf, or the length-limited strncpy, and always make sure your buffer is big enough — including room for the terminator.

Tokenizing with strtok

A very common job is splitting a string into pieces — a line of comma- or space-separated fields, say. strtok does this. The first call takes the string and a set of delimiter characters; each later call passes NULL to continue where it left off, returning the next token or NULL when there are none:

tok.c
1#include <stdio.h>
2#include <string.h>
3
4int main(void)
$ gcc tok.c -o tok$ ./tok[one][two][three]

One thing to know: strtok works by writing \0 bytes over the delimiters, so it modifies the string in place. Never hand it a string literal (char *s = "..."), which is read-only — give it a writable char array like the one above.

Converting Strings to Numbers

Input arrives as text, so you constantly need to turn "42" into the number 42. The quick tool is atoi, which returns the integer value of a string — but it gives you no way to tell a genuine 0 from unparseable input:

c
int n = atoi("42"); // 42

When you need to detect bad input, reach for strtol, which writes back a pointer to where it stopped parsing so you can check whether anything was actually a number. We use it throughout the challenges — it is covered in the ASCII challenges introduction.

With arrays and strings in hand, the last building block is the one that lets you bundle different types together: the struct.

{
6 char s[] = "hello";
7
8 printf("%zu\n", strlen(s));
9 return 0;
10}
"forge"
)
==
0
)
4 printf("match\n");
5
{
6 char line[] = "one two three";
7 char *tok = strtok(line, " ");
8
9 while (tok != NULL)
10 {
11 printf("[%s]\n", tok);
12 tok = strtok(NULL, " ");
13 }
14 return 0;
15}