Why does the SimpleWebServerWiFi sketch use 'Serial.write()' at line 75 instead of 'Serial.print()'? Isn't the latter more appropriate?

WiFiNINA/SimpleWebServerWiFi.ino at master · arduino-libraries/WiFiNINA · GitHub

The write() command sends a raw byte across the serial line
print() , on the other hand, will try and interpret the number and send the ASCII-encoded version of that number.

@ruilviana!
Serial.print() (link)
Serial.write() (link)

Not In that case since the type of c is char

        char c = client.read();             // read a byte, then
        Serial.write(c);

So it would do what’s expected

It’s more of a performance idea as basically when you print() a char it calls write()

size_t Print::print(char c)
{
  return write(c);
}

so by calling write() directly you skip one indirection in the function call stack (which probably the optimizer would catch so it’s a moot point)

REF:

ASCII - SparkFun Learn.

Yes, I was agreeing with you, showing a link to the data pages.

1 Like

I am agreeing with the doc too but here print or write would do the same thing as print just calls write… so just call write yourself and dont rely on the optimizer to save an indirection

When you say "print just calls write", are you implying that, at least in certain cases, "print()" is functionally the same as "write()"?

I gave the code of print in post #4 when the type of the parameter is a char. You can see it’s just calling write

Of course other variants of print exist and will ve called depending on the type of the parameter. If you pass an int for example that’s when the conversion to the ASCII representation happens

If you click on the link you can see the source code for those variants too

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