-

2011年8月4日星期四

How you create a c program without semicolon?

-include<stdio.h>

void main()

{

if(printf("hai"))

{}

}Hi,



That is a rather odd question you have there. Get's you thinking.



There are a few ways to assign special characters in the Shell commands, for example:

聽聽聽gcc -Wall -W -pedantic -DSPECIAL=; main.c -o main



聽聽聽cl -DSPECIAL=; main.c



However, while these are legitimate, they use a semicolon LoL ~ so thinking you won't be allowed to do that.



However, while its in my opinion bad coding there are in fact three simple ways to write a C program without using a semicolon. Examples, below:



void main() {

聽聽if(printf("Hello world")) {

聽聽}

}





void main() {

聽聽while(!printf("Hello world")) {

聽聽}

}







void main() {

聽聽switch(printf("Hello world")) {

聽聽}

}





Actually you could even do this:



#include <iostream>



int main() {

聽聽if (std::cout << "Hello world!" << std::endl) {

聽聽}

}



You can get away with that 4th choice because Compilers default to a return 0 when no return in main(), plus I did not use namespace std because then you would need a semicolon.



The other three ways above work also, but all I can saw is gross LoL. I wouldn't use any of these four methods but for the challenge of answering your question.



Thanks for the challenge though.



Regards,



Javalad
This is impossible, because the ';' tells the compiler that the statement is complete.

A ; must be placed after each primary expression, prototype or definition.

A possible way to do this is to set a macro (not sure it will work tho)

Add something like

#define STATEMENT_END ;

in the beginning, and use that instead, it WILL look ugly tho:

priintf("hello!")STATEMENT_END

没有评论:

发表评论