strlen() is used to get the length of a string stored in an array.

sizeof() is used to get the actual size of any type of data in bytes.

Besides, sizeof() is a compile-time expression giving you the size of a type or a variable's type. It doesn't care about the value of the variable.

strlen() is a function that takes a pointer to a character, and walks the memory from this character on, looking for a null character. It counts the number of characters before it finds the null character. In other words, it gives you the length of a C-style null-terminated string.

The two are quite different. In C++, you do not need either very much, strlen() is for C-style strings, which should be replaced by C++-style std::strings, whereas the primary application for sizeof() in C is as an argument to functions like malloc(), memcpy() or memset(), all of which you shouldn't use in C++ (use new, std::copy(), and std::fill() or constructors).

Answer from Mithun Sasidharan on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › c language › difference-strlen-sizeof-string-c-reviewed
Difference between strlen() and sizeof() for string in C - GeeksforGeeks
July 23, 2025 - Evaluation size: sizeof() is a compile-time expression giving you the size of a type or a variable's type. It doesn't care about the value of the variable. Strlen on the other hand, gives you the length of a C-style NULL-terminated string.
Discussions

sizeof VS. strlen
The sizeof operator tells you the size of its operand. The size of an array is the size of an element times the number of elements; str is an array of 10 characters @ 1 byte each, so its size is 10. Note that sizeof does just that; for example, if you pass a pointer to an array to sizeof, you are going to get the size of a pointer, not of the underlying array. This is a common pitfall for beginners. Note further that array size is completely unrelated to string length. The array size as reported by sizeof is generally computed at compile time; sizeof doesn't know and doesn't care how much of the array you actually used. That's what the strlen function is for. More on reddit.com
🌐 r/C_Programming
33
11
October 9, 2018
c - How to get the string size in bytes? - Stack Overflow
As the title implies, my question is how to get the size of a string in C. Is it good to use sizeof if I've declared it (the string) in a function without malloc in it? Or, if I've declared it as a More on stackoverflow.com
🌐 stackoverflow.com
Is it possible to get sizeof string in array of string literals?
No, you cannot use the sizeof operator for this. The sizeof operator works entirely off the type of its operand. Indeed, this is pretty much why the operator can also be used directly on a type. But there's nothing in the type of strarray that says what the length of those strings are. The type of this object is "3-element array of pointer to constant character", so the only things sizeof has to work on are "3", "pointer" and "character"... which is why you got those results when you used it on various parts of strarray. More on reddit.com
🌐 r/C_Programming
19
2
May 19, 2020
sizeof a string
Hi all. Yet again a question about the sizeof operator. First an example of the code (without setup, loop etc.) ; void function (char *_min, char *_max) ( byte a = sizeof(_min); byte b = sizeof(_max); Serial.println(a, DEC); Serial.println(_min); Serial.println(b, DEC); Serial.println(_max); ... More on forum.arduino.cc
🌐 forum.arduino.cc
0
0
December 13, 2009
🌐
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 sizeof() operator returns the total size in bytes of a string. ... #include <stdio.h> int main(void) { char greeting[] = "Hello"; int size = sizeof(greeting); printf("The size is %d bytes \n", size); } // Output: // The size is 6 bytes
🌐
W3Schools
w3schools.com › c › c_data_types_sizeof.php
C sizeof operator
The memory size refers to how much space a type occupies in the computer's memory. To actually get the size (in bytes) of a data type or variable, use the sizeof operator:
🌐
Reddit
reddit.com › r/c_programming › sizeof vs. strlen
r/C_Programming on Reddit: sizeof VS. strlen
October 9, 2018 -
#include<stdio.h>
#include<string.h>
main()
{
char str[10] = "clue";
strcpy(str, "no ");
strcat(str, "more");
printf("%d", sizeof(str) - strlen(str));
}

In the following code, I expected the output to be 1, since if I'm not wrong, the length of str changes twice after it's initialized, and in the end I have ''no more'' string, strlen for which is 7 and sizeof for which is 8, since \0 is also included in the count. I get 3 for some reason though.

Top answer
1 of 5
38
The sizeof operator tells you the size of its operand. The size of an array is the size of an element times the number of elements; str is an array of 10 characters @ 1 byte each, so its size is 10. Note that sizeof does just that; for example, if you pass a pointer to an array to sizeof, you are going to get the size of a pointer, not of the underlying array. This is a common pitfall for beginners. Note further that array size is completely unrelated to string length. The array size as reported by sizeof is generally computed at compile time; sizeof doesn't know and doesn't care how much of the array you actually used. That's what the strlen function is for.
2 of 5
2
Both functions are somewhat out dated in the real world. Firstly the "sizeof" thing is not really a function at all. The compiler will compute whatever "sizeof" should be and stuff the result into the output assembly language and then the assembler and linker just take that in a literal values. There really is no such function as "sizeof" and you won't find it in any header anywhere. #include #include int main(int argc,char *argv[]){ size_t k = sizeof(unsigned char); fprintf(stdout,"%d\n",k); return EXIT_SUCCESS; } Take the above and ask gcc to output assembly language on whatever machine you are on : $ gcc -m32 -march=i386 -mno-default -std=c99 -S -o h.s h.c Then look for a call to the function sizeof. You won't find it. It doesn't exist. $ cat h.s .file "h.c" .text .section .rodata .LC0: .string "%d\n" .text .globl main .type main, @function main: .LFB1: .cfi_startproc leal 4(%esp), %ecx .cfi_def_cfa 1, 0 andl $-16, %esp pushl -4(%ecx) pushl %ebp .cfi_escape 0x10,0x5,0x2,0x75,0 movl %esp, %ebp pushl %ecx .cfi_escape 0xf,0x3,0x75,0x7c,0x6 subl $20, %esp movl $1, -12(%ebp) movl __stdoutp, %eax subl $4, %esp pushl -12(%ebp) pushl $.LC0 pushl %eax call fprintf addl $16, %esp movl $0, %eax movl -4(%ebp), %ecx .cfi_def_cfa 1, 0 leave .cfi_restore 5 leal -4(%ecx), %esp . . . However look closely and you will see the literal value 1 being stuffed into some memory region that is 12 bytes less than the contents of the register %ebp. That is the literal value one for one byte. That is the sizeof(unsigned char). Change the code : #include #include int main(int argc,char *argv[]){ size_t k = sizeof(double); fprintf(stdout,"%d\n",k); return EXIT_SUCCESS; } So now we have sizeof(double) in there. movl $8, -12(%ebp) Run the code and you will get 8 as your output. There never was a call to sizeof and there never will be. So that leaves strlen() which isn't very helpful in the real world where unicode and utf-8 character strings are everywhere and they are the norm. We have to stuff them into database records and fetch them later and strlen() is really not helpful at all.
🌐
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 - C strings have a 0 byte added to the end of them, so “hello” is really “hello\0”. The standard C string functions like strcpy(), strlen(), etc. look for that 0 to know when to stop. #include <stdio.h> #include <stdlib.h> // for EXIT_SUCCESS #include <string.h> // for strlen() #define STRING "hello" int main() { printf ("sizeof(STRING) = %ld\n", sizeof(STRING)); printf ("strlen(STRING) = %ld\n", strlen(STRING)); return EXIT_SUCCESS; }
🌐
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 - The ways to calculate the length ... By using the built-in sizeof() operator, which returns the length of the string in terms of the size of the character array (in bytes)....
Find elsewhere
🌐
W3Schools
w3schools.com › c › ref_string_strlen.php
C string strlen() Function
C Examples C Real-Life Examples ... Try it Yourself » · The strlen() function returns the length of a string, which is the number of characters up to the first null terminating character....
🌐
Scaler
scaler.com › home › topics › how to use the strlen() function in c
How to use the strlen() function in C - Scaler Topics
November 3, 2023 - We can also find the length of a string using the sizeof Operator in C. The sizeof is a unary operator which returns the size of an entity (variable or value).
🌐
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, ...
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › string length in c
String Length in C – Methods & Examples
April 28, 2025 - If it is '\0', return 0. This keeps counting characters until the end is reached. The sizeof() operator returns the size of the array or pointer, including null terminator in arrays. For pointers, it gives the pointer’s size, not the string’s length, leading to incorrect results.
🌐
Reddit
reddit.com › r/c_programming › is it possible to get sizeof string in array of string literals?
r/C_Programming on Reddit: Is it possible to get sizeof string in array of string literals?
May 19, 2020 -

I know you can do this:

const char str[] = "string";
size_t len = sizeof(str);

but if I had multiple strings in an array, how would I use sizeof on one of the strings:

const char *strarray[] = {"string1", "string2", "string3"};
sizeof(strarray); //this equals size of all pointers
sizeof(strarray[0]); //this equals pointer size
sizeof(*strarray[0]); //this equals char size

Is this possible in C or do I have to use strnlen?

🌐
Guru99
guru99.com › home › c programming › difference between strlen() and sizeof() for string in c
Difference between strlen() and sizeof() for string in C
August 8, 2024 - Strlen method is used to find the length of an array whereas sizeof() method is used to find the actual size of data. Strlen() counts the numbers of characters in a string while sizeof() returns the size of an operand.
🌐
Quora
quora.com › What-is-the-difference-between-strlen-and-sizeof-when-used-on-strings-in-the-C-programming-language
What is the difference between strlen and sizeof when used on strings in the C programming language? - Quora
Evaluation size: sizeof() is a compile-time expression giving you the size of a type or a variable’s type. It doesn’t care about the value of the variable. strlen on the other hand, gives you the length of a C-style NULL-terminated string. Summary: The two are almost different concepts and used for different purposes. ... Since size of char in C is 1 byte but then also we find that strlen() gave one less value than sizeof().
🌐
Cplusplus
cplusplus.com › reference › cstring › strlen
Cplusplus
For example: char mystr[100]="test string"; defines an array of characters with a size of 100 chars, but the C string with which mystr has been initialized has a length of only 11 characters. Therefore, while sizeof(mystr) evaluates to 100, strlen(mystr) returns 11.
🌐
Sololearn
sololearn.com › en › Discuss › 2054396 › what-is-the-difference-between-sizeof-and-strlen
What is the difference between sizeof() and strlen() | Sololearn: Learn to code for FREE!
October 31, 2019 - If there is a string which is of ... ... The function sizeof() is a unary operator in C language and used to get the size of any type of data in bytes....
🌐
Arduino Forum
forum.arduino.cc › forum 2005-2010 (read only) › software › syntax & programs
sizeof a string - Syntax & Programs - Arduino Forum
December 13, 2009 - Hi all. Yet again a question about the sizeof operator. First an example of the code (without setup, loop etc.) ; void function (char *_min, char *_max) ( byte a = sizeof(_min); byte b = sizeof(_max); Serial.pr…