By default, all strings are handled as all other initialized variables: they occupy RAM (even though the compiler might warn you when it detects write attempts to these RAM locations), and occupy the same amount of flash ROM so they can be initialized to the actual string by startup code. The compiler can optimize multiple identical strings into a single one, but obviously only for one compilation unit (i. e., a single C source file).

That way, any string literal will be a valid argument to any C function that expects a const char * argument.

Of course, this is going to waste a lot of SRAM. In Program Space String Utilities, a method is described how such constant data can be moved out to flash ROM. However, a constant string located in flash ROM is no longer a valid argument to pass to a function that expects a const char *-type string, since the AVR processor needs the special instruction LPM to access these strings. Thus, separate functions are needed that take this into account. Many of the standard C library functions have equivalents available where one of the string arguments can be located in flash ROM. Private functions in the applications need to handle this, too. For example, the following can be used to implement simple debugging messages that will be sent through a UART:

#include <inttypes.h>
#include <avr/io.h>
#include <avr/pgmspace.h>

uart_putchar(char c)
  if (c == '\n')
  loop_until_bit_is_set(USR, UDRE);
  UDR = c;
  return 0; /* so it could be used for fdevopen(), too */

debug_P(const char *addr)
  char c;

  while ((c = pgm_read_byte(addr++)))

  ioinit(); /* initialize UART, ... */
  debug_P(PSTR("foo was here\n"));
  return 0;


By convention, the suffix _P to the function name is used as an indication that this function is going to accept a "program-space string". Note also the use of the PSTR() macro.

Back to FAQ Index.