2

Trying to figure out a stack corruption error in a function when I noticed this piece of code:

fprintf( fp, "\n%s %3c %12s %2c %12s %2c %12s %2c %12s %2c"
             "%12s %2c %12s", 
             xys_field[3],      x,
             xyzFunc(val1, 0),  x, 
             xyzFunc(val2, 0),  x, 
             xyzFunc(val3, 0),  x,
             xyzFunc(val4, 0),  x, 
             xyzFunc(val5, 0),  x, 
             xyzFunc(val6,0) );

What I am asking is about this line "\n%s %3c %12s %2c %12s %2c %12s %2c %12s %2c" "%12s %2c %12s", I don't even understand how this compiles since I never seen two formats follow each other like that. Thanks for any help.

jn1kk
  • 4,822
  • 1
  • 38
  • 68
  • possible duplicate of [String concatenation using preprocessor](http://stackoverflow.com/questions/5106280/string-concatenation-using-preprocessor) – user7116 May 14 '12 at 14:54
  • Ok, I guess that from the plethora of answers you did understand that you can concatenate strings in that way ;) Can you tell us something about the type of xys_field, x and the return value of xyzFunc (even if I think it may print garbage but it won't corrupt the stack). – Adriano Repetti May 14 '12 at 14:55
  • @Adriano The stack corruption error shows up at the end of the function. And there are dozens of statements like that. I doubt this is the one. Thanks anyways. I'll take care of the rest. – jn1kk May 14 '12 at 14:59
  • It's out of topic but I had a problem like that, usually the compiler "allocates" variable on the stack at the very beginning of the function (regardless where they're declared). I solved simply stepping line by line in debug with one eye on the ESP register (by the way finally it was a wrong calling convention :|) – Adriano Repetti May 14 '12 at 15:02

3 Answers3

6

Those are not two formats - notice the absence of comma, or anything separating them but whitespace. This is C syntax for continuation of a long string. In C, these are equivalent:

"abc" "def"
"abcdef"

Note that this only works for string literals; you can't concatenate string variables. This is a syntax error:

string1 string2
Amadan
  • 179,482
  • 20
  • 216
  • 275
  • Haha, thanks. Can't believe I was never taught this / learned this on my own. Thanks. Accept in a min. – jn1kk May 14 '12 at 14:55
2

In C, juxtaposed string literals (with only whitespace in between) denote a single string:

int main()
{
    puts("Hello, " "world!");
    return 0;
}

prints Hello, world!.

Fred Foo
  • 342,876
  • 71
  • 713
  • 819
1

This has nothing to do with format specifiers and everything to do with C allowing you to split a string literal into multiple parts (e.g. across lines for clarity) and have it be concatenated.

user7116
  • 61,730
  • 16
  • 140
  • 170