Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
3  
Sort of explains why C is used a lot on embedded devices. –  Robert Harvey Jun 26 at 16:07
1  
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 at 16:07
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? @RobertHarvey FWIW Java ME CLDC 1.0 footprint is per my recollection 128K, not too much even for end '90s embedded devices –  gnat Jun 26 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 at 16:14
5  
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 at 18:30

4 Answers 4

up vote 13 down vote accepted

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.

share|improve this answer
    
Doesn't really explain a 1.33MB file size, though. –  Robert Harvey Jun 26 at 21:06
    
He must be using Visual Studio :P –  glampert Jun 27 at 1: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 2 at 7:59

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.

share|improve this answer

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

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.