MeshWorld India LogoMeshWorld.

How to find the length of a string using strlen() in C

Listen to ArticleAI Speech
~2 min read narration
100%
How to find the length of a string using strlen() in C

A tutorial for finding the length of a string using strlen in C - language

  • The strlen() function counts the number of characters in a given string and returns the long unsigned integer value.
  • It is defined in C standard library, within <string.h> header file.
char proudToBeAnIndian[] = "East or West INDIA is best";
strlen(proudToBeAnIndian);
  • It stops counting when the null character is encounter. Because in C, null character is considered as the end of the string.

Working example

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

int main()
{
    char planet[] = "Earth", title[] = "MeshWorld";

    printf("\n Using strlen for title without null character: %zu", strlen(title));
    printf("\n Using sizeof for title with null character: %zu", sizeof(title));

    printf("\n Using strlen for planet without null character: %zu", strlen(planet));
    printf("\n Using sizeof for planet with null character: %zu", sizeof(planet));

    return 0;
}

Output

Using strlen for title without null character: 9
Using sizeof for title with null character: 10
Using strlen for planet without null character: 5
Using sizeof for planet with null character: 6

Note that the strlen() function doesn’t count for the null character \0 whereas sizeof() function does.

Happy 😄 coding

With ❤️ from 🇮🇳

Reader Quality Feedback

Did this technical guide help solve your problem?

Suggest Errata ($0)
Vishnu
Primary Author

Vishnu

Founder & Principal Architect at MeshWorld. Senior engineer and instructor specializing in AI agent systems, scalable web architecture, and modern development workflows.

Explore Author Archive
Compute Fuel & Open Testbed
100% Independent & Verified

Fuel High-Density, Zero-Fluff Engineering Deep-Dives

Every guide on MeshWorld is validated on physical Linux nodes and reproducible testbeds. If this article saved you hours of debugging or unblocked production, consider funding our next cluster run.

Weekly Dispatch

Join MeshWorld Dispatch

Get practical tutorials, system blueprints, and curated AI engineering notes straight to your inbox. No fluff, zero spam.

Zero spam. 1-click unsubscribe anytime.Prefer RSS?
Curated Continuations

Up Next in This Domain.

Browse Full Archive