Nearby lessons

63 of 124

C - String Introduction

C has no built-in string type — a string in C is an array of characters ending with the null character '\0'. Learn what that means, why the terminator matters, and how strings differ from character arrays.

There Is No string Type

Languages like Java and Python give you a String type. C does not. In C a string is simply an array of char with one rule attached: the last useful character must be followed by the null character '\0'.

Example01
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 /* Both lines create the same 6-byte string */
6 char a[] = "Hello";
7 char b[] = {'H', 'e', 'l', 'l', 'o', '\0'};
8 
9 printf("a = %s\n", a);
10 printf("b = %s\n", b);
11 printf("sizeof(a) = %zu, sizeof(b) = %zu\n", sizeof(a), sizeof(b));
12 return 0;
13}
Output
a = Hello
b = Hello
sizeof(a) = 6, sizeof(b) = 6

The Null Terminator

'\0' is the character whose numeric value is zero. It is the sentinel that tells every string function "stop here".

Index012345
CharacterHello\0
ASCII value721011081081110
In simple words: C does not store a string's length anywhere. It finds the end by scanning forward until it hits a zero byte. That single design decision explains almost everything about how C strings behave.

Three Different Zeros

These look similar and are constantly confused. They are not the same:

Example03
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 char nullChar = '\0'; /* the terminator, value 0 */
6 char digitZero = '0'; /* the character 0, value 48 */
7 char space = ' '; /* a space, value 32 */
8 
9 printf("'\\0' has value %d\n", nullChar);
10 printf("'0' has value %d\n", digitZero);
11 printf("' ' has value %d\n", space);
12 return 0;
13}
Output
'\0' has value 0
'0'  has value 48
' '  has value 32

Why the Extra Byte Matters

An array of exactly 5 chars cannot hold the 5-letter string "Hello" — there is no room for the terminator:

Example04
CCode Cell
1#include <stdio.h>
2#include <string.h>
3 
4int main()
5{
6 char good[6] = "Hello"; /* 5 letters + \0 = 6 - correct */
7 
8 printf("good : %s\n", good);
9 printf("strlen : %zu (letters only)\n", strlen(good));
10 printf("sizeof : %zu (letters + terminator)\n", sizeof(good));
11 return 0;
12}
Output
good   : Hello
strlen : 5  (letters only)
sizeof : 6  (letters + terminator)

strlen vs sizeof

This pair is the most common string interview question, and the distinction is simple:

strlen(s)sizeof(s)
MeasuresCharacters before \0Bytes reserved for the array
Counts \0?NoYes
When decidedAt runtime, by scanningAt compile time
For char s[20]="Hi"220
Example05
CCode Cell
1#include <stdio.h>
2#include <string.h>
3 
4int main()
5{
6 char s[20] = "Hi";
7 
8 printf("strlen : %zu\n", strlen(s)); /* 2 */
9 printf("sizeof : %zu\n", sizeof(s)); /* 20 */
10 return 0;
11}
Output
strlen : 2
sizeof : 20

A Character Array Without \0

Omit the terminator and you no longer have a string — you have a character array. printf("%s") will run past the end and print whatever follows:

Example06
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 char notAString[5] = {'H', 'e', 'l', 'l', 'o'}; /* no \0 */
6 int i;
7 
8 /* Safe: print character by character with a known count */
9 printf("As chars: ");
10 for (i = 0; i < 5; i++) printf("%c", notAString[i]);
11 
12 /* Unsafe: %s keeps reading until it finds a random zero byte */
13 printf("\nAs %%s : %s\n", notAString);
14 return 0;
15}
Output
As chars: Hello
As %s   : Hello╠╠╠╠▌

Strings Are Not Assignable

Because a string is an array, the array rules apply: you cannot assign or compare with = and ==:

Example07
CCode Cell
1#include <stdio.h>
2#include <string.h>
3 
4int main()
5{
6 char a[20] = "Hello";
7 char b[20];
8 
9 /* b = a; INVALID - arrays are not assignable */
10 /* if (a == b) compares ADDRESSES, not text */
11 
12 strcpy(b, a); /* copy - correct way */
13 printf("b = %s\n", b);
14 
15 if (strcmp(a, b) == 0) /* compare - correct way */
16 printf("a and b hold the same text\n");
17 return 0;
18}
Output
b = Hello
a and b hold the same text

Common Mistakes

  • Sizing the array exactlychar s[5] = "Hello"; leaves no room for \0.
  • Confusing 'A' and "A" — the first is 1 byte, the second is 2 bytes (the letter plus \0).
  • Comparing with == — that compares pointers. Use strcmp.
  • Assuming strlen counts the terminator — it does not.
  • Overwriting the \0 — one stray write and the string loses its end marker.
Every buffer-overflow bug in C history starts here. Because the length is not stored, functions like strcpy simply copy until they see \0 — even if that means running far past the end of your array. Always reserve space for the terminator.
📝 Key Takeaways
  • C has no string data type; a string is char[] ending in \0.
  • The null character \0 marks where the string ends.
  • A string of n characters needs n + 1 bytes.
  • \0 is the character with value 0 — not the digit 0 and not a space.
  • Without \0, printf keeps reading past the end into garbage.

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4