This question is a duplicate of https://stackoverflow.com/q/300522/773113, but since that question is on stackoverflow, technically it is not a duplicate. (I tried to mark it as a duplicate, but I was prevented, because the other question is not on Programmers SE.)

So, here is what is happening: it is all a matter of convention, and it is all arbitrary. Different languages and environments have their own conventions, (sometimes even self-contradictory,) and you need to learn the conventions of the language you are using, and follow it.

In the old times when C ruled, "size" was the fixed number of bytes allocated for something, while "length" was the smaller, variable number of bytes actually in use. Generally, "size" stood for something fixed, while "length" stood for something variable. But it was still not uncommon for someone to say that "a machine word is 32 bits long" instead of "the size of a machine word is 32 bits", despite the fact that the number of bits in a machine word is, of course, very fixed.

And then comes java, which has arrays of fixed size, but their size is returned via a length property, and strings of fixed size, but their size is returned via a length() method, and collections of variable size, but their length is returned via a size() method. So, java decided to turn things around.

Then came C#, which keeps the term "length" for stuff of fixed size, but for variable size stuff it uses the term "count", which would be perfect, if it was not for the unfortunate fact that besides being a noun it is also a verb, which can be taken to mean that when you get the "count" of a collection, the items in the collection will be counted one by one. (O(N) instead of O(1).)

So, go figure. There is no definitive answer, be sure to carefully study the documentation of the system that you are dealing with, and to understand the precise definition of the terms "length" and "size" within the context of that system, and be even prepared that there may be no precise definition of these terms, and they may be used interchangeably and arbitrarily.

Answer from Mike Nakis on Stack Exchange
🌐
Reddit
reddit.com › r/c_programming › "length" vs "size"
r/C_Programming on Reddit: "length" vs "size"
September 23, 2018 -

I see the terms "length" and "size" used interchangeably in a lot of places, even in the Linux kernel. Example

- "array size" when referring to number of elements in a array.

- "len" or "length" when referring to sizeof(something) i.e. number of chars (which is a byte in 99.9% of the cases).

An example where this causes confusion is with strings, where the size of a string would likely include the terminating nul byte, wheres the length of the string (i.e. strlen) do not.

Both strlen and sizeof have been around for decades, so why do people not follow this "convention"?

Sorry for the rant!

S.size() and S.length() wont work. Any Help? Jan 30, 2023
r/cpp_questions
3y ago
is using sizeof(example) really a big deal? Jul 4, 2024
r/cpp_questions
2y ago
Confused by sizeof integer in C Oct 14, 2024
r/learnprogramming
last yr.
Why many functions ask for length ? May 13, 2024
r/cprogramming
2y ago
More results from reddit.com
Top answer
1 of 1
8

This question is a duplicate of https://stackoverflow.com/q/300522/773113, but since that question is on stackoverflow, technically it is not a duplicate. (I tried to mark it as a duplicate, but I was prevented, because the other question is not on Programmers SE.)

So, here is what is happening: it is all a matter of convention, and it is all arbitrary. Different languages and environments have their own conventions, (sometimes even self-contradictory,) and you need to learn the conventions of the language you are using, and follow it.

In the old times when C ruled, "size" was the fixed number of bytes allocated for something, while "length" was the smaller, variable number of bytes actually in use. Generally, "size" stood for something fixed, while "length" stood for something variable. But it was still not uncommon for someone to say that "a machine word is 32 bits long" instead of "the size of a machine word is 32 bits", despite the fact that the number of bits in a machine word is, of course, very fixed.

And then comes java, which has arrays of fixed size, but their size is returned via a length property, and strings of fixed size, but their size is returned via a length() method, and collections of variable size, but their length is returned via a size() method. So, java decided to turn things around.

Then came C#, which keeps the term "length" for stuff of fixed size, but for variable size stuff it uses the term "count", which would be perfect, if it was not for the unfortunate fact that besides being a noun it is also a verb, which can be taken to mean that when you get the "count" of a collection, the items in the collection will be counted one by one. (O(N) instead of O(1).)

So, go figure. There is no definitive answer, be sure to carefully study the documentation of the system that you are dealing with, and to understand the precise definition of the terms "length" and "size" within the context of that system, and be even prepared that there may be no precise definition of these terms, and they may be used interchangeably and arbitrarily.

Discussions

