jn1kk
jn1kk

Reputation: 5102

Weird C fprintf format notation

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.

Upvotes: 2

Views: 1278

Answers (3)

user7116
user7116

Reputation: 64068

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.

Upvotes: 1

Fred Foo
Fred Foo

Reputation: 363587

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

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

prints Hello, world!.

Upvotes: 2

Amadan
Amadan

Reputation: 198324

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

Upvotes: 6

Related Questions