2

I've noticed that the size of a simple C program that prints "hello world":

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

is approximately 8kB. I thought that it might be because of the include, then I've generated a simpler program:

int main()
{
    int x=1+13;
    return x;
}

But again the size of the compiled program was ~8kB.

Why is that so large? If a machine instruction is 8B then there are about 1k instructions, but I don't see any reason why such simple code will result in such many instructions.

Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
MRm
  • 517
  • 2
  • 14
  • 1
    It changes how you define the 'such simple'. – tango-1 Sep 20 '20 at 14:52
  • 1
    It depends on your linker settings mostly (data section default size, etc). – goodvibration Sep 20 '20 at 14:53
  • Related: https://stackoverflow.com/questions/13487113/is-there-a-method-function-to-get-the-code-size-of-a-c-program-compiled-using-gc and https://softwareengineering.stackexchange.com/questions/246167/why-would-a-c-executable-be-smaller-when-compared-to-c-executable – gsamaras Sep 20 '20 at 14:54
  • 7
    The executable file is not just instructions. First, it has a header telling the linker what sections are in the file, along with where they are and what their properties are. For example, there may be sections for read-only data, uninitialized data, initialized non-read-only data, instructions, and more. There is also a table of symbols that need to be resolved when the program is loaded and locations where those symbols are used. (Data at those locations needs to be adjusted by the loader or dynamic linker.) – Eric Postpischil Sep 20 '20 at 14:55
  • 1
    Additionally, including `` brings in a number of facilities even if you do not use all of them. It has to provide various data and routines for processing streams, formatting output, matching input, and so on. (Those routines might not be linked in if you do not use them, depending on the structure of the library, but there is a core amount of data and routines that are included.) Another thing that is linked in implicitly is the C run-time startup code, which runs before `main` and sets up a number of things needed for the C environment. – Eric Postpischil Sep 20 '20 at 14:55
  • 1
    https://www.muppetlabs.com/~breadbox/software/tiny/teensy.html is an interesting read. It gets very technical, but uncovers some of the sorts of things which are actually in a tiny program (on Linux). – aschepler Sep 20 '20 at 14:58
  • 5
    There's a bunch of startup code that runs before `main` is invoked. See [crt0](https://en.wikipedia.org/wiki/Crt0) – Tom Karzes Sep 20 '20 at 14:58
  • Keep in mind also that this is just the program code. It will likely be statically linked against any libraries. More importantly, it needs the entirety of the linux kernel to execute. So quite a bit goes into these "trivial" programs that isn't even part of the program. – erik258 Sep 20 '20 at 15:00
  • 1
    @EricPostpischil, actually, on my Debian and gcc, just including the header it doesn't really do anything for the end results, the binaries I get are exactly the same size with or without the include. (with the sample codes here, that is, skipping the declaration for `printf` probably could cause issues but it works for me here.) The C library is linked in with or without the explicit include. – ilkkachu Sep 20 '20 at 15:10
  • https://man7.org/linux/man-pages/man1/readelf.1.html readelf shows the contet of an elf – erik258 Sep 20 '20 at 15:17
  • 2
    Your question is missing the compiler options you used. Perhaps the binary isn't stripped? – Murphy Sep 20 '20 at 16:11
  • Does this help? https://stackoverflow.com/questions/63669125/what-is-in-an-executable-besides-the-raw-machine-instructions/63675550#63675550. The short answer is that executable files seem large, for small programs, because there's no real need to make them smaller. – Kevin Boone Sep 20 '20 at 17:03
  • It would be interesting to go through the entire file and identify the purpose of every byte, but that would be a topic for an article - a bit much for an SO answer. – Nate Eldredge Sep 20 '20 at 17:52

0 Answers0