printf(), C string & Comma operator

Write here if you have problems with your C source code

Moderator: Board moderators

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

printf(), C string & Comma operator

Post by IIUC GOLD » Sun Aug 08, 2004 1:53 pm

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

Code: Select all

Hello
Can anyone explain the output.

User avatar
sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

Post by sohel » Sun Aug 08, 2004 2:23 pm

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

User avatar
UFP2161
A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm
Contact:

Post by UFP2161 » Sun Aug 08, 2004 5:21 pm

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”