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?

4

5 Answers 5

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.
1
  • 1
    This is most authoritative. Commented Sep 4, 2015 at 9:01
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

6
  • 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"? Commented Sep 4, 2015 at 8: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'. Commented Sep 4, 2015 at 8: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" Commented Sep 4, 2015 at 8:59
  • @CIsForCoocckies I agree the compiler adds it by default for you but printf executes till firs \0 Commented Sep 4, 2015 at 9:02
  • @smwikipedia agree with you Commented Sep 4, 2015 at 9:03
5

The two strings are just concatenated by the compiler.

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.

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.

2
  • I'm not sure it is the main reason. Probably, the main reason is to be able to enter a very long multiline string literal constants without using backslashes at end of line. Commented Sep 4, 2015 at 11:18
  • @BasileStarynkevitch true, I didn't mean to imply it was the only reason :L Commented Sep 4, 2015 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.