Thuta Learning
IntermediateProgrammingbeginner

Strings

Relax. We'll talk through this in plain words — no textbook voice.

C doesn't have a dedicated string type. Text is stored as a character array with a null character at the end. If you're handling text data, it's worth really understanding this string concept.

c
#include <stdio.h>
#include <string.h>

int main() {
  char name[] = "ThutaTech";

  printf("Name: %s
", name);
  printf("Length: %lu", strlen(name));
  return 0;
}

%s is the format specifier for printing strings. strlen() calculates the string's length, not counting the null character.

You should see
Name: ThutaTech Length: 9

Info

If you want to use string functions, <string.h> needs to be included.

Easy traps

  • If your string buffer isn't large enough, you risk an overflow. In C, you're responsible for string memory safety yourself.
Strings | Thuta Learning