16

Please take the following:

char buffer[512];

memset(buffer, 0, sizeof(buffer));
sprintf(&buffer[0],"This Is The Longest String In the World that in text goes on and..");

printf("Buffer:%s\r\n",buffer);

I would like to be able to create this string over multiple lines, for ease of troubleshooting and editing. However, when I use the \ command my output is separated by what appear to be tabs?

Example:

sprintf(&buffer[0],"This Is The\
    Longest String In the World\
    that in text goes on and..");

yields an output of:

Buffer:This Is The        Longest String In the World       that in text goes on and..

Any ideas? Is this just an incorrect approach to try to break up a string across multiple lines of code?

pb2q
  • 58,613
  • 19
  • 146
  • 147
Nanomurf
  • 709
  • 3
  • 12
  • 32

3 Answers3

25

The newline continuation takes into account any whitespace within the code.

You can take advantage of string literal concatenation for better readability:

sprintf(buffer, "This Is The "
                "Longest String In the World "
                "that in text goes on and..");

Using \ you'll need to begin the continuation of your string at column 0:

sprintf(buffer, "This Is The \
Longest String In the World \
that in text goes on and..");
pb2q
  • 58,613
  • 19
  • 146
  • 147
  • 1
    Forehead slapped. Auto-tabbing has done me in! Thank you for pointing this out, this was indeed the issue. – Nanomurf Oct 02 '12 at 17:08
8

Although this may seem pedantic, I've been bitten enough times in the real world to have the following issues with the other two posted answers.

  • The two posted answers neglect to give spaces between words joining the separate string literals (obvious, after the first test).

  • If your string is really long, use snprintf() instead--it is slightly clumsier, but it tells anyone reviewing your code that you are aware of common dangers in code maintenance.

  • If your string happens to contain %, you'll get a compiler warning (good) or random segmentation faults (bad). So use "%s" or, perhaps in this case, just strcpy(). (In two months' time, a co-worker could easily add 99.9% to the message.)

  • The use of memset(), which I see often, is just cargo-cult programming. Yes, in special cases one needs it, but using it all the time sends the wrong message.

  • And finally, why would anyone use &buffer[0] when just buffer would do?

So to summarize, your code should perhaps read:

char buffer[512];
snprintf(buffer, sizeof buffer, "%s", 
   "This is The Longest String "
   "In the World that in text "
   "goes on and on and on and on ....");
printf("Buffer:%s\r\n", buffer);
Joseph Quinsey
  • 9,553
  • 10
  • 54
  • 77
  • 1
    These are all very good points. Thank you for taking the time to make them. The code above is really just an example of what I'm dealing with, but all of the points you've listed here are very valid. And to answer your last question: I'm not an elegant programmer, so often rely on brute force. >< I appreciate the feedback! – Nanomurf Oct 02 '12 at 18:29
  • 1
    @JosephQuinsey I didn't think it was necessary, even though you did draw my attention to deficiencies in my answer. The goal on this site is good questions and answers, and that each must stand on it's on merits, even if that means synthesizing useful pieces of other answers, and I often don't @ cite other users. Having said that, the input from your comment is also useful to me, and I do apologize. – pb2q Oct 02 '12 at 21:07
  • @pb2q: Thank you for your very graceful response. – Joseph Quinsey Oct 02 '12 at 21:12
  • 2
    agree with the first 4 points, but I think `&x[0]` is not a flaw and it clearly specifies what is happening. – M.M Dec 29 '15 at 02:25
4

This too would just work as well:

char buffer[512];
sprintf(&buffer[0], "This is the longest string"
        "in the world that in text"
        "goes on and on and on and on ....");
printf("%s\n", buffer);
Vikdor
  • 23,934
  • 10
  • 61
  • 84