11

I'm trying to understand why the output file sizes are significantly different when using a C and a C++ compiler.

I was writing a small hello world program in C and C++, I noticed that in C version, the size of the executable was 93.7KB and in C++, the size of the same hello world program was 1.33MB. I am not sure why that is. I think it may be because C++ has more libraries and namespaces to use so I removed the using namespace std line and simply used std::cout and still the result was the same.

C

#include <stdio.h>

int main()
{
    printf("hello world");
    return 0;
}

// size 93.7KB

C++

#include <iostream>

int main()
{
    std::cout<<"Hello world";
    return 0;
}

// size 1.33MB

There doesn't seem to be much difference in the code above. Is there some sort of compiler difference that creates the differing file sizes?

Hawk
  • 349
  • 2
  • 4
  • 10
  • why didn't you ask at Stack Overflow? http://meta.stackexchange.com/a/129632/165773 – gnat Jun 26 '14 at 16:03
  • didn't know where to ask..thought i'd ask here – Hawk Jun 26 '14 at 16:04
  • so should i delete the question and post it there @gnat – Hawk Jun 26 '14 at 16:05
  • 7
    Sort of explains why C is used a lot on embedded devices. – Robert Harvey Jun 26 '14 at 16:07
  • 2
    Also, it would be a fairer test if you were to write the same exact code in both languages. Even then, I'm not sure that would rid you of the difference, since C++ is much more complicated to compile (and thus would be done differently). – Panzercrisis Jun 26 '14 at 16:07
  • so is it better if i learn C or C++?? i mean which is more powerful? – Hawk Jun 26 '14 at 16:08
  • cos I've noticed people talking about C being lower level and more closer to hardware systems but C++ has classes whereas C doesn't..I am confused.. – Hawk Jun 26 '14 at 16:08
  • @Hawk C++ is more powerful by a long shot (partially because C is almost completely contained in it). C++ is better for you to learn for general intents and purposes, though C is better for things like embedded devices. – Panzercrisis Jun 26 '14 at 16:09
  • @Panzercrisis, is there a possibility of interaction with other languages like there is in C..?? – Hawk Jun 26 '14 at 16:11
  • 3
    as far as I can tell, this has been asked and answered at SO: [Why is a C/C++ “Hello World” in the kilobytes?](http://stackoverflow.com/questions/11815005/why-is-a-c-c-hello-world-in-the-kilobytes) @RobertHarvey FWIW Java ME [CLDC 1.0](http://en.wikipedia.org/wiki/CLDC) footprint is per my recollection 128K, not too much even for end '90s embedded devices – gnat Jun 26 '14 at 16:12
  • 2
    @Hawk The fact that C++ has classes and all the other stuff associated with them is a large part of the reason that C is lower level and closer to the hardware. Still you can usually do the same things in C++ as you can in C. Since C is basically a subset of C++ - without a lot of the features in C++ - it takes longer to write big, huge programs, but it will run somewhat faster. As for interacting with other languages...do you mean like writing assembly language into the C++ code? Yeah, C++ supports that, but I'm not sure if C does or not. – Panzercrisis Jun 26 '14 at 16:14
  • got it @gnat, that was what I thought..needed confirmation..but I still remain a skeptic on whether to choose C or C++ considering that most languages are written in C. – Hawk Jun 26 '14 at 16:14
  • @Panzercrisis, I don't code Assembly but since PHP is written in C, would C++ do almost the same as C if combined with PHP? – Hawk Jun 26 '14 at 16:16
  • question on [what technology to take up next](http://meta.programmers.stackexchange.com/a/6486/40980 "here is a detailed explanation why questions like this tend to be closed and downvoted") would be closed here – gnat Jun 26 '14 at 16:16
  • 1
    What compiler are you using? I get 6.7K and 7.9K respectively, which is comparable to the question gnat linked. I'm thinking your compiler choice and/or settings are having a larger effect than the language in this case. – Karl Bielefeldt Jun 26 '14 at 16:41
  • 6
    There is no question here. Furthermore, even if there was, that question would be unanswerable without specific knowledge about compiler and compilation flags used. The only way to get a precise answer to a question *why* C++ executable would be so much bigger and what exactly takes up the bytes would be to use some kind of a binary analysis tool (e.g. readelf on Unix systems, part of GNU binutils) or a hex editor to investigate file sections and contents. What comes to *languages* themselves, there's absolutely no reason for a difference like that. C++ output could be just as small as C. – zxcdw Jun 26 '14 at 18:30
  • Assuming he has no idea of compiler flags, etc, etc, the answer by glampert neatly explains the size difference. – iheanyi Jun 26 '14 at 19:41
  • With Fabrice Bellard's "Tiny C compiler", the hello world program even needs only 2036 Bytes (type `tcc hello.c` in http://bellard.org/jslinux/ and inspect `a.out` in `ls -ls` – rplantiko Jun 26 '14 at 20:03
  • 3
    Comparing "Hello World" programs is almost entirely meaningless as no one optimizes compilers for trivially small programs. It is very likely that your C++ compiler has flags that, if you switch to `printf` (available in C++) will produce a file of similar size to C (for example, `-nostdlib` and `-nodefaultlibs`. – Gort the Robot Jun 26 '14 at 22:37
  • 1
    It's not "the same hello world program", they are two different programs. You should compile the C program with a C++ compiler and see what happens. People tend to assume that for a program to be in C++ it *has* to use streams and not printf. – Petruza Jul 04 '14 at 02:20
  • 1
    Actually streams are not a C++ language feature as many believe, they're just template classes with the bitshifting operators << and >> overloaded. – Petruza Jul 04 '14 at 02:26
  • Possible duplicate of [Why are the sizes of programs so large?](http://programmers.stackexchange.com/questions/298117/why-are-the-sizes-of-programs-so-large) – gnat Aug 16 '16 at 10:38

5 Answers5

24

Most of the C++ standard library, including all the streams which cout is part of, are inline template classes. Whenever you #include one of these inline library components, the compiler will copy and paste all that code to the source file that is including it. This will help the code to run faster, but will also add a lot of bytes to the final executable. This is likely the reason for the results you got.

Doing a similar test with the clang compiler on OSX (Apple LLVM version 5.1), using default flags, I got comparable results:

hello_cpp_cout:

#include <iostream>
int main()
{
    std::cout << "Hello world" << std::endl;
    return 0;
}

Size: 14,924 bytes

hello_c:

#include <stdio.h>
int main()
{
    printf("hello world\n");
    return 0;
}

Size: 8,456 bytes

And, as a bonus, I tried to compile a .cpp file with the exact same code as hello_c, i.e.: using printf instead of cout:

hello_cpp_printf:

#include <stdio.h>
int main()
{
    printf("hello world\n");
    return 0;
}

Size: 8,464 bytes

As you can see, the executable size is hardly related to the language, but to the libraries you include in your project.

Update:

As it was noted by several comments and other replies, the choice of compiler flags will also be influential in the size of the compiled executable. A program compiled with debug flags will be a lot larger than one compiled with release flags, for example.

glampert
  • 1,131
  • 1
  • 9
  • 13
  • 1
    Doesn't really explain a 1.33MB file size, though. – Robert Harvey Jun 26 '14 at 21:06
  • 1
    He must be using Visual Studio :P – glampert Jun 27 '14 at 01:17
  • thanks, dude..i'm using dev c++ and it did work.. you're right, the libraries indeed do give too much space – Hawk Jul 02 '14 at 07:59
  • I compiled these examples with VS2015 x64 and the results support this answer. Release: hello_cpp_cout 12288 Bytes, hello_c 10240 Bytes, hello_cpp_printf 10240 Bytes. Debug: hello_cpp_cout 56320 Bytes, hello_c 51200 Bytes, hello_cpp_printf 51200 Bytes. – pschill Sep 13 '19 at 14:03
8

There doesn't seem to be much difference in the code above.

Yes there is. It's a totally different code. The c++ iostream library relies heavily on template which creates more inline code and so the C++ executable is bigger.

Another reason is that you did not remove debug sympols from the executable files, and for C++ the symbols are quite verbose. If you are under linux, you can use the "strip" command to remove those symbols and the executable size will shrink.

Nikko
  • 652
  • 6
  • 14
5

The difference in executable sizes will be heavily dependent on what type of linkage is specified, optimisations and the compiler(s) being used.

Given the significant difference in final sizes, it looks like the C++ variant is being statically linked with the runtime. Remove or change that and you should see a drop in the C++ size; for gcc (g++) look for --static* et. al and for msvc /MD and /MT

Niall
  • 1,831
  • 1
  • 18
  • 20
2

There is actually quite a lot of difference. The C code uses unformatted, unlocalized output. The C++ equivalent has quite a lot of gunk in it for changing locales and that kind of thing. Functionally, they are far from equivalent. You just happen to be using only a tiny subset of the C++ interface.

However more generally, the larger code sizes are a defect of this particular portion of the Standard library in particular, which is well known to be over-engineered, slow, and large, rather than the C++ language in general.

DeadMG
  • 36,794
  • 8
  • 70
  • 139
-1

With CMake MinSizeRel BUILD_TYPE cpp is 8744, while c is 8296 bytes. Not that much difference also if you are in an embedded environment.