printf(), C string & Comma operator

Write here if you have problems with your C source code

Moderator: Board moderators

Post Reply
New poster
Posts: 19
Joined: Tue Jun 11, 2002 4:27 pm
Location: Bangladesh

printf(), C string & Comma operator

Post by IIUC GOLD »

The following code
[c]char* strTest="Hello";
printf(("%s World!!", strTest));
gives output

Code: Select all

Can anyone explain the output.

Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

Post 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. :)

A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm

Post 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"

Post Reply

Return to “C”