HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to CVariablesData TypesOperatorsStringsArraysConditions (if / switch)Loops (for / while)FunctionsPointers
REFERENCE
📖 Overview
🖥️ Output & Input
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array
🔁 Condition & Loop
🧩 Function
👉 Pointer
C›Strings

Strings

Text = char array; working with strlen, strcat, and strcmp (string.h).

Simple enough for a 5-year-old to understand

Char array and length

In C, text is a char array. strlen from string.h is used for its length.

Example code
1
#include <stdio.h>
2
#include <string.h>
String functions.
ImportString functions.
3
int main() {
4
char name[] = "hello";
Text (char array).
VariableText (char array).
5
printf("%d\n", (int)strlen(name));
Length: 7
Output / InputLength: 7
6
return 0;
7
}
ImportVariableOutput / Input
💡 Hint: strlen returns size_t; cast to (int) for %d.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Print the length of the text "hexlibrary" as %d using strlen.

🎯 Expected output: 10

Loading editor…

About this topic

There is no text data type in C. Text is an array of characters, and its end is marked by a special character: the terminator character with a value of zero. All text functions look for this character to understand where the text ends.

This design has two consequences. First, to find the length of a text, you must scan it from beginning to end; this operation takes time proportional to the text length. Second, if the terminator character is lost, functions continue reading outside the array.

C's most well-known security vulnerabilities stem from this point. A write operation that overflows the allocated space of the array corrupts neighboring memory regions. Therefore, it is necessary to use boundary-checking versions of text functions.

What you will learn

  • That text is stored as a character array
  • Why the terminator character takes up space
  • The strlen, strcpy, and strcmp functions
  • Why comparing texts with equals is wrong

When to use?

When reading text from the user, use functions that specify limits. Reading functions without boundary checking have been completely removed from modern standards because safe usage was not possible.

If you are writing a program that does a lot of text processing, C is usually not the right tool; higher-level languages in this domain are both safe and fast.

Common mistakes

Wrong

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

int main() {
    char word[5] = "Hello";
    printf("%s\n", word);
    return 0;
}

A seven-character text does not fit into a five-byte array; furthermore, one more byte is needed for the terminator character, meaning at least eight bytes are required. The compiler gives the warning "initializer-string for array of chars is too long" and the program prints garbage by reading neighboring memory.

Correct

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

int main() {
    char word[] = "Hello";
    printf("%s\n", word);
    return 0;
}

Wrong

#include <stdio.h>

int main() {
    char a[] = "test";
    char b[] = "test";
    if (a == b) {
        printf("Equal\n");
    }
    return 0;
}

The equality operator compares memory addresses, not the contents of the texts. Since the two arrays reside in different places, their addresses are different and the condition is never true. For content comparison, strcmp is used and strcmp returns ZERO upon equality.

Correct

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

int main() {
    char a[] = "test";
    char b[] = "test";
    if (strcmp(a, b) == 0) {
        printf("Equal\n");
    }
    return 0;
}

Wrong

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

int main() {
    char name[20] = "Ali";
    strcpy(name, " Veli");
    printf("%s\n", name);
    return 0;
}

strcpy copies, it does not append: Ali inside name is erased and replaced with Veli. To append text, strcat is used. No error message is received, only unexpected output is seen, and bugs of this kind are the easiest to overlook.

Correct

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

int main() {
    char name[20] = "Ali";
    strcat(name, " Veli");
    printf("%s\n", name);
    return 0;
}

Frequently asked questions

Does strlen give the size of the array?

No, it gives the number of characters up to the terminator. If you wrote three letters to a twenty-byte array, strlen returns three. You need to keep track of the allocated size of the array yourself; C does not maintain this information at runtime.

Why do Turkish characters look corrupted?

char holds one byte, and Turkish letters take up two bytes in UTF-8 encoding. Therefore, strlen returns more than the letter count, and traversing character by character splits the letters in half. Using English characters in code examples prevents this problem right from the start.

Can I modify the text?

If you defined it as an array, yes. However, if you are pointing to a constant text with a pointer, that text resides in read-only memory and trying to modify it crashes the program. The two look the same but behave differently; this is one of C's classic traps.

Summary

Text is an array of characters marked at the end with a zero-valued character.

The array size must be at least one more than the length of the text.

Text comparison is done with strcmp and returns zero upon equality.

strcpy copies, strcat appends.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code