On my forum I go to we were talking about how some peoples C++ hellowords were 10x larger than the C version.On my GCC and G++ there is no difference but on some of theirs there is a huge one.If any of you wouldn't mind could you compile
C

#include<stdio.h>

int main()
{
   printf("HelloWorld\n");
  
   return 0;
}

and C++

#include<iostream>

int main()
{
   std::cout<< "HelloWorld\n";

   return 0;
}

and post both sizes and which compiler was used to do this test.

Recommended Answers

All 4 Replies

Even with a single compiler, whether you choose to build the debug or release version, or whether you choose static or dynamic linking with the run-time library can have huge impact on the (apparent) size of the executable.

What's your point?

This isn't going to turn into another "x is better than y because" threads is it?

It might also depend on how the compiler implemented cout. Microsoft compilers cout is just a thin wrapper for printf(), that is, both cout and printf() call the win32 api function WriteFile(). I have no idea how *nix compilers implement the two functions.

No I don't mean ones better than the other. Mine are the same and the creator of C++ says they should be the same too.I wanted to know which ones were showing this size problem.And i mean just a basic compile no debug info or optimizations. I'm not looking to compare C and C++ as a language just the size of helloworld. Note it appeared that most of these problems were on cygwin users .

it all depends on the compiler, the linker, and the options you set for them.
A suboptimal compiler or linker may link in a lot of stuff that's not needed, an optimising one may remove all that.
Runtime performance won't be affected by that (except maybe application loading times, which for a "hello world" style application will be a significant part of the total execution time of course), only storage space on disk will be.

The rest depends as already mentioned on the specific implementations in machine instructions created by the supplier of the libraries you use.
Theoretically the two programs you showed could be compiled into identical machine instructions, so theoretically there is no reason why the executable (or its image in memory) should of necessity be of different size.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.