-9

I tried the following C code by accident:

char *str = "hello " "world";

It's right, but I can't understand. How to explain this instrument?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
L.T
  • 1
  • 1
  • 1
    What have you found out yourself? What does your C book say? Online tutorials? Your compiler? Anything? And how is that related to compiler construction? (and it is not gcc specific). – too honest for this site Apr 18 '17 at 11:44
  • 2
    Adjacent string literals get concatenated. You could've looked it up. – DeiDei Apr 18 '17 at 11:44

2 Answers2

0

According to the C Standard (5.1.1.2 Translation phases)

  1. Adjacent string literal tokens are concatenated.

So after this translation phase this code snippet

char *str = "hello " "world";

is adjusted to

char *str = "hello world";

As result the pointer str points to the first character of the string literal "hello world".

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

C standard says these literals are to be concatenated.

More details in earlier answer: How does concatenation of two string literals work?

Jeroen3
  • 919
  • 5
  • 20