← Back to C Course | Chapter 6: Arrays & Strings | Lesson 6 of 8

C Strings

A string in C is a row of letters that ends with a special stop marker, like a train with a last car. It is how C holds text.
Syntax
c
char string_name[size];

char string_name[] = "text";  // ends with a hidden '\0'

printf("%s", string_name);

What is a String?

This null-terminator convention is what lets functions like strlen() know where a string ends without being told its length separately -- but it also means the underlying char array must always be at least one byte larger than the visible text to hold that trailing zero.

Example: What is a String?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	char name[] = "Hi";
	// Print formatted text to the screen
	printf("%s", name);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Declaring and Initializing

char name[20] reserves a fixed 20-byte buffer regardless of how much text you actually store in it, while char name[] = "Hi" lets the compiler size the array to exactly fit the initializer plus its null terminator (3 bytes here).

Example: Declaring and Initializing

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	char name[20] = "Alex";
	// Print formatted text to the screen
	printf("%s", name);
	// Return 0 to signal that the program finished successfully
	return 0;
}

The Null-Terminator

Because \0 has the integer value 0, it's often described as falsy in conditional checks -- a common idiom like while (*str) loops until it hits the terminator, since any other character has a nonzero ASCII value.

Example: The Null-Terminator

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	char str[] = "Hi";
	// Declare i and set it to 0
	int i = 0;
	// Repeat while str[i] != '/0' is true
	while (str[i] != '\0') {
		i++;
	}
	// Print formatted text to the screen
	printf("%d", i);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Accessing Characters

Since a C string is just a char array, name[0] = J changes only the first character; there's no dedicated string-replace function, and any modification that shortens the visible text still needs its own \0 placed at the new end.

Example: Accessing Characters

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	char name[] = "Jill";
	name[0] = 'J';
	// Print formatted text to the screen
	printf("%s", name);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Reading Strings safely with fgets

fgets() takes a maximum buffer size as an argument, so it can never write past the end of your array the way gets() can -- the one gotcha is that fgets() keeps the trailing newline character if there's room, which gets() never did.

Example: Reading Strings safely with fgets

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	char buffer[20] = "Sample\n";
	// Print formatted text to the screen
	printf("%s", buffer);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting room for the null terminator, like char s[5] = "Hello";, which does not fit.
  2. Comparing strings with ==, which compares addresses, not contents; use strcmp.
  3. Assigning with s = "Hi" to a char array, which is not allowed; use strcpy.
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.