Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Tricky , yet interesting question

 
Saif Asif
Ranch Hand
Posts: 440
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greeting ranchers

I was asked an interesting question about programming recently and I want to share with you all.

"Without using any semi-colons in the entire code write a simple application in C/C++ which simply prints "Hello World" on the console "

Of-course I know the answer but I want to see your responses first before I share the solution
 
fred rosenberger
lowercase baba
Bartender
Posts: 12127
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a better question...

Why would you want to?
 
Saif Asif
Ranch Hand
Posts: 440
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No particular reason . Of-course no one wants to do that , it was just a challenge that my friend gave to all of us and we were coming will all sorts of weird solutions , in the end he showed us how to do it So I thought to ask here on the ranch as well
 
Henry Wong
author
Marshal
Pie
Posts: 21125
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saif Asif wrote:No particular reason . Of-course no one wants to do that , it was just a challenge that my friend gave to all of us and we were coming will all sorts of weird solutions , in the end he showed us how to do it So I thought to ask here on the ranch as well


Since this is a java site, wouldn't the question be better if the challenge was for java?

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21125
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

And BTW, here is the solution in Java...



For C/C++, I would try to do something similar with the C preprocessor.

Henry
 
Myke Enriq
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class App {
static{
if( System.out.append("hello world").checkError() ){}

}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

}

}


This is how you do it in Java
 
Saif Asif
Ranch Hand
Posts: 440
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Since this is a java site, wouldn't the question be better if the challenge was for java?


That's an even better idea !

 
Saif Asif
Ranch Hand
Posts: 440
Eclipse IDE Hibernate Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
And BTW, here is the solution in Java...



For C/C++, I would try to do something similar with the C preprocessor.

Henry


You are using the Unicode for semi-colon here, but the requirement was to avoid the use of a semi-colon at all . Still I like your approach !



Myke wrote:



This is how you do it in Java



Great approach Mike .

The solution in C is similar to this one, i.e to wrap the console writer within an if statement and an empty if block ! And truly your code is without any semi-colon at all ! Great !
 
ragu krishna
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wow... great and thanks for sharing
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic