What does the F() do exactly?

Thanks for finding out the answer mmcp42. I guess F() is a part of the solution to SRAM problem but eventually becomes part of the FLASH problem. Does anyone know how to turn on optimization option for the compiler while using arduino ide?

I believe it's just a technique to avoid using "words" that people might already have in their program
who in their right mind would use variables like __fred?!?

Right. One would have to count (underscores) instead of read (words) :slight_smile:

this is a bit klunky, but might help
(like I said, who in their right mind actually types __ :slight_smile: )

__FlashStringHelper *myStrings[3];

void setup(void)
{
  Serial.begin(19200);
  myStrings[0] = F("abcdefg");
  myStrings[1] = F("xbcdefg");
}
void loop(void)
{
  Serial.println(myStrings[0]);
  Serial.println(myStrings[1]);
  delay(1000);
}

I think you can even initialize the array at compile time rather than run time.

__FlashStringHelper *myStrings[] = {F("abcdefg"), F("xbcdefg")};

void setup(void)
{
  Serial.begin(19200);
}
void loop(void)
{
  Serial.println(myStrings[0]);
  Serial.println(myStrings[1]);
  delay(1000);
}

:slight_smile:

[quote author=Coding Badly link=topic=91314.msg685894#msg685894 date=1328909536]

ninja2:
Could this F() syntax be used in place of rather cumbersome:
strcpy_P(buffer, (char*)pgm_read_word(&(str_table[index])));

This may help...
http://arduiniana.org/libraries/flash/[/quote]

can you clarify the origin of the F() syntax, is it from Mikal Hart's Flash library, or is it part of C or the standard IDE ?

ninja2:
can you clarify the origin of the F() syntax, is it from Mikal Hart's Flash library

That's how I was introduced to it.

so:
(and this may seem a very dumb question)

if (I don't include the flash library in my sketch)
{ F() will not work? ;}
endif

This sketch compiles and runs, so the answer seems to be no, one doesn't have to (explicitly) include Flash library to use F().

// F test

void setup() {
    Serial.begin(9600);
    
    Serial.println(F("STRINGA 1"));
    Serial.println(F("STRINGA 2"));
}

void loop() {
}

doh! I could've done that ... excellent way to resolve my query, thanks mromani

:slight_smile:

Could someone please help me out with this, I am unable to get the F() macro compiled on FreeBSD.
I am using avr-gcc and the arduino 1.0 codebase.
The following line
Serial << F("This is a test string");
throws up an error by the compiler which says

flash_streaming.cpp:210:13: error: reinterpret_cast from type 'const char*' to type '__FlashStringHelper*' casts away qualifiers

I apologize for cross posting, but I already asked the same question in an earlier post - but didn't make any headway

The command I am issuing is -
avr-gcc tst.cpp -mmcu=atmega328p -DF_CPU=16000000UL -Os -w -Wl,--gc-sections -ffunction-sections -fdata-sections -DARDUINO=100 -I /home/rr/code/arduino//include/arduino/ -I . -c -o tst.o

kb3dow:
Serial << F("This is a test string");

Try

Serial.print( F("This is a test string") );

or

Serial.println( F("This is a test string") );

It has nothing to do with the << operator, even Serial.print(F("Hello World")) has the same problem.
Serial.print(F("Hello World"));
gives
tst.cpp:19:18: error: reinterpret_cast from type 'const char*' to type '__FlashStringHelper*' casts away qualifiers

I have a medium sized multi-file sketch compiling and functioning using avr-gcc, so there is no problem in general with compiling/linking etc. But this particular issue has me puzzled.

What version of avr-gcc are you using?

avr-gcc-4.5.1

It seems you found a bug in the Arduino code. Here's the fix. Make the following change to WString.h.

class __FlashStringHelper;
//#define F(string_literal) (reinterpret_cast<__FlashStringHelper *>(PSTR(string_literal)))
#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))

Yes, that appears to fix it. It compiles and I'll test it at home later tonight.

Thank you very much.

I have posted a bug report to the Arduino team.

http://code.google.com/p/arduino/issues/detail?id=866

Another question with the F() macro, now that I have it working. The following code fails to compile

struct some_table{
char *name;
int val;
} arrary[] = {
{ F("a"), 0},
{ "b", 1}
};

The compiler says
tst.cpp:31:7: error: statement-expressions are not allowed outside functions nor in template-argument lists
Maybe I should try using the flash library and use a variable to hold that string in PROGMEM