Just for fun, I decided to try to write a valid C++ program without headers that printed "Hello world!" Took my compiler's source for stdio.h, substituted it directly into main.cpp, took the source for every header in there, substituted it directly instead of the #include for that header, and so on. I don't feel like posting it because
a) It's super long
b) It's a macro/#if soup
c) I don't know if the license would allow it
But anyways, I was just wondering if anyone had done anything of this sort before.
Why did you go through all that trouble? It isn't hard to "print "Hello world!" without headers", all you have to do is not put any headers in the "Hello world!" text string and you're good to go ;)
The headers just give you forward declarations so that your compilers knows that a function exists and can look for it somewhere. For external libraries, you'll just need to link that library. For standard libraries, they are already linked.
If you look how printf is defined, you'll see it is something like this: int printf(constchar* _Format,...);
So just write this:
1 2 3 4 5 6
int printf(constchar* _Format,...); // forward declaration
int main()
{
printf("Hello world!");
return 0;
}
If you don't use #include or a forward declaration, it may still work because the compiler will usually try to guess. If you left the forward declaration out of Stewbond's example, the compiler would probably generate one like int printf(constchar*);, and if he had done printf("%s\n", "hello, world"); it'd probably generate int printf(constchar*, constchar*);. It works because printf() accepts a string followed by any other arguments, so whatever the compiler generates will be valid (for printf() in these examples - YMMV).