Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why a comma can be missed in a printf() call?

Tags:

c

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?

like image 599
smwikipedia Avatar asked Sep 04 '15 08:09

smwikipedia


2 Answers

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.
like image 66
Jens Avatar answered Sep 30 '22 23:09

Jens


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

like image 35
Vinay Shukla Avatar answered Sep 30 '22 21:09

Vinay Shukla