Problems trying to use print F

using example digitalreadserial
ESP8266 board
1.8.2 IDE

/*
  DigitalReadSerial

  Reads a digital input on pin 2, prints the result to the Serial Monitor

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/DigitalReadSerial
*/

// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = D2;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the pushbutton's pin an input:
  pinMode(pushButton, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input pin:
  int buttonState = digitalRead(pushButton);
  // print out the state of the button:
  Serial.println((buttonState));
  delay(1);        // delay in between reads for stability
}

compiles properly.

Sketch uses 263316 bytes (25%) of program storage space. Maximum is 1044464 bytes.
Global variables use 26792 bytes (32%) of dynamic memory, leaving 55128 bytes for local variables. Maximum is 81920 bytes.

===
only change is to add (F in the print line

/*
  DigitalReadSerial

  Reads a digital input on pin 2, prints the result to the Serial Monitor

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/DigitalReadSerial
*/

// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = D2;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the pushbutton's pin an input:
  pinMode(pushButton, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input pin:
  int buttonState = digitalRead(pushButton);
  // print out the state of the button:
  Serial.println(F(buttonState));
  delay(1);        // delay in between reads for stability
}

will not compile.
error :

In file included from /home/dave/.arduino15/packages/esp8266/hardware/esp8266/2.5.0/cores/esp8266/Arduino.h:261:0,
from sketch/DigitalReadSerial.ino.cpp:1:
/tmp/arduino_modified_sketch_452053/DigitalReadSerial.ino: In function 'void loop()':
/home/dave/.arduino15/packages/esp8266/hardware/esp8266/2.5.0/tools/sdk/libc/xtensa-lx106-elf/include/sys/pgmspace.h:37:114: error: initializer fails to determine size of '__c'
#define PSTR(s) (extension({static const char __c attribute((aligned(4))) PROGMEM = (s); &__c[0];}))
^
/home/dave/.arduino15/packages/esp8266/hardware/esp8266/2.5.0/cores/esp8266/WString.h:38:76: note: in definition of macro 'FPSTR'
#define FPSTR(pstr_pointer) (reinterpret_cast<const __FlashStringHelper *>(pstr_pointer))
^
/home/dave/.arduino15/packages/esp8266/hardware/esp8266/2.5.0/cores/esp8266/WString.h:39:34: note: in expansion of macro 'PSTR'
#define F(string_literal) (FPSTR(PSTR(string_literal)))
^
/tmp/arduino_modified_sketch_452053/DigitalReadSerial.ino:27:18: note: in expansion of macro 'F'
Serial.println(F(buttonState));
^
/home/dave/.arduino15/packages/esp8266/hardware/esp8266/2.5.0/tools/sdk/libc/xtensa-lx106-elf/include/sys/pgmspace.h:37:114: error: array must be initialized with a brace-enclosed initializer
#define PSTR(s) (extension({static const char __c attribute((aligned(4))) PROGMEM = (s); &__c[0];}))
^
/home/dave/.arduino15/packages/esp8266/hardware/esp8266/2.5.0/cores/esp8266/WString.h:38:76: note: in definition of macro 'FPSTR'
#define FPSTR(pstr_pointer) (reinterpret_cast<const __FlashStringHelper *>(pstr_pointer))
^
/home/dave/.arduino15/packages/esp8266/hardware/esp8266/2.5.0/cores/esp8266/WString.h:39:34: note: in expansion of macro 'PSTR'
#define F(string_literal) (FPSTR(PSTR(string_literal)))
^
/tmp/arduino_modified_sketch_452053/DigitalReadSerial.ino:27:18: note: in expansion of macro 'F'
Serial.println(F(buttonState));
^
exit status 1
Error compiling for board LOLIN(WEMOS) D1 R2 & mini.

buttonState isn’t a string.

The F macro expects a literal string, not an integer.

2 Likes
Serial.println(F(buttonState));

What did you expect this to do?
What benefit did you expect it to provide?

Are you thinking of Serial.printf( ) ?

As i understand the F() function
Puts the information in PROGMEM and not RAM.

i thought Serial.printf()
Is used with character strings. But not sure.

Serious question: Why would you put a run time variable in flash? Unless of course it’s a constant...

As someone previously asked, what is the benefit?

The whole point of (F) is to leave constant strings in flash rather than pointlessly copying them to ram.

Ok. It was an attempt to write to flash memory at runtime. Well, on the ESP you can do it.
For example, the eeprom read/write is emulated in flash. On AVR (uno etc.) The flash can be written to in the context of the bootloader only.

Not just like that.

The ESP8266 can write to Flash with a very special function: ESP.flashWrite().
It can also select part of the Flash as a file system.
And it can do OTA (Over The Air upload of a sketch).
Those are all very special things.

By the way, there is a special bootloader for the Uno that can write to Flash during runtime: https://hackaday.com/2015/07/03/arduinos-and-other-avrs-write-to-own-flash/.
The Uno can not execute code from ram and can not read data from Flash. The usage of PROGMEM requires again a very special function.

I think I got [ answers for ] what I need and I need to do some more studying.

one of the reasons is that over time. memory gets corrupted or there is a higher use just due to time.

WhattsThat mentioned putting a constant in flash, not variables.
so...

Serial.print(F("buttonState = "));
Serial.println(buttonState);

did compile , and lets the variable go where it belongs.