-1

I wrote this in notepad and then compiled it with lcc-win, using the command lc hello.c

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

The resulting exe was 100 KB. Seems kind of huge for a program that just prints Hello World. Is this normal? Can I reduce the size? 100 KB isn't really an issue these days but it still seems kind of big for what it does. Wouldn't be too bad if every C code I write comes out as a 100 KB exe though.

MxLDevs
  • 19,048
  • 36
  • 123
  • 194
  • possible duplicate of [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) – John Carter Aug 16 '12 at 07:28
  • I fail to understand why you would use lcc, when there is mingw available for the last 10 years – Ulterior Aug 16 '12 at 07:29
  • Does it make a difference which compiler I use? Anyways, lcc was small to download and quick to setup. All I needed was a C compiler that runs on windows. Does mingw's gcc compile exe's that run on windows machines without requiring some sort of dll unlike the way cygwin does? – MxLDevs Aug 16 '12 at 07:41
  • No, it shouldn't make much of a difference what compiler you use. What you see augmenting the size of an executable for C (compared to java e.g) is that it must do all the startup itself. Interpreted languages such as java have their VM doing this for them. But for sensibly larger code, this overhead should be marginal. – Jens Gustedt Aug 16 '12 at 07:48
  • compile it with BCC64 static link and you will get 8MB executable – M.M Aug 30 '14 at 12:36

2 Answers2

0

1- Everytime you use the include <> tag you do make a link with a c library and load it in your programm. That is also why it is important to include only in the files that actualy need the library functions.

2- On the other part, the binary that you generate is always full of important informations (cf : libelf or ASM), headers, steps that needs to be here if you want to programm to be run nicely. This does take space to.

C404
  • 243
  • 4
  • 14
  • 1
    Eh, no, using an `include` directive does not control which libraries that are linked into the binary. – HonkyTonk Aug 16 '12 at 08:50
  • @HonkyTonk it could if the included file has a pragma directive to link a particular library – M.M Aug 30 '14 at 12:37
0

This is a really simple question, what happens to the lcc-win is the same with the C Compiler Digital Mars, he do not link the exe with dlls containing the functions printf and etc., functions are linked together with EXE, so no requerindo that your computer has the DLLs.

Look, I created a simple Hello World EXE, and I opened hin in Hex Editor.... the printf function is stored in msvcrt.dll, and, the exe don't have this dll in import section...

enter image description here

And, u can found the source definition in this other picture:

enter image description here

Use this style of function definition is more fast then make a dll call....

Alexandre
  • 1,985
  • 5
  • 30
  • 55