Warning on printp_P possible ?

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

I always use printf_P(PSTR("Hello world")) instead of printf("Hello world") because of the RAM memory usage which works great.
However sometimes I forget the PSTR(), which results in printing some random text. Is there any way I can get a compiler warning on this mistake ?

Thanks

Patrick

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I guess you could consider redefining printf_P (warning: I have not tried it):

#define printf_P(format, ...) printf_P(PSTR(format), __VA_ARGS__)

Eugene

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You will need to use a slightly different definition if you want to make it work when there are no arguments. i.e. only a format string.
Example:

#define printf (fmt, ...) printf_P(PSTR(fmt), ##__VA_ARGS__)

The ## token paste operator is very important when using a variadic macro as it removes the comma when there are no additional arguments.

Note: the above remaps printf and then you never use PSTR or printf_P() anymore. You simply call printf() with a "normal" string constant and the format strings is *always* mapped into flash.

--- bill

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

The ## token paste operator is very important when using a variadic macro as it removes the comma when there are no additional arguments.

Neat, something I wasn't aware of! I didn't know the paste operator would also remove the trailing operator if the argument was omitted. Do you have a reference in the C spec where this is discussed, or is it a GCC-only extension to the preprocessor?

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
which results in printing some random text.
That's why they invented debug. :wink: anyway that's your warning.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

abcminiuser wrote:

Neat, something I wasn't aware of! I didn't know the paste operator would also remove the trailing operator if the argument was omitted. Do you have a reference in the C spec where this is discussed, or is it a GCC-only extension to the preprocessor?

- Dean :twisted:

Is there another compiler? :lol:

I believe this is a GCC extension:
(There was some C99 reference but I couldn't quite tell if it is the same for C99)
http://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html#Variadic-Macros

It comes in very handy for situations like this where you need to eliminate the comma.

I've used various forms of this mapping
for printf() myself as I don't see a need to ever put printf() format strings in RAM
on AVR environments and it seems simpler, at least for me, to simply hide all the PSTR() and printf_P() weirdness and forget about it.

--- bill

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks guys, nice solution !

Quote:

That's why they invented debug. anyway that's your warning.

.. which brings me to the dilemma :
Which of the following is the best :
1) Someone who found a few bugs in his own code or
2) Someone who found lots of bugs in his own code ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Paddy wrote:
Which of the following is the best :
1) Someone who found a few bugs in his own code or
2) Someone who found lots of bugs in his own code ?
3) Someone who fixed lots of bugs in his code! :twisted:

Stu

Engineering seems to boil down to: Cheap. Fast. Good. Choose two. Sometimes choose only one.

Newbie? Be sure to read the thread Newbie? Start here!