Go Down

Topic: the F() function? (Read 555 times) previous topic - next topic


Has anyone seen this before? I can't find it in the docs.

Serial.println( F("What is this for?") );



Has anyone seen this before? I can't find it in the docs.

Serial.println( F("What is this for?") );


I've seen it recently used by some people, I know what it's purpose is but haven't used it myself. It's to force the compiler to store the constant string into Flash memory of the AVR chip rather then the limited SRAM memory where normally all variables, constants, and arrays are stored. A 328P chip has 32KB of flash memory but just 2K of SRAM space, so you can see that most people tend to run out of SRAM before they run out of FLASH in their larger sketches.




I just found this in WString.h (the last file in the list - Murphy's law), but your explanation is what I really needed.

#define F(string_literal) (reinterpret_cast<__FlashStringHelper *>(PSTR(string_literal)))


It is a macro.
Yes, it is used to place a string in Flash rather than RAM thus saving RAM.
Just a note, it can also be used with the LCD library.
ex.  lcd.print(F("test"));         
No technical PMs.
The last thing you did is where you should start looking.


It can be used with any class with a print and a println method  -- any class that is derived from Print.


I don't think it is actually a "function".

I'd probably use the term "macro"  but that is probably wrong too.


The official name is a preprocessor directive,
before the code is compiled these directives are replaced in the source to generate the final code.

details see - http://en.wikipedia.org/wiki/C_preprocessor -

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131