-1

I am working on a project in C, in which I am thinking to cut short stdio.h header file and only keep the code which I need.

How shall I do this? Plus I would like to know whether cutting it short will make any difference to the speed?

ndim
  • 35,870
  • 12
  • 47
  • 57
  • 7
    There's no benefit in doing this. The optimizer will produce an assembly that only includes used parts. – Hatted Rooster Sep 10 '17 at 19:41
  • 2
    That's absolutely pointless. stdio.h contains very little, basically just declarations, the corresponding definitions of which have already been compiled into the standard library. It's a piece of cake for a compiler to race through it. – Petr Skocik Sep 10 '17 at 19:42
  • 1
    not to mention that if you copy the relevant parts, changing compiler can change the original definition and your code will break – Jean-François Fabre Sep 10 '17 at 19:43
  • What reason do you have for doing this? As others have said, if there is any benefit to doing this, it's not worth the time you spend debugging everything you break. Plus, you might change your code later and suddenly need the code you removed. –  Sep 10 '17 at 19:46
  • What kind of speed are you talking about? The only speed it could possibly affect is compilation speed, not runtime speed. – melpomene Sep 10 '17 at 19:48
  • At best, will only reduce the time to compile your program. It will not make your resulting code more efficient. Probably the compromise here is to simply avoid including `stdio.h` in the source files that don't need it. Otherwise, not worth the effort. – selbie Sep 10 '17 at 19:49
  • 2
    How did you determine that including `` is your performance bottleneck? What kind of benchmark are you using? – melpomene Sep 10 '17 at 19:50
  • @ArpitaSharma Which parts of `stdio.h` do you intend to use? – SBS Sep 10 '17 at 20:02
  • It may make a difference in compilation times but you won't notice it. A typical program reads in between 1K to 15K lines of headers. Even on a 20 year old 300MHz P3, this is over in less than 1s. If the compiler has a precompiled header option, use that. – cup Sep 11 '17 at 05:19
  • Well... stdio.h _is_ most often a bottleneck. The solution is to not use it in the first place, rather than to try to meddle with its internals. Replacing stdio.h calls with pure API calls will speed up the program considerably. At the cost of portability. – Lundin Sep 11 '17 at 14:51

1 Answers1

2

Header files are interface files. They (usually) do not contain implementation code which would bloat your application. They are like a table of contents for the compiler into library files named such as crt.o or libc.so.

If you want, you can try forward-declaring standard functions by yourself, for example:

extern int printf( const char *, ... );

This practice is frowned upon and may not be supported very well. Note that such functions may be defined in standard headers as macros (as well as by the standard library binary file as extern symbols), so there may be some difference in performance or executable size, compared to the usual #include.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • There's a rule somewhere about "any function that can be declared without library types" – o11c Sep 10 '17 at 21:56
  • @o11c Yes, I also can't be bothered to look it up. I seem to recall that the C committee isn't very happy about this rule but don't remember whether it's being deprecated. – Potatoswatter Sep 10 '17 at 22:34
  • Thank you, I have dropped my idea to redesign it. Though its a part of a small project that I am working on . Basically in that project, I am making R libraries in C , so as to perform statistical functions on data set using those libraries. – Arpita Sharma Sep 18 '17 at 19:44