-2

I have a global variable

int CYCLE0;

I would like to insert into the middle of a very long snprintf statement.

snprintf(temp, 400,

           "<html>\
  <head>\
    <meta http-equiv='refresh' content='5'/>\
    <title>ESP32 Demo</title>\
    <style>\
      body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
    </style>\
  </head>\
  <body>\
    <h2>Turn on</h2> \n\
    <p>cycled "CYCLE0" times</p> \n\"
);

Error received:

unable to find string literal operator 'operator""CYCLE0' with 'const char [1118]', 'unsigned int' arguments

How do I insert a integer variable into the middle of a snprintf statement?

brad
  • 870
  • 2
  • 13
  • 38

1 Answers1

1

This is a straightforward application of snprintf. But, as always, you have to use a % specifier to insert something. Try this:

snprintf(temp, 400,
    "<html><head>"
    "<meta http-equiv='refresh' content='5'/>"
    "<title>ESP32 Demo</title>"
    "<style>"
    "body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }"
    "</style></head>"
    "<body>"
    "<h2>Turn on</h2>\n"
    "<p>cycled %d times</p>\n",
              CYCLE0);

I have split the long string across multiple lines using string constant concatenation, because I think that's more convenient and more readable than the backslash continuation you had used in the question.

Steve Summit
  • 45,437
  • 7
  • 70
  • 103