No headers?

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.
Last edited on
Sure, although I just make the compiler output the preprocessed source and use that.
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 ;)
How so? Something like

1
2
3
4
int main()
{
    printf("Hello world!");
}


If so, my compiler doesn't like using an undeclared printf.
It is kind of missing the point to do it that way, since #include does exactly what you did by hand, plus some.

What about something like:
1
2
3
4
5
6
int main()
  {
  int puts( const char* );
  puts( "Hello world!" );
  return 0;
  }

When you compile, just make sure to link against libc:

    gcc a.c -lc

(MinGW users, this won't work, since MinGW links against msvcrt instead of glibc or libc.)

[disclaimer]
I didn't actually try this with an actual compiler...
I didn't know that C++ would actually work without headers!
It's obvious if you know what #include is for.
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(const char* _Format,...);

So just write this:
1
2
3
4
5
6
int printf(const char* _Format,...); // forward declaration
int main()
{
    printf("Hello world!");
    return 0;
}
You could also do it with inline assembly.
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(const char*);, and if he had done printf("%s\n", "hello, world"); it'd probably generate int printf(const char*, const char*);. 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).
Last edited on
Whovian wrote:
a valid C++ program that printed "Hello world!" without headers
1
2
3
4
5
6
#include <iostream>

int main()
{
    std::cout << "Hello world!";
}
Here is the opposite; with headers:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main()
{
    std::cout << "HTTP/1.0 200 OK\n"
                 "Date: Thu, 31 May 2012 15:35:25 GMT\n"
                 "Server: Apache/2.2.3 (CentOS)\n"
                 "X-Powered-By: PHP/5.1.6\n"
                 "X-CodeHQ-Source: Unit\n"
                 "Content-Length: 12\n"
                 "Content-Type: text/html\n"
                 "X-Cache: MISS from ProxyD\n"
                 "X-Cache-Lookup: MISS from ProxyD:8857\n"
                 "Via: 1.0 ProxyD (squid/3.1.19)\n"
                 "Connection: keep-alive\n"
                 "\n"
                 "Hello world!";
}


EDIT: Perhaps the original author meant this instead?
Whovian wrote:
a valid C++ program without headers that printed "Hello world!"
Last edited on
Ah. Sorry. That's what I meant. One without headers. firstpost.edit() And yes, I do know what headers are for.

@Stewbond Nah, my compiler sucks enough to not be able to guess what I mean by printf, and I haven't told it what to do with printf yet.
@L B,
I lol'd, although CGI programs don't need to print all that; all you'd need to print from a CGI program is
Content-Type: text/plain

Hello world!

The server will handle everything else.
@LB
LOL! Nice!
closed account (zb0S216C)
Numeri wrote:
"I didn't know that C++ would actually work without headers!"
1
2
3
4
int main()
{
    return(0);
}

It's valid C++, and it compiles.

Wazzak
Topic archived. No new replies allowed.