You can use strlen. Size is determined by the terminating null-character, so passed string should be valid.

If you want to get size of memory buffer, that contains your string, and you have pointer to it:

  • If it is dynamic array(created with malloc), it is impossible to get it size, since compiler doesn't know what pointer is pointing at. (check this)
  • If it is static array, you can use sizeof to get its size.

If you are confused about difference between dynamic and static arrays, check this.

Answer from nmikhailov on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ c-program-to-find-the-length-of-a-string
C Program to Find the Length of a String - GeeksforGeeks
The length of a string is the number of characters in it without including the null character (โ€˜\0โ€™).
Published ย  July 11, 2025
Discussions

Calculating length of a string with strlen()
The first option only works if you have an array of chars, but most of the time (e.g. if you pass the string between functions) you're only going to have a pointer to the chars. The second option always works if your string is properly null terminated. If you want the length of the string including the null terminator itself, you can just do strlen(..) + 1. More on reddit.com
๐ŸŒ r/C_Programming
9
0
July 16, 2024
Why is the length of a string in Foreign.C.String defined as Int and not as CSize?
The Foreign.C.String module contains the type -- | A string with explicit length information in bytes instead of a -- terminating NUL (allowing NUL characters in the middle of the string). type CStringLen = (Ptr CChar, โ€ฆ More on discourse.haskell.org
๐ŸŒ discourse.haskell.org
0
2
September 28, 2021
Why and how is size_t type used for storing string length?
size_t is what it says. a size. specifically it is supposed to hold the largest possible size an object (in bytes) can be. It's an alias for an unsigned integer type. More on reddit.com
๐ŸŒ r/C_Programming
26
3
August 21, 2023
Size occupied by C++ string
The sizeof operator gives you the size of a type on the stack. This is completly evaluated at compile time. A std::string object itself holds about 4 worth pointers of memory*, with the text itself being stored outside of the object on the heap (manged by the pointers). If you want to know how much memory the string holds in total, you would need sizeof( str ) + (str.capacity()+1) * sizeof(char)` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ memory required by all the chars on the heap + the hidden null terminator. ~~~~~~~~~~~~~ ^ memory required by the object itself The reason that sizeof("text") returns something different is simple. What you put in there, is not a string, but a raw string literal, i.e. a char array. Its size can is "correctly" evaluated at compile time. Note its also the number of chars + 1 for the null terminator. what would be the Space Complexity of this code? For only the string, see above. *: In the given case, presumably using libstdc++ as the standard library implemntation. The implementation of std::string is not standardized, only its interface is. A std::string with microsofts standard library will be larger (as of the time of writing this). //edit: Since you are probably learning it is also worth noting that sizeof isnt something you usually use in normal codeTM . You may have seen it in those pesky sizeof(arr)/sizeof(arr[0]) tricks. Dont do that. More on reddit.com
๐ŸŒ r/cpp_questions
16
3
March 13, 2021
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ ref_string_strlen.php
C string strlen() Function
C Examples C Real-Life Examples ... strlen(myStr)); printf("%zu\n", sizeof(myStr)); Try it Yourself ยป ยท The strlen() function returns the length of a string, which is the number of characters up to the first null terminating ...
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ calculating length of a string with strlen()
r/C_Programming on Reddit: Calculating length of a string with strlen()
July 16, 2024 -

Let's say that we have a string char myStr[] = "Hello there!"

The 1st option to calculate the length is

int length = sizeof(myStr)/ sizeof(myStr[0]);

The 2nd option is simply to use the strlen() function, but I'm seeing that it will not count the null terminating character at the end.

My question: Would it be a problem using strlen() in embedded projects since it dosn't include the null terminating character as it's length? Or it's safer to just use the 1st option instead?

I'm thinking as an example of a case when you need to allocate emory, and you're relying on this strlen() function, it will not allocate for the null character, which might lead to problems down the line?

Thanks

๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ how-to-find-length-of-c-string-with-examples
Length of C String โ€“ How to Find the Size of a String in C
April 17, 2024 - The total size of the array includes the null terminator, \0, whereas the length of the string is the number of characters before the null terminator.
๐ŸŒ
Quora
quora.com โ€บ Every-data-type-has-a-size-in-C-What-is-the-size-of-a-string-type-in-C
Every data type has a size in C. What is the size of a string type in C? - Quora
Answer (1 of 11): There is no string type in C. Only char*. The size of the pointer type depends on your computer architecture e.g. 4 for 32-bit machine, and 8 for 64-bit machine. You can always print sizeof(char*) if in doubt.
Find elsewhere
๐ŸŒ
Programiz
programiz.com โ€บ c-programming โ€บ library-function โ€บ string.h โ€บ strlen
C strlen() - C Standard Library
#include <stdio.h> #include <string.h> int main() { char a[20]="Program"; char b[20]={'P','r','o','g','r','a','m','\0'}; // using the %zu format specifier to print size_t printf("Length of string a = %zu \n",strlen(a)); printf("Length of string b = %zu \n",strlen(b)); return 0; }
๐ŸŒ
Sub-Etha Software
subethasoftware.com โ€บ 2024 โ€บ 08 โ€บ 26 โ€บ in-c-you-can-sizeof-a-string-constant
In C, you can sizeof() a string constant? | Sub-Etha Software
August 27, 2024 - โ€ฆbut if you know it is a #define string constant, you can use sizeof() and that will be changed in to the hard-coded value that matches the length of that hard-coded string.
๐ŸŒ
GNU
gnu.org โ€บ software โ€บ libc โ€บ manual โ€บ html_node โ€บ String-Length.html
String Length (The GNU C Library)
Info document (gzipped tar file). ASCII text compressed (gzipped). TeX dvi file (gzipped). ... Texinfo source (gzipped tar file). The manual is available for the following releases of glibc:
๐ŸŒ
Unstop
unstop.com โ€บ home โ€บ blog โ€บ find length of string in c in 7 ways! (with code examples)
Find Length Of String In C In 7 Ways! (With Code Examples)
March 15, 2024 - Also read- How To Run C Program | Step-by-Step Explanation (With Examples) In C programming language, sizeof() is a built-in unary operator that returns the size (in bytes) of a data type, variable, or expression.
๐ŸŒ
Haskell Community
discourse.haskell.org โ€บ learn
Why is the length of a string in Foreign.C.String defined as Int and not as CSize? - Learn - Haskell Community
September 28, 2021 - The Foreign.C.String module contains the type -- | A string with explicit length information in bytes instead of a -- terminating NUL (allowing NUL characters in the middle of the string). type CStringLen = (Ptr CChar, Int) However, as far as I understand, string lengths are conventionally given as size_t in C, i.e., Foreign.C.Types.CSize in Haskell.
๐ŸŒ
Petrsu
cs.petrsu.ru โ€บ ~vadim โ€บ sp2012 โ€บ libc โ€บ String-Length.html
String Length - The GNU C Library
char string[32] = "hello, world"; char *ptr = string; sizeof (string) โ‡’ 32 sizeof (ptr) โ‡’ 4 /* (on a machine with 4 byte pointers) */ This is an easy mistake to make when you are working with functions that take string arguments; those arguments are always pointers, not arrays.
๐ŸŒ
Aticleworld
aticleworld.com โ€บ home โ€บ c program to find the length of a string
C Program to Find the Length of a String - Aticleworld
February 8, 2023 - In this article, you will learn to find the length of a string using the strlen() and your own created functions. Also using the sizeof().
๐ŸŒ
Codelessons
codelessons.dev โ€บ en โ€บ string-size-in-cplusplus
string::size in C++: Measuring String Length
But, thereโ€™s a catch! string::size gives the number of bytes, not necessarily characters. So, why the difference? Well, in simple terms, not all characters are made equal. Some characters (especially in non-English languages or special symbols) might take more than one byte.
๐ŸŒ
Medium
medium.com โ€บ @teamcode20233 โ€บ exploring-c-string-length-a-comprehensive-guide-d70a72b48df9
Exploring C# String Length โ€” A Comprehensive Guide | by Teamcode | Medium
June 6, 2023 - In this article, we will explore the various ways in which you can work with string length in C. In C#, the maximum length of a string is limited by the available memory resources of the system.
๐ŸŒ
Code Beautify
codebeautify.org โ€บ calculate-string-length
Calculate String Length Online
Calculate String Length is easy to use tool to count the characters in provided string.
๐ŸŒ
Sanfoundry
sanfoundry.com โ€บ c-tutorials-length-string-character-array-sizeof-operator-strlen-function
Difference between strlen() and sizeof() for String in C
December 31, 2025 - This program creates a string called ... not counting the null character โ€˜\0โ€™. ... The sizeof operator returns the total size in bytes of a variable or datatype at compile-time....
๐ŸŒ
Linux Hint
linuxhint.com โ€บ string_length_c_language
String Length in C Language โ€“ Linux Hint
We also can use the sizeof operator for string length (only for string literal). But, we have to subtract 1 from the value returned by this operator, because it also counts theโ€™\0โ€™ character. For array and pointer, the sizeof operator returns the allocated size of the array and the pointer, ...
๐ŸŒ
ScriptVerse
scriptverse.academy โ€บ tutorials โ€บ c-program-string-length.html
C Program: Length of a String (w/ strlen()) - ScriptVerse
The program below illustrates the use of strlen() to find the length of a given string. We use scanf("%[^\n]") to read the input string instead of the usual scanf("%s"). The difference is, while scanf("%s") stops reading characters on encounter of first blank space, the conversion specifier scanf("%[^\n]") reads them like any normal character.