Pages: [1]   Go Down
Author Topic: the F() function?  (Read 353 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
CJH Innovations
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

?
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17263
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
CJH Innovations
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Offline Offline
Edison Member
*
Karma: 64
Posts: 2489
The last thing you did is where you should start looking.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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"));         
Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Faraday Member
**
Karma: 61
Posts: 2898
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 -

Logged

Rob Tillaart

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

Pages: [1]   Go Up
Jump to: