SovranCode
HomeCourses C C Strings
This device
Course contentsC Strings · 21 titles

C Fundamentals

C IntroductionC First ProgramC VariablesC Data TypesC OperatorsC Input and Output

Control Flow

C If ElseC For LoopC While Loop

Functions and Collections

C FunctionsC ArraysC Strings

Memory and User-Defined Types

C PointersC StructuresC EnumsC Dynamic Memory

Files, Tooling, and Projects

C File HandlingC PreprocessorC DebuggingC MakefilesC Project: Command-Line Inventory Manager
Learn C Programming21 complete lessons

C Fundamentals

C IntroductionC First ProgramC VariablesC Data TypesC OperatorsC Input and Output

Control Flow

C If ElseC For LoopC While Loop

Functions and Collections

C FunctionsC ArraysC Strings

Memory and User-Defined Types

C PointersC StructuresC EnumsC Dynamic Memory

Files, Tooling, and Projects

C File HandlingC PreprocessorC DebuggingC MakefilesC Project: Command-Line Inventory Manager
PREVIOUS LESSONC Arrays
NEXT LESSONC Pointers
Functions and Collections 70 min

C Strings

Treat text as a character array that ends at \0, keep capacity visible, and never use gets().

What you will leave with

You will treat a C string as a character array that ends at \0, size buffers for that terminator, print with %s and %c, compare with strcmp, and refuse unbounded input such as gets().

A C string is a character array terminated by \0

C has no built-in string object. char city[16] = "Rabat"; creates a writable array. The visible letters occupy positions 0 through 4; C places a terminating null character \0 at position 5. String-aware functions use that marker to know where text ends.

Capacity must include both the visible characters and the terminator. char city[6] = "Rabat"; fits exactly. char city[5] = "Rabat"; does not leave room for \0 and is invalid. Array indexes and index < count loops are the same rules as C Arrays; the extra rule is the terminator.

C FUNDAMENTALS RUNNER

Print a full string and one selected character

%s prints until \\0. city[0] selects one character for %c.

#include <stdio.h>

int main(void) {
  char city[16] = "Rabat";

  printf("City: %s\n", city);
  printf("First character: %c\n", city[0]);
  return 0;
}
TERMINAL OUTPUT
Edit the program, predict its output, then run it.

This safe browser runner supports the lesson subset: scalar and fixed-size int/double arrays, character strings, indexed reads and numeric writes, arithmetic, printf/puts, numeric scanf into scalar or bounded array-element addresses, comparisons, bounded if/else, for/while loops, and return.

Try this next: Change Rabat to a city with at most 15 visible characters. Then try city[1] and explain why the final visible index is length - 1.

Read text with a capacity-aware policy

gets() is removed from modern C and was never safe: it writes until a newline with no limit. Do not use it. scanf("%s", city) is also unsafe for a fixed buffer because it has no width. For a char city[16] that reads one whitespace-delimited word, use scanf("%15s", city);. The width leaves one slot for \0. It stops at whitespace, so it is not suitable for a name with spaces.

For a line of text, prefer fgets(city, sizeof city, stdin). It receives the buffer and its capacity, and it can retain spaces. Check the return value. It may keep a trailing newline when there is room; real programs often remove that newline before comparison or display.

string.h counts and compares characters, not capacity

strlenCounts visible characters before \0.#include <string.h>It does not know the array's total capacity. Only use it with a properly terminated string.
strcmpUse strcmp(a, b) == 0 for equal text.if (strcmp(city, "Rabat") == 0) { ... }== on arrays compares addresses, not characters.
strcpyNeeds a destination large enough for the source plus \0.char copy[16];Prefer a bounded copy such as snprintf(copy, sizeof copy, "%s", city) when the source length is not proven.
#include <stdio.h>
#include <string.h>

int main(void) {
  char city[16] = "Rabat";

  printf("Length: %zu\n", strlen(city));
  if (strcmp(city, "Rabat") == 0) {
    puts("Match");
  }
  return 0;
}

Do not write through a pointer to a string literal

char city[] = "Rabat"; creates an array you can modify. char *city = "Rabat"; points at a string literal. Treat that literal as read-only: city[0] = 'r'; has undefined behavior. Use a character array whenever the program must change the characters. Pointer details continue in C Pointers.

Make capacity visible at the declaration

When a buffer has a known purpose, choose a named size or use sizeof buffer at the call that needs capacity. Avoid a separate magic number that can drift away from the actual array.

Independent lab: a labeled report name

Store a report title in a char array large enough for the text plus \0, print it with %s, and print the first character with %c. Compile locally to add strlen and a strcmp check. Do not call gets().

Lesson review

  • A C string is a char array that ends at \0.
  • Capacity must include the terminator.
  • %s prints the whole terminated string; %c prints one character.
  • strlen and strcmp need a terminator; they do not know the buffer size.
  • Never use gets(). Bound scanf or prefer fgets.

Related lessons

  • C Arrays — Strings reuse array indexes, capacity, and bounds.
  • C Pointers — A string literal is accessed through a pointer to char.
  • C Input and Output — Read lines with fgets instead of unbounded scanf %s.
KNOWLEDGE CHECK

Keep the terminator inside the buffer

A C string is a character array that ends at \\0. Capacity must include that extra character.

01Which declaration has enough storage for the string "Rabat"?
02What marks the end of an ordinary C string stored in a char array?
03Which printf placeholder prints a full null-terminated character string?
04What does strlen(city) count for a properly terminated string?
05Which input call protects a 16-character city buffer from a longer word?
PREVIOUS LESSONC Arrays
NEXT LESSONC Pointers
ON THIS PAGEC StringsNull-terminated arraysReading stringsstring.hString literalsIndependent labLesson reviewKnowledge checkRelated lessons
Course contents