Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the meaning of F(string) in C++?

Tags:

c++

arduino

Currently I'm writing C++ code on Arduino. In an example, I find a expression

Serial.print(F("Disconnected from central:"));

It's obvious that this statement is used to send string to the serial, but why it uses F(string) instead of using string directly?

I try to google it but with no results. If someone know it, I would be greatly appreciated.

like image 440
Xiangyi Meng Avatar asked Dec 07 '25 13:12

Xiangyi Meng


1 Answers

This macro is Arduino-specific, it's not "C++" as such.

It places the string in flash memory, to conserve RAM.

It means the string cannot be modified when the program runs.

One current definition is:

#define F(slit) (reinterpret_cast<const __FlashStringHelper *>(PSTR(slit)))

See the source code for more.

like image 79
unwind Avatar answered Dec 09 '25 01:12

unwind



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!