7

I am used to C# or Java.

How could the following statement be correct in C?

printf("aaa" "bbb");

On my Xubuntu 15.04 with GCC 4.9. It outputs:

aaabbb

And as I tried, below works too!

CHAR *p = "aaa""bbb""ccc";
printf(p);

It outputs:

aaabbbccc

I think there should be a comma but in that way, the first string will be treated as a format string. So, is this syntax legal?

smwikipedia
  • 61,609
  • 92
  • 309
  • 482
  • 1
    This is the syntax for concatening two strings. Putting a comma will not work since it will be interpreted as two different function's arguments – aldeb Sep 04 '15 at 08:50
  • See http://programmers.stackexchange.com/questions/254984/on-concatenating-adjacent-string-literals – Benjy Kessler Sep 04 '15 at 08:51
  • `"aaa" "bbb"` in C is 100% equivalent to Java `"aaa" + "bbb"`. Only difference is that C doesn't force you to write `+`all over the place. – Lundin Sep 04 '15 at 09:11
  • Also see [Why allow concatenation of string literals?](http://stackoverflow.com/q/2504536/1708801) – Shafik Yaghmour Sep 04 '15 at 09:28

5 Answers5

16

Yes it is legal syntax because of translation phase 6 in ISO C99, #5.1.1.2 Translation phases:

  1. Adjacent string literal tokens are concatenated.
Jens
  • 69,818
  • 15
  • 125
  • 179
6

As mentioned adjacent strings are concatenated by the compiler. But if you want to see some difference you may add a \0 null terminator in your strings.

On adding the aaa\0 your o/p will be just aaa as printf will print till it finds the 1st \0 null terminator.

#include<stdio.h>

int main()
{

printf("aaa\0" "bbb");

}

Output

aaa

Vinay Shukla
  • 1,818
  • 13
  • 41
  • I know that '\0' is treated as the ending sign of a string in C. But do I need to explicitly add it to the end of a string? Like this `"aaa\0"`? – smwikipedia Sep 04 '15 at 08:57
  • hmm...very explicit! The `"aaa""bbb\0""ccc"` outputs `aaabbb`. But I think the compiler still makes a `aaabbb\0ccc\0`. But the `printf()` stops at the first `\0'. – smwikipedia Sep 04 '15 at 08:59
  • You do not need to add it yourself, the compiler will do so for you, but only when it realizes the string has ended, so char* s = "as" will be null terminated by compiler, but char* q = "as" "ca" will be null terminated only after "ca" – CIsForCookies Sep 04 '15 at 08:59
  • @CIsForCoocckies I agree the compiler adds it by default for you but printf executes till firs `\0` – Vinay Shukla Sep 04 '15 at 09:02
  • @smwikipedia agree with you – Vinay Shukla Sep 04 '15 at 09:03
  • @VinayShukla indeed, the printf will print the string, and the first time it sees "\0" it will stop – CIsForCookies Sep 04 '15 at 09:05
5

The two strings are just concatenated by the compiler.

wap26
  • 2,180
  • 1
  • 17
  • 32
2

When the compiler see two consecutive string literals, it concatenate them (at parsing time in the compiler), like you observe. This won't work (compiler syntax error) for non literals.

The comma operator is unrelated to concatenation. It evaluates first the left operand, then the right one, and discards the result of the left, giving the right result. It is useful for side effects (like progn in Lisp, ; in Ocaml, begin in Scheme). Of course, the comma is also used to separate arguments in calls.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

As @Jens said, adjacent string literals are concatenated by the compiler.

One reason for this is so that you can do preprocessor things like this:

#include <stdio.h>

#if defined(__linux__)
#define MY_OS "linux"
#elif defined(_WIN32)
#define MY_OS "windows"
#else
#define MY_OS "probably something BSD-derived"
#endif

int main(void){
    printf("my os is " MY_OS "\n");
}

Which saves everybody a lot of time.

Jens
  • 69,818
  • 15
  • 125
  • 179
RamblingMad
  • 5,332
  • 2
  • 24
  • 48