Hello World!

Tuesday, August 31, 2010

It is a very commonplace question for technical interviews that tests your proficiency in C. The problem is quite well-known and so is its solution. The objective is to print two string literals say "Hello" and "World" using two printf statements where one printf should be a part of an if block and the other a part of its matching else. The most common solution is

if(!printf("Hello "))
{}
else printf("World");

Being aware that printf returns the number of characters printed on screen (in case of Hello, it is 5) which we negate using the logical operator ! to make the entire expression as 0 implying false and thus the else part is executed.

Now what if, you were restricted to use any of the logical operators for achieving the same job. Well, here's a solution. The most undermined comma (,) operator plays an interesting role in doing the job:

if(printf("Hello") , NULL) //we can even write 0 instead of NULL
{}
else printf("World");
Note in C, the comma operator has an interesting semantic. All expressions separated by comma's are evaluated from left-to-right however the value of the entire expression is the right-most expression. i.e a= (5+2,3+5,6*4); then a gets the value 24, though all the previous expressions are evaluted. Now using this feature, the if condition part is well explained.

If someone can come up with a more compact and cryptic solution to this, do post it here.

Read more...

My take on Holmes.. Mr. Sherlock Holmes...

Wednesday, August 4, 2010

Never was I an ardent book-worm to turn to classics. For good books are like rare wine and unfortunately I've tasted only a handful of them. For my reading habits get honed up only when I come to know about a best-seller. However, lately, I'd been sitting quite idle post my University degree, which compelled me to raid my bookshelf in the hope of getting something interesting to engage myself upon.


Frantic searches led my hand on a book whose front-page bears my school's emblem with a small message of good luck, reminding me how I walked up the podium to collect this book as a souvenir during our farewell party in the year 2004. Six years hence I thank my school for having gifted me this book which kept me enthralled until I came to the tragic end. It brings me a lot of shame for not having experienced such a recognized collection.

Arthur Conan Doyle has laid before the world some criminal framework which we seldom come across in modern thrillers. Every little adventure has a slow beginning, but the best part is, it suddenly draws the readers full attention into some "tragic case" and you feel arrested until you reach the climax. Over and again I've tried reading a piece in parts but failed in every attempt. My chosen five adventures out of the 24 selected ones, in their order of ranking, would undoubtedly be:
  • The Speckled Band
  • The Red-Headed League
  • The Five Orange Pips
  • The Engineer's Thumb
  • The Adventure of the Beryl Coronet
Although I've dared to make selections out of the 24, but I'm sure every Holmes lover would've appreciated the "Sign of Four" which depicts some unparallel methods for crime detection.

I fancy that the most unfortunate adventure as a reader, must be the "The Adventure of the Final Problem" which eventually causes Sherlock's death and termination. I know, that the ardent subscribers' of the Strand Magazine that time, roared against the termination of such a mesmerizing character and the popularity was such, that it led to new series "The Return of Sherlock Holmes". However, I'm still looking for the other collection namely
"The Casebook of Sherlock Holmes" , "The Hound of Baskervilles" and "The Best of Sherlock Holmes".
I'm ready to cough up a price for a used copy, if someone is ready to sell off, although I doubt whether someone has the courage to shed off such collection from their shelves in return of a scanty sum...



Read more...

  © Blogger templates Newspaper by Ourblogtemplates.com 2008

Back to TOP