What is the difference between sizeof() and strlen()
You can use strlen for getting the length of an string ( there is no concept of string its just pointer pointing to the first character of sequence of character and there is a terminating chracter. If there is a string which is of 4 characters then you will need 5 bytes to store why = char=1byte *4 + null terminating character) Now towards the main point - size of is used to know the size of data types like int ... More on sololearn.com
🌐 sololearn.com
17
6
October 31, 2019
c++ - What is the difference between "length" and "size" of a sequence? - Stack Overflow
Print out the number of items in the container, followed by the sum of the items in the container. That's it. ... There is no difference between the size of a sequence and the length of a sequence, they're two different words for the same thing, so you can stop worrying about that detail. More on stackoverflow.com
🌐 stackoverflow.com
difference between sizeof and strlen in c - Stack Overflow
Besides, sizeof() is a compile-time ... 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... More on stackoverflow.com
🌐 stackoverflow.com
c++ - Why does std::string have a length() member function in addition to size()? - Stack Overflow
I was reading the answers for How ... called length() for std::string (I always used size()). Is there any specific reason for having this member function in the std::string class? I've read both MSDN and CppRefernce, and they seem to indicate that there is no difference between size() and ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Quora
quora.com › What-is-the-difference-between-array-size-and-array-length
What is the difference between array size and array length? - Quora
Answer (1 of 4): * “Length” is generally taken to mean the number of elements in the array. * “Size” is the number of bytes that the array occupies in memory - and C provides a built-in function to return it, called “sizeof()”: EXAMPLE: [code]char array1 [ 100 ] ; int array2 [ 100 ...
🌐
Sololearn
sololearn.com › en › Discuss › 927744 › difference-between-size-and-length-in-c-for-string
Difference between size and length in c++ for string? | Sololearn: Learn to code for FREE!
string a,b; a.length a.size() what is the diffference between these two · c++stringdifferencea.lengtha.size() 14th Dec 2017, 2:23 PM · Frankestine Stine · 2 Answers · Answer · + 2 · They are synonyms for each other.
🌐
CodeProject
codeproject.com › Questions › 453779 › what-is-the-difference-between-size-and-length-fun
https://www.codeproject.com/Questions/453779/what-...
Disclaimer: References to any specific company, product or services on this Site are not controlled by GoDaddy.com LLC and do not constitute or imply its association with or endorsement of third party advertisers
🌐
Quora
quora.com › Do-people-use-string-length-or-string-size-in-C-Why
Do people use string:length or string:size in C++? Why? - Quora
Answer (1 of 5): [code ]size_t std::string::length()[/code] is the C++ equivalent of [code ]size_t strlen(char*)[/code] in the C library. [code]std::string::size() [/code]is the same value, the same code. But [code ]std::string[/code] also has [code]std::string::resize(size_t n) std::string::...
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › sizeof-vs-strlen-vs-size-in-cpp
sizeof() vs strlen() vs size() in C++ - GeeksforGeeks
July 23, 2025 - Data Types Supported: sizeof() ... (including the null values), strlen() is used to get the length of an array of chars/string whereas size() returns the number of the characters in the string....
Find elsewhere
🌐
Sololearn
sololearn.com › en › Discuss › 1812599 › what-is-the-difference-between-size-and-length-of-a-string-in-general-programing
What is the difference between size and length of A String ...
December 2, 2023 - Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › difference-strlen-sizeof-string-c-reviewed
Difference between strlen() and sizeof() for string in C - GeeksforGeeks
July 23, 2025 - 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.
🌐
Sololearn
sololearn.com › en › Discuss › 2097158 › what-is-the-difference-between-length-count-and-size
What is the difference between length, count and size?
sl_scroll_/en/Discuss/1099402/what-is-the-difference-between-the-librarys-like-cstdlib-or-ctime-and-the-librarys-of-c-stdlibh-or-timeh-in-cppPending
🌐
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 - There for it will return: 6. The sizeof() function, however, return the number of total elements in the array, that is: 100. ... Because the length of the string is also the size of the array.
🌐
Codecademy
codecademy.com › docs › c++ › strings › .length()
C++ (C Plus Plus) | Strings | .length() | Codecademy
April 10, 2023 - It functions exactly like the .size() ... “the length of a string” rather than “the size of a string”). However, both methods yield the same result. ... Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more! ... Learn C++ — a versatile ...
🌐
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 - Return value in strlen() is long int on the other hand return value in sizeof() is unsigned int. ... Here, my_string is a character array variable. In the below C program, we have declared string variable of type char.
🌐
Julia Programming Language
discourse.julialang.org › general usage
Why length(str) != sizeof(str)? - General Usage - Julia Programming Language
March 16, 2020 - I used to use length() for string, but right now I noticed it’s not correct for big string, around 3000 characters. sizeof() give right result. Why is it? thank you in advance.