Stringification

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

Hi All,

wondering if it is possible to place a defined value into a constant string

eg: PSTR ( "sum of the numbers is 30" );

but i want to use the VALUE_SUM define from below
instead of manually typing "30"

#define VALUE1 (10)
#define VALUE2 (20)
#define VALUE_SUM (VALUE1 + VALUE2)

thanks,

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

But the values are not strings, they are integers. To make them strings you need something like itoa or sprintf, but those aren't available to the preprocessor.

Regards,
Steve A.

The Board helps those that help themselves.

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

I used this based on samples I found

PSTR ( "sum of the numbers is " XSTR(VALUE_SUM) );

// Stringification
#define XSTR(s) STR(s)
#define STR(s) #s
/*
#define foo 4
STR (foo)
==> "foo"
XSTR (foo)
==> XSTR (4)
==> STR (4)
==> "4"
*/

still need to test it

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

Stringification does not do what you are expecting...

#define VALUE1 10
#define VALUE2 20
#define VALUE_SUM VALUE1 + VALUE2

#define XSTR(s)	 STR(s)
#define STR(s)	 #s

const char * sum_string = XSTR(VALUE_SUM);

results in

const char * sum_string = "10 + 20";

see http://gcc.gnu.org/onlinedocs/cpp/Stringification.html