Pages: [1]   Go Down
Author Topic: the F() function?  (Read 319 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)
Online Online
Brattain Member
*****
Karma: 331
Posts: 16485
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: 56
Posts: 2144
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: 17
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
Edison Member
*
Karma: 28
Posts: 2040
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
Online Online
Shannon Member
*****
Karma: 168
Posts: 12427
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: