Question about size_t and strlen()

I found the following code

size_t len;[color=#222222][/color]
char buf[51];[color=#222222][/color]
void setup(){[color=#222222][/color]
  Serial.begin(9600);[color=#222222][/color]
}[color=#222222][/color]
void loop()[color=#222222][/color]
{[color=#222222][/color]
  if(Serial.available() > 0){[color=#222222][/color]
    len = Serial.readBytes(buf, 50);[color=#222222][/color]
    buf [len] = '';[color=#222222][/color]
  }[color=#222222][/color]
  if(strlen(buf) > 0){[color=#222222][/color]
    //do something with buf here[color=#222222][/color]
    *buf = '';[color=#222222][/color]
  }[color=#222222][/color]
}

(it doesn't preview properly, I can't figure out why, so here is where I found it in post#2) (I can't get the link to work either, so here it is: Arduino Forum I just pasted the URL in text and it worked, so why can't I paste it into the link box's URL field?

I seemed to understand the code except for a couple of things. The docs say about Serial.readBytes(), "Serial.readBytes()
returns the number of characters placed in the buffer." So if there are, say, 40 characters in the buffer, it returns the number 40 because that's the number of bytes it uses. Why does the number 40 need its own data type of size_t ? Couldn't int or byte or uint8_t hold the number 40?
And about

  if(strlen(buf) > 0){

search gave me, "No search results for strlen() in Reference" Why was strlen() used instead of sizeof() which is said to return "... the number of bytes occupied by an array." I would have thought that sizeof() was right on the money. Is there any difference using strlen() ?

I'm also wondering about the two ways post#2 does the same task, so basically, what is the difference between using Serial.readString() vs. **Serial.readBytes() **I find it a bit odd that the string goes into the variable on the left side of the "*=" *using Serial.readString(), but the same string would go on the right side of the "=" using Serial.readBytes() In this case the variable on the left is the buffer size, so if I don't need the size, could I just have a line of code be Serial.readBytes(buf, 50); (an optional "=" sign?) It seems like len = Serial.readBytes(buf, 50); is assigning two variables at once (len and buf). I know that's not really a question, but it throws me off balance a bit. (I'm not sure if there is anything to be said about my vertigo.) Thanks in advance!

size_t is the standard data type used often for anything related to the size of "something" in bytes. It is commonly found as the return type of sizeof or print() related calls but it’s not mandatory to use. It is similar to an unsigned long - 4 bytes on your arduino. If you know what you’ll get back is smaller then you can use byte (1 byte) or an unsigned int (2 bytes) to store the result of your function call. The C++ standards defined how the possible larger value will be shrunken to fit into the smaller one (you’ll loose the most significant bytes, so if it is a small enough value and you don’t overflow, it works).


strlen() is very different than sizeof:

  • sizeof is part of the language, it gives you the number of bytes allocated for a variable.

  • strlen() is a function call, part of the many standard libraries of C and C++ that returns the length of a cString (a null terminated char array).

Say you do this

char message[100] = "Hello";

the size of (sizeof operator) message is 100 whereas the length of the actual string stored in this buffer is 5 (the character letters ‘H’, ‘e’, ‘l’, ‘l’ and ‘o’).

The actual memory occupied will be 6 because a cString is null terminated so there is a non visible ‘\0’ character at the end that is not part of the length of the string but necessary for the functions to know where the text ends)


Look at the doc for the difference between Serial.readString() and Serial.readbytes(), in practice we would often advise against those, I would suggest to study Serial Input Basics to handle the serial port input.

sizeof() returns the number of bytes allocated for the string, including the null (\0) terminator.
strlen() returns the number of characters in the string, excluding the null (\0) terminator.

char str[] = "this is a string of characters";

void setup()
{
   Serial.begin(115200);
   Serial.print("sizeof returns ");
   Serial.println(sizeof(str));
   Serial.print("strlen() returns ");
   Serial.println(strlen(str));    
}

void loop()
{
}

output

sizeof returns 31
strlen() returns 30

See also

groundFungus:
sizeof() returns the number of bytes allocated for the string, including the null (\0) terminator.

That’s only true if you don’t offer a size for your array as the compiler allocates just enough bytes to fit the cString. So yes if you do char str[] = "this is a string of characters";but not the case if you do char str[100] = "this is a string of characters";

So sizeof is what you use to know the number of bytes allocated by the compiler to a type or a variable, strlen() the number of bytes occupied by the text in a cString (basically counting the number of bytes until you reach the null char, a byte with value 0) and strlen()+1 the memory used to represent your cString in your array (that is the null char at the end is part of the representation so you need room to accommodate this too).

I did not realize that, thanks for enlightening me. I looked at several tutorials before posting and none actually made that clear. All that I saw declared the string with empty braces (str[]).

char str[100] = "this is a string of characters";

void setup()
{
    Serial.begin(115200);
    Serial.print("sizeof returns ");
    Serial.println(sizeof(str));
    Serial.print("strlen() returns ");
    Serial.println(strlen(str));    
}

void loop()
{
}

output

sizeof returns 100
strlen() returns 30

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.