File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programming Diversions and the fly likes Tricky , yet interesting question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Other » Programming Diversions
Bookmark "Tricky , yet interesting question" Watch "Tricky , yet interesting question" New topic
Author

Tricky , yet interesting question

Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

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


Muhammad Saif Asif Mirza
OCJA(5/6) OCJP(6) OCJWCD(6)
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11313
    
  16

Here's a better question...

Why would you want to?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

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
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40


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

Joined: Feb 13, 2012
Posts: 109
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

Joined: Aug 11, 2011
Posts: 440

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

Joined: Aug 11, 2011
Posts: 440

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

Joined: Sep 08, 2013
Posts: 5
wow... great and thanks for sharing
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Tricky , yet interesting question