3

I have the following code:

    int i=1;
    printf((i==1)?" ":"   " "hello");
    printf(" " "hello");

And I am astonished to see that the first printf only gives a space as output and the second printf outputs a space followed by the string hello. I was expecting output like second one in case of first one. But is there something here I am missing. Please help me with this ...

5
  • yes..in first printf on a true condition there is single space and on false condition it has print double space, followed by print string hello....and in second printf a single space followed by string hello..
    – amin__
    Commented Jun 30, 2012 at 20:44
  • The real confusion here is likely an order of operations / operator precedence issue. Based on the poster's question, I'm assuming there's a missing set of parentheses in the first invocation of printf.
    – reuben
    Commented Jun 30, 2012 at 20:50
  • parenthesis around which part you assuming?
    – amin__
    Commented Jun 30, 2012 at 20:53
  • Around '(i==1)?" ":" "'. But that won't work with string concatenation. I'm just pointing out that the operator precedence is probably not what you're expecting... What are you really trying to accomplish?
    – reuben
    Commented Jun 30, 2012 at 20:55
  • I was misinterpreted string hello in the first printf is treating separately. But now I came to know from replies that this string hello is going to be concatenated with previous " " at compile time. So when the condition is true it outputs only " ". and when condition will be false then it would output " hello". but my problem was that, I was expecting output " hello" on a true condition of ternary operator...now it is cleared to me
    – amin__
    Commented Jun 30, 2012 at 21:02

5 Answers 5

8

String literal joining is a lexical feature, which means this:

(i==1) ? " " : "   " "hello"

is the same as this:

(i==1) ? " " : "   hello"

It should now be pretty obvious why you get the result you get.

0
2

i == 1 is true, so the ternary operator evaluates to the first of the two options, " ". Not at all surprising.

1
  • yes, I was thinking weirdly like left the string hello out of ?: operator..thank u
    – amin__
    Commented Jun 30, 2012 at 20:51
2

C automatically combines two adjacent string literals together.

So your parameter to the second printf: " " "hello" gets joined together to become " hello", which is then printed out normally.

Other answers have explained why your first printf works the way it does, which should be pretty obvious.

1

Since the condition tested in the ternary operator (i==1) evaluates to true, it returns the expression right after the ?.

The semantics of the ternary operator are something like this:

test_something?if_true:not_true

Your printf statement works as it should.

1

The source of your confusion is the misunderstanding of when the concatenation is performed. Joining two consecutive string literals is done by the compiler at compile time, not by your program at run time. Therefore there is only one way to parse the first printf: both string literals belong to the "else" branch of the expression. You can test it by setting i to zero and observing the output.

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