Go Down

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

Checkers1811

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

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

?

retrolefty


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.

Lefty

Checkers1811

Thanks,

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)))


LarryD

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"));         
The way you have it in your schematic isn't the same as how you have it wired up!

WizenedEE

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

michinyon

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

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

robtillaart

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