Pages

Wednesday, April 14, 2010

What’s with a Semicolon; !!!

Engineering programming labs in VTU, are pretty much the same boring sessions where you copy something from the notice board and type it on the computer, ask the lab assistant how to compile the program and just copy down the output and leave the lab. But here is something for those who want to spice up their programs this is a technique i learnt in high school. How to write C/C++ programs without a semicolon?”.

A little Background:

This technique is very vastly discussed all over the internet in the past few years, but when i learnt this the internet in India was still in its infancy and our only source was the Library and if you were lucky enough a programming geek (usually 5~6 yrs older than you) with uncles in USA.

The Trick:

Conditional statements. !!
That's all.
A normal output statement in c would be
printf(“Hello”);
And in C++ it would be
cout<<”Hello”;
A normal Condition in C/C++ would look like this,
if(Test condition){
Stmnt1;
Stmnt2;
}
The interesting part is the Test condition, just replace it with your output statement that's all.

Example:

#include <stdio.h>

void main(){
printf("test");
}


see it working here http://codepad.org/QZnYTR5o


#include <stdio.h>

void main(){
if(printf("test")){}
}


see it working here http://codepad.org/cKnS605C


As you can see both the outputs are same.


The second program has no semicolons, the same can be achieved using Loops but i will leave it to you to figure it out. :-)

No comments:

Post a Comment