Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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

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

prints Hello, world!.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top