-2
#include<assert.h>
#include<stdio.h>
**#include<stdlib.h>**
#include<string.h>
**#include<stdlib.h>**
#include<time.h>

stdlib.h is included for two times, but why?

Zhou Tai
  • 214
  • 1
  • 10
  • 8
    Copy and paste from another code base without checking? Forgetfulness? – Jonathan Potter Mar 18 '15 at 01:34
  • 2
    You certainly don't need to. If you delete the later one, it is almost certainly going to work. – merlin2011 Mar 18 '15 at 01:34
  • as far as i know, the preprocessor ignores duplicate headers, so no need to worry about it – mangusta Mar 18 '15 at 01:35
  • Multiple includes are useful for code generation but in this case, it's probably just result of not-thoroughly-checked copy&paste or merge. – StenSoft Mar 18 '15 at 01:36
  • 2
    I always try to sort my includes alphabetically, which makes this error a lot easier to spot and harder to make. But, it's also harmless. – Greg Hewgill Mar 18 '15 at 01:37
  • 4
    @mangusta No, it does not. It's [include guards](https://en.wikipedia.org/wiki/Include_guard) that are used against duplicit code inclusion, preprocessor is dumb and happily includes it. – StenSoft Mar 18 '15 at 01:38
  • There is exactly one non-idempotent standard header in C: ``, and in C++ additional the C++ adaptation ``. Even for those, multiple inclusion does not make sense without changing `NDEBUG` between them. – Deduplicator Mar 18 '15 at 01:41
  • because the original author wasn't paying attention. – Captain Obvlious Mar 18 '15 at 02:18
  • 5
    I'm voting to close this question as off-topic because there is no way to determine what the author was thinking when they wrote the code. – Captain Obvlious Mar 18 '15 at 02:20
  • This code is part of a file in QFS. – Zhou Tai Mar 18 '15 at 02:23

1 Answers1

3

There is exactly one non-idempotent standard header in the C standard: <assert.h>.
C++ inherits that and adds its own adaptation: <cassert>.

Even for those, multiple inclusion has no effect without changing NDEBUG between them.

Your own headers, as well as headers from other libraries you might use, should use include guards (or the unportable but often working #pragma once).

If you sort your headers (include the implementation-files own header first though), you can easily eliminate duplicates and don't need to rely on that.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118