Nearby lessons

65 of 124

C - String Initialization

All the ways to initialise a string in C — string literals, character lists, letting the compiler size the array, and using strcpy to assign text after declaration.

Method 1 — String Literal (Preferred)

Assign a quoted string at the point of declaration. The compiler appends '\0' automatically:

Example01
CCode Cell
1#include <stdio.h>
2#include <string.h>
3 
4int main()
5{
6 char a[] = "Hello"; /* size 6: compiler counts + \0 */
7 char b[10] = "Hello"; /* size 10: 4 spare bytes */
8 char c[6] = "Hello"; /* exact fit */
9 
10 printf("a: %s (sizeof %zu, strlen %zu)\n", a, sizeof(a), strlen(a));
11 printf("b: %s (sizeof %zu, strlen %zu)\n", b, sizeof(b), strlen(b));
12 printf("c: %s (sizeof %zu, strlen %zu)\n", c, sizeof(c), strlen(c));
13 return 0;
14}
Output
a: Hello (sizeof 6, strlen 5)
b: Hello (sizeof 10, strlen 5)
c: Hello (sizeof 6, strlen 5)

Method 2 — Character List

List each character individually. Here you must supply the terminator — the compiler will not add it:

Example02
CCode Cell
1#include <stdio.h>
2#include <string.h>
3 
4int main()
5{
6 char good[] = {'H', 'e', 'l', 'l', 'o', '\0'}; /* correct */
7 char bad[] = {'H', 'e', 'l', 'l', 'o'}; /* NOT a string */
8 
9 printf("good : %s (strlen %zu)\n", good, strlen(good));
10 printf("sizeof good: %zu, sizeof bad: %zu\n",
11 sizeof(good), sizeof(bad));
12 /* printf("%s", bad) would read past the end */
13 return 0;
14}
Output
good  : Hello (strlen 5)
sizeof good: 6, sizeof bad: 5

What Fills the Spare Bytes?

When you declare a larger array than the text needs, every remaining byte is set to '\0' — not left as garbage:

Example03
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 char s[10] = "Hi"; /* 'H','i', then eight \0 bytes */
6 int i;
7 
8 for (i = 0; i < 10; i++)
9 {
10 if (s[i] == '\0')
11 printf("s[%d] = \\0\n", i);
12 else
13 printf("s[%d] = %c\n", i, s[i]);
14 }
15 return 0;
16}
Output
s[0] = H
s[1] = i
s[2] = \0
s[3] = \0
s[4] = \0
s[5] = \0
s[6] = \0
s[7] = \0
s[8] = \0
s[9] = \0

Initialising an Empty String

An "empty" string is one whose first byte is already the terminator. All three forms below are equivalent:

Example04
CCode Cell
1#include <stdio.h>
2#include <string.h>
3 
4int main()
5{
6 char a[20] = ""; /* empty literal */
7 char b[20] = {'\0'}; /* explicit terminator */
8 char c[20] = {0}; /* zero-fill the whole array */
9 
10 printf("a: [%s] len %zu\n", a, strlen(a));
11 printf("b: [%s] len %zu\n", b, strlen(b));
12 printf("c: [%s] len %zu\n", c, strlen(c));
13 
14 strcpy(a, "Now filled");
15 printf("a: [%s] len %zu\n", a, strlen(a));
16 return 0;
17}
Output
a: [] len 0
b: [] len 0
c: [] len 0
a: [Now filled] len 10

After Declaration — Use strcpy

The literal form works only at declaration. Later on, = is illegal and you must copy:

Example05
CCode Cell
1#include <stdio.h>
2#include <string.h>
3 
4int main()
5{
6 char s[20];
7 
8 /* s = "Hello"; INVALID - arrays are not assignable */
9 
10 strcpy(s, "Hello"); /* correct */
11 printf("1: %s\n", s);
12 
13 strcpy(s, "Replaced"); /* overwrite */
14 printf("2: %s\n", s);
15 
16 strcat(s, " again"); /* append */
17 printf("3: %s\n", s);
18 
19 s[0] = 'D'; /* single character */
20 printf("4: %s\n", s);
21 return 0;
22}
Output
1: Hello
2: Replaced
3: Replaced again
4: Deplaced again

Building a String Character by Character

If you fill an array manually, remember to place the terminator yourself when you are done:

Example06
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 char s[10];
6 int i;
7 
8 for (i = 0; i < 5; i++)
9 s[i] = 'A' + i; /* A B C D E */
10 
11 s[5] = '\0'; /* essential! */
12 
13 printf("Built: %s\n", s);
14 return 0;
15}
Output
Built: ABCDE

Safe Copying with strncpy

strcpy does not check the destination size. strncpy takes a limit — but note that it does not always terminate, so add the \0 yourself:

Example07
CCode Cell
1#include <stdio.h>
2#include <string.h>
3 
4int main()
5{
6 char small[6];
7 const char *longText = "This is far too long";
8 
9 strncpy(small, longText, sizeof(small) - 1);
10 small[sizeof(small) - 1] = '\0'; /* guarantee termination */
11 
12 printf("Truncated safely: %s\n", small);
13 return 0;
14}
Output
Truncated safely: This 

Common Mistakes

MistakeProblem
char s[5] = "Hello";No room for \0
{'H','i'} without '\0'Not a valid string
s = "text"; after declaringArrays are not assignable
strcpy into a too-small arrayBuffer overflow
Filling manually and forgetting \0printf reads past the end
char *s; strcpy(s, "Hi");Writes through an uninitialised pointer
Trainer's Note: char s[5] = "Hello"; is especially nasty. Some compilers accept it with only a warning, storing the 5 letters and dropping the terminator. The program then appears to work until printf happens to run into a non-zero byte. Always count the terminator.
📝 Key Takeaways
  • char s[] = "Hello"; is the simplest form — \0 is added for you.
  • A character list needs \0 written explicitly.
  • Unused bytes in a partially initialised array become \0.
  • After declaration you must use strcpy, not =.
  • char s[] = "Hi" sizes the array to 3 automatically.

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4