Page 1 of 1

printf(), C string & Comma operator

Posted: Sun Aug 08, 2004 1:53 pm
The following code
[c]char* strTest="Hello";
printf(("%s World!!", strTest));
gives output

Code: Select all

Can anyone explain the output.

Posted: Sun Aug 08, 2004 2:23 pm
by sohel
printf(("%s World!!", strTest));
This function looks a little different from the traditional. You have used two pairs of parantheses to write the function.
Perhaps thats the reason. :)

Posted: Sun Aug 08, 2004 5:21 pm
by UFP2161
The comma operator evaluates things from left to right, and returns the result of the last operation, i.e. tries to evaluate "%s World!" and then tries to evaluate strTest .. then it returns strTest, which is a string, which printf can directly print, and thus you get "Hello"