0

In K&R, I saw an example where a string can be concatenated with a space"

char *s = "abc" "foo";
printf("%s", s); // prints "abcfoo"

How is space string concatenation different from using strcpy and strcat?

user2864740
  • 60,010
  • 15
  • 145
  • 220
George Newton
  • 3,153
  • 7
  • 34
  • 49
  • 2
    Fairly sure that's only for string literals. – Blorgbeard Feb 28 '14 at 00:10
  • possible duplicate of [concatenation of two string literals](http://stackoverflow.com/questions/12120944/concatenation-of-two-string-literals) , http://stackoverflow.com/questions/3142630/implementation-of-string-literal-concatenation-in-c-and-c?rq=1 – user2864740 Feb 28 '14 at 00:13

2 Answers2

1

Adjacent string literal are concatenated by the pre-processor. From the draft C99 standard section 5.1.1.2 Translation phases paragraph 6:

Adjacent string literal tokens are concatenated

so it creates one string literal as a result.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
1

Try: char *s = "abc" " " "foo";

BTW: it is not concatenation per se, it is just a method for initialising a string with a concatenation of a bunch of literals.

wildplasser
  • 43,142
  • 8
  • 66
  • 109