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!

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.

🌐
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 ...
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › std-string-length-std-string-capacity-std-string-size-in-cpp-stl
std::string::length, std::string::capacity, std::string::size in C++ STL - GeeksforGeeks
July 23, 2025 - This returns the exact no. of ... the same and return the same value but size can be used in any container like vector, list, etc whereas length is more associated with the string....
🌐
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: ...
🌐
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....
🌐
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.
Find elsewhere
🌐
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!
strlen() returns the actual character ... for that variable. (Stdlib.h) As 1 character = 1 bytes, the length of the string is the dimension you declared -1 and they will return the same value....
🌐
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 language › length-of-array-in-c
Length of Array in C - GeeksforGeeks
The Length of an array in C refers to the maximum number of elements that an array can hold. It must be specified at the time of declaration. It is also known as the size of an array that is used to determine the memory required to store all ...
Published   October 17, 2025
🌐
Team Treehouse
teamtreehouse.com › community › do-you-always-have-to-declare-the-length-or-size-of-an-array-in-c
Do you always have to declare the length or size of an array in C? (Example) | Treehouse Community
August 12, 2014 - I just realized you said C, not Objective-C. In C, I don't believe there is a way to initialize an array without a specific size.
🌐
W3Schools
w3schools.com › cpp › cpp_strings_length.asp
C++ String Length
string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; cout << "The length of the txt string is: " << txt.size(); Try it Yourself »
Top answer
1 of 10
108

C arrays do keep track of their length, as the array length is a static property:

int xs[42];  /* a 42-element array */

You can't usually query this length, but you don't need to because it's static anyway – just declare a macro XS_LENGTH for the length, and you're done.

The more important issue is that C arrays implicitly degrade into pointers, e.g. when passed to a function. This does make some sense, and allows for some nice low-level tricks, but it loses the information about the length of the array. So a better question would be why C was designed with this implicit degradation to pointers.

Another matter is that pointers need no storage except the memory address itself. C allows us to cast integers to pointers, pointers to other pointers, and to treat pointers as if they were arrays. While doing this, C is not insane enough to fabricate some array length into existence, but seems to trust in the Spiderman motto: with great power the programmer will hopefully fulfill the great responsibility of keeping track of lengths and overflows.

2 of 10
39

A lot of this had to do with the computers available at the time. Not only did the compiled program have to run on a limited resource computer, but, perhaps more importantly, the compiler itself had to run on these machines. At the time Thompson developed C, he was using a PDP-7, with 8k of RAM. Complex language features that didn't have an immediate analog on the actual machine code were simply not included in the language.

A careful read through the history of C yields more understanding into the above, but it wasn't entirely a result of the machine limitations they had:

Moreover, the language (C) shows considerable power to describe important concepts, for example, vectors whose length varies at run time, with only a few basic rules and conventions. ... It is interesting to compare C's approach with that of two nearly contemporaneous languages, Algol 68 and Pascal [Jensen 74]. Arrays in Algol 68 either have fixed bounds, or are `flexible:' considerable mechanism is required both in the language definition, and in compilers, to accommodate flexible arrays (and not all compilers fully implement them.) Original Pascal had only fixed-sized arrays and strings, and this proved confining [Kernighan 81].

C arrays are inherently more powerful. Adding bounds to them restricts what the programmer can use them for. Such restrictions may be useful for programmers, but necessarily are also limiting.

🌐
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. ... Because the length of the string is also the size of the array.