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
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
sizeof( <string literal> ) - Post.Byes - Bytes
This topic is closed. ... When applied to a string literal, is the sizeof operator supposed to return the size of the string (including nul), or the size of a pointer? For example, assuming a char is 1 byte and a char * is 4 bytes, should the following yield 4, 5, of something else? More on post.bytes.com
🌐 post.bytes.com
November 13, 2005
Understanding `sizeof` return values on `Char` / `String`
Can someone explain this behavior of sizeof vs summarysize ? sizeof("z") # 1 sizeof('z') # 4 Base.summarysize('z') # 4 Base.summarysize("z") # 9 When I read the doc sizeof(str::AbstractString) Size, in bytes, of the string str. Equal to the number of code units in str multiplied by the size, ... More on discourse.julialang.org
🌐 discourse.julialang.org
1
0
August 17, 2021
🌐
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
🌐
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.
🌐
W3Schools
w3schools.com › c › c_data_types_sizeof.php
C sizeof operator
Note that we use the %zu format specifier to print the result, instead of %d. This is because the compiler expects the sizeof operator to return a value of type size_t, which is an unsigned integer type.
Find elsewhere
🌐
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…
🌐
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.
🌐
Lobsters
lobste.rs › s › kgxogp
In C, you can sizeof() a string constant? | Lobsters
In C++ you can use the safe std::size() function to get the compile-time size of a string constant, or any array for that matter (including C arrays.) And it will fail if the argument isn’t an array with a size known at compile time.
🌐
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....
🌐
Post.Byes
post.bytes.com › home › forum › topic
sizeof( <string literal> ) - Post.Byes - Bytes
November 13, 2005 - This is one of the very few cases where an array reference does *not* turn into a pointer to the zero'th element; the `sizeof' operator applies to the array as a whole. See also Questions 6.4 and 6.8 -- in fact, read all of Section 6 -- in the comp.lang.c Frequently Asked Questions (FAQ) list ... Re: sizeof( &lt;string literal&gt; ) Don Starr <nospam@nospam.
🌐
W3Schools
w3schools.com › c › ref_string_strlen.php
C string strlen() Function
char myStr[20] = "Hello World"; printf("%zu\n", 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 character.
🌐
Cprogramming
cboard.cprogramming.com › cplusplus-programming › 119597-difference-between-strlen-sizeof.html
Difference between strlen() and sizeof()
September 15, 2009 - Hey, The ''sizeof" is a keyword of operator, while the strlen() is a function. The sizeof returns the length of a varible or data type, in other words, how many bytes this data block of type occupied. It can used on all dada types. The strlen() can only operate on a string and returns the number ...
🌐
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?

🌐
Shiksha
shiksha.com › home › it & software › colleges in india
Difference between strlen() and sizeof() in C
December 16, 2024 - Amity University Noida, Galgotias University, MIT-WPU, Parul University, Christ University and D.Y.
🌐
Medium
huutamnguyen.medium.com › random-programming-problems-part-1-differences-between-strlen-and-sizeof-in-c-8df07a04f723
Differences between strlen() and sizeof() in C++ (Random Programming Problems Part 1) | by Tam Nguyen | Medium
September 7, 2021 - It counts the number of elements in the string till NULL. There for it will return: 6. The sizeof() function, however, return the number of total elements in the array, that is: 100.