6

Just found out that all of the following work:

printf( "%ls\n", "123" L"456" );
printf( "%ls\n", L"123" "456" );
printf( "%ls\n", L"123" L"456" );

The output is

123456
123456
123456

Why can I freely mix and match wide and narrow string literals to get a wide string literal as a result? Is that a documented behavior?

Shafik Yaghmour
  • 143,425
  • 33
  • 399
  • 682
dragonroot
  • 5,010
  • 2
  • 30
  • 57

2 Answers2

9

Is that a documented behavior?

Yes, this behavior is supported by the standard, from section 6.4.5 String literals paragrph 4 of the C99 draft standard says (emphasis mine):

In translation phase 6, the multibyte character sequences specified by any sequence of adjacent character and wide string literal tokens are concatenated into a single multibyte character sequence. If any of the tokens are wide string literal tokens, the resulting multibyte character sequence is treated as a wide string literal; otherwise, it is treated as a character string literal.

Shafik Yaghmour
  • 143,425
  • 33
  • 399
  • 682
6

6.4.5 String literals

In translation phase 6, the multibyte character sequences specified by any sequence of adjacent character and wide string literal tokens are concatenated into a single multibyte character sequence. If any of the tokens are wide string literal tokens, the resulting multibyte character sequence is treated as a wide string literal; otherwise, it is treated as a character string literal.

msc
  • 30,333
  • 19
  • 96
  • 184
David Ranieri
  • 36,077
  • 6
  • 44
  • 85