I just made a 'Hello World' program

#include<stdio.h>
int main()
{
	printf("Hello World\n");
    return 0;
}
exe file size    = 15.5 KB
Source file size = 79.0  B

Then i made a data structure program (implementing stack, queue, deque)

exe file size    = 21.9 KB
Source file size = 17.3 KB

So i guess it's not linearly dependent with its source file
So what really decides the size of an exe file.
PS - I think it is also compiler dependent 'coz exe files on turbo c++ are larger compared to those of code blocks

Dani AI

Generated

Good point to ask — the source-file byte count is almost never the thing that determines an EXE’s size. ’s experiment exposed the symptom; and were right to point at the toolchain and build choices. Below is a compact, practical breakdown of what actually adds bytes and a short workflow to find and trim the big contributors.

What typically contributes most

  • The runtime and libraries that get linked into the binary (static CRT vs shared DLLs, third‑party libs).
  • Compiler code generation: inlining, template expansion and how the compiler implements C++ features.
  • Linker output: unused-code elimination, identical‑folding, and section alignment/padding.
  • Language/runtime features: iostreams, heavy STL/template use, exceptions, RTTI and global constructors add code.
  • Embedded resources, manifests and code signatures.
  • Debug / symbol data and other metadata (PDBs or embedded symbols).

Quick diagnostic workflow

  • Build a release build and produce a map file so you can see which object/library contributes most.
  • Inspect the binary’s sections and imports (Windows: dumpbin /headers and /imports; ELF: readelf -S / objdump -h).
  • Example shrink-builds (GCC/MinGW and MSVC):
    gcc -Os -ffunction-sections -fdata-sections -Wl,--gc-sections -Wl,-Map=app.map -o app.exe main.c
    cl /O1 /GL /Gy /MD main.c /link /LTCG /OPT:REF /OPT:ICF /MAP:app.map
  • Read the produced map and the linker output to find the biggest objects or libraries.

Practical ways to shrink an EXE (tradeoffs noted)

  • Strip symbols for release, use optimization-for-size (-Os or /O1), enable function/data sections + gc-sections, and prefer the shared CRT to avoid embedding the whole runtime.
  • Avoid heavy C++ features or large template instantiations if size matters, or move them to a DLL.
  • For embedded assets: on Windows use a resource script + resource compiler (rc.exe / windres) or convert files into C arrays/objects and link them.
  • As a last step consider packers (UPX) but expect antivirus false positives and harder debugging.

Use the map and section dumps first — they show the exact guilty objects and let you make focused changes.

Recommended Answers

All 5 Replies

>>So what really decides the size of an exe file.
Your compiler. Two different compilers using the same identical code can produce different exe sizes. And the same compiler can produce different exe sizes depending on the flags you give it, such as DEBUG and UNICODE, and the way it might optimize or not optimize the code.

And if you include 'resource files' to your application, the size will go up about the size of the files you include in it. As AD said, it's very often different because of the compiler and settings.

And the same compiler can produce different exe sizes depending on the flags you give it, such as DEBUG and UNICODE

What are these DEBUG and UNICODE? Can you give me an example?

And if you include 'resource files' to your application, the size will go up

By resource files, do you mean to say header files?

What are these DEBUG and UNICODE? Can you give me an example?

If the compiler is set up to produce a debug executable (to allow the programmer to debug the program easier), then there's more information and instructions packed into the executable which would increase its size.

UNICODE is a type of character, normally a character in C++ is one byte, but if UNICODE is defined, then you would be using a two byte characters (capable of holding thousands more characters to support chinese and other types of symbols.

By resource files, do you mean to say header files?

By resource files, I mean files with the .res extensions which allow you to internally attach files such as images or videos into the executable. Nothing to do with header files.

The file size is dependant on so many things, If i statically link all the applications dependencies, then the program can turn from 10kb to 60kb.

UNICODE is a type of character, normally a character in C++ is one byte, but if UNICODE is defined, then you would be using a two byte characters (capable of holding thousands more characters to support chinese and other types of symbols

Ohh, that UNICODE! Thanks +1

I mean files with the .res extensions which allow you to internally attach files such as images or videos into the executable.

I don't know much about them (time to Google!) but can this(.res files) be done using just a C compiler? Like we do in HTML.

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.