2 ms·
> Write a C program which prints Hello World! without using a semicolon Here's one way, cheating a bit by abusing the definition of "a C program which prints":
by JoshTriplett 11y ago
> Write a C program which prints Hello World! without using a semicolon
Here's one way, cheating a bit by abusing the definition of "a C program which prints":
/tmp$ gcc hw.c
hw.c:1:2: warning: #warning Hello world! [-Wcpp]
#warning Hello world!
A more serious solution, though:
/tmp$ cat hw.c
#include <stdio.h>
void main(void)
{
if (puts("Hello world!")) {}
}
/tmp$ gcc hw.c
/tmp$ ./a.out
Hello world!
- nathell 11y agoHow about this one? It's not standards-compliant, but GCC swallows it even with -Wall (or at least it used to), and it's by far my favourite: int main(int args, char *argv[printf("Hello, world!")]) { }
- cremno 11y agoNice one but: >It's not standards-compliant Why? It's just a VLA.
- nathell 11y agoPer 5.1.2.2.1 [C11 Final Draft], the `main` function should be declared as either int main(void) { /* ... */ } or int main(int argc, char *argv[]) { /* ... */ }
- opcvx 11y agoYou missed the relevant text: >or equivalent; 10) >10) Thus, int can be replaced by a typedef name defined as int, or the type of argv can be written as char * * argv, and so on. And Standard also says that: >or in some other implementation-defined manner. and: >In a freestanding environment (in which C program execution may take place without any benefit of an operating system), the name and type of the function called at program startup are implementation-defined. So main can be pretty much anything according to the Standard, depending on the system.