c++ c++-faq implementation-defined-behavior portability turbo-c++

Why should I not #include ?


I posted a question with my code whose only #include directive was the following:

#include <bits/stdc++.h>

My teacher told me to do this, but in the comments section I was informed that I shouldn’t.



  • 129

    Huh. I should have known there would be an include version of using namespace std; out there somewhere.

    Aug 5, 2015 at 0:47

  • 5

    why does this header even exist? surely none of the standard includes actually include this, since it would bring in lots of junk? and if its not included by any of the public includes… then why is it shipped in the distribution?

    Feb 1, 2016 at 8:42

  • 16

    @ChrisBeck: It’s an implementation detail. It’s not part of the “public API” or meant for use. But it still has to be shipped otherwise nothing would work. The standard includes may not use it individually but it’s there for use in precompiled headers. See the comment at the top, which says: “This is an implementation file for a precompiled header.”.

    Feb 1, 2016 at 10:41

  • 4

    @LightnessRacesinOrbit “It’s not part of the “public API” or meant for use.” Entirely wrong, it’s intended for public use, as a precompiled header. Libstdc++ (pre)compiles and installs a precompiled version of that header, so if you include it then G++ will actually include bits/stdc++.h.gch instead, the precompiled version. It exists because it has to exist so that the precompiled version of it can be generated.

    Aug 7, 2019 at 16:00

  • 4

    @JonathanWakely The public API of the standard library, I mean; not of libstdc++. The people using it on Stack Overflow every day (and in their projects) are not using it for the reason, or in the use case, that you’re talking about. Sure, my comment could have been worded more accurately, though note that I did point out its use for precompiled headers. Feel free to write a competing answer.

    Aug 7, 2019 at 16:06


Including <bits/stdc++.h> appears to be an increasingly common thing to see on Stack Overflow, perhaps something newly added to a national curriculum in the current academic year.

I imagine the advantages are vaguely given thus:

  • You only need write one #include line.
  • You do not need to look up which standard header everything is in.

Unfortunately, this is a lazy hack, naming a GCC internal header directly instead of individual standard headers like <string>, <iostream> and <vector>. It ruins portability and fosters terrible habits.

The disadvantages include:

  • It will probably only work on that compiler.
  • You have no idea what it’ll do when you use it, because its contents are not set by a standard.
  • Even just upgrading your compiler to its own next version may break your program.
  • Every single standard header must be parsed and compiled along with your source code, which is slow and results in a bulky executable under certain compilation settings.

Don’t do it!

More information:

Example of why Quora is bad:


  • 111

    “perhaps something newly added to a national curriculum in the current academic year” Blind leading the blind 🙁

    Sep 4, 2015 at 22:24

  • 58

    Just came here through a wormhole in another question, very good. What makes this teaching habit worse is that it is usually followed by a direct using namesapce std;. Just two lines in and virtually every nice identifier is used. Incredibly frustrating to see it being taught.

    Jun 27, 2017 at 8:27

  • 13

    About the quora example, it might have shifted with time. I’ve visited the page today and both pros and cons of <bits/stdc++.h> where listed in the specific context of online programming contests. I find their conclusion ok-ish.

    – YSC

    Feb 6, 2018 at 14:47

  • 9

    @EvgeniSergeev: 2KiB is a lot of code, data, symbol information, etc., when trying to determine its effect. Do you understand everything that is being added? For your compiler? The current release? All releases in between? All future releases? If you need to decide between convenience and correctness, there is only a single valid option.

    Aug 25, 2018 at 7:48

  • 5

    Hopefully not a curriculum but rather just a cargo-cult spread across ‘contest’ websites or something… although sadly nothing would surprise me either way.

    Nov 19, 2018 at 23:09


Why? Because it is used as if it were supposed to be a C++ standard header, but no standard mentions it. So your code is non-portable by construction. You won’t find any documentation for it on cppreference. So it might as well not exist. It’s a figment of someone’s imagination 🙂

I have discovered – to my horror and disbelief – that there is a well-known tutorial site where every C++ example seems to include this header. The world is mad. That’s the proof.

To anyone writing such “tutorials”

Please stop using this header. Forget about it. Don’t propagate this insanity. If you’re unwilling to understand why doing this is Wrong, take my word for it. I’m not OK being treated as a figure of authority on anything at all, and I’m probably full of it half the time, but I’ll make an exception in this one case only. I claim that I know what I’m talking about here. Take me on my word. I implore you.

P.S. I can well imagine the abominable “teaching standard” where this wicked idea might have taken place, and the circumstances that led to it. Just because there seemed to be a practical need for it doesn’t make it acceptable – not even in retrospect.

P.P.S. No, there was no practical need for it. There aren’t that many C++ standard headers, and they are well documented. If you teach, you’re doing your students a disservice by adding such “magic”. Producing programmers with a magical mindset is the last thing we want. If you need to offer students a subset of C++ to make their life easier, just produce a handout with the short list of headers applicable to the course you teach, and with concise documentation for the library constructs you expect the students to use.


  • 2

    That well known site is that the one where every C++ example looks like a C program?

    – Surt

    Dec 20, 2020 at 11:57

  • 5

    He is talking about GeeksForGeeks

    – SuperNoob

    Mar 15, 2021 at 14:42

  • 5

    It will be fun when that header is included, along with using namespace std;. Then simple things like a user-defined gcd, swap or variable named data will act strangely or not compile at all, leaving the coder scratching their head as to what the issue could be.

    Apr 24, 2021 at 9:58

  • 5

    I’m talking about the prototypical “well known site”. There are, unfortunately, very many of them. And they all invariably look like blind leading the blind 🙁

    Apr 26, 2021 at 12:54


There’s a Stack Exchange site called Programming Puzzles & Code Golf. The programming puzzles on that site fit this definition of puzzle:

a toy, problem, or other contrivance designed to amuse by presenting difficulties to be solved by ingenuity or patient effort.

They are designed to amuse, and not in the way that a working programmer might be amused by a real-world problem encountered in their daily work.

Code Golf is “a type of recreational computer programming competition in which participants strive to achieve the shortest possible source code that implements a certain algorithm.” In the answers on the PP&CG site, you’ll see people specify the number of bytes in their answers. When they find a way to shave off a few bytes, they’ll strike out the original number and record the new one.

As you might expect, code golfing rewards extreme programming language abuse. One-letter variable names. No whitespace. Creative use of library functions. Undocumented features. Nonstandard programming practices. Appalling hacks.

If a programmer submitted a pull request at work containing golf-style code, it would be rejected. Their co-workers would laugh at them. Their manager would drop by their desk for a chat. Even so, programmers amuse themselves by submitting answers to PP&CG.

What does this have to do with stdc++.h? As others have pointed out, using it is lazy. It’s non-portable, so you don’t know if it will work on your compiler or the next version of your compiler. It fosters bad habits. It’s non-standard, so your program’s behavior may differ from what you expect. It may increase compile time and executable size.

These are all valid and correct objections. So why would anyone use this monstrosity?

It turns out that some people like programming puzzles without the code golf. They get together and compete at events like ACM-ICPC, Google Code Jam, and Facebook Hacker Cup, or on sites like Topcoder and Codeforces. Their rank is based on program correctness, execution speed, and how fast they submit a solution. To maximize execution speed, many participants use C++. To maximize coding speed, some of them use stdc++.h.

Is this is a good idea? Let’s check the list of disadvantages. Portability? It doesn’t matter since these coding events use a specific compiler version that contestants know in advance. Standards compliance? Not relevant for a block of code whose useful life is less than one hour. Compile time and executable size? These aren’t part of the contest’s scoring rubric.

So we’re left with bad habits. This is a valid objection. By using this header file, contestants are avoiding the chance to learn which standard header file defines the functionality they’re using in their program. When they’re writing real-world code (and not using stdc++.h) they’ll have to spend time looking up this information, which means they’ll be less productive. That’s the downside of practicing with stdc++.h.

This raises the question of why it’s worth taking part in competitive programming at all if it encourages bad habits like using stdc++.h and violating other coding standards. One answer is that people do it for the same reason they post programs on PP&CG: some programmers find it enjoyable to use their coding skills in a game-like context.

So the question of whether to use stdc++.h comes down to whether the coding speed benefits in a programming contest outweigh the bad habits that one might develop by using it.

This question asks: “Why should I not #include <bits/stdc++.h>?” I realize that it was asked and answered to make a point, and the accepted answer is intended to be the One True Answer to this question. But the question isn’t “Why should I not #include <bits/stdc++.h> in production code?” Therefore, I think it’s reasonable to consider other scenarios where the answer may be different.


  • 13

    I’ve upvoted already, but it might be worth pointing out that “for fun” is a good reason to take part in competitive programming. On the other hand “to impress a potential employers” is not – it will actively harm your case with me.

    Apr 29, 2019 at 8:32

  • 8

    @MartinBonner I know some hiring managers see competitive programming experience as a red flag. But as long as top software companies use CP-style problems in their interviews and run programming contests to find new recruits, CP will continue to be popular among aspiring developers.

    Apr 29, 2019 at 15:46

  • 9

    @JesperJuhl If technical interviewers at your company use algorithmic puzzles in their interviews (as many do), that gives candidates with competitive programming experience an advantage. Maybe the rational choice for candidates is to participate in CP but avoid mentioning it on their resume/CV.

    Jul 23, 2019 at 23:41

  • 6

    While it’s true that this header can find use in some competitive programming, it’s not quite where it came from. It came from a classroom. And whoever taught in that classroom had enough influence to pollute – via cascade that followed – tens if not hundreds of thousands of students (by educating the teachers and peers who then, unwittingly, had been spreading that disease). And now those students are also writing tutorials in a go-to-place for tutorials. I just want to cry in a corner. Competitive programming sites should just have a regex to reject any nonstandard header.

    Apr 6, 2020 at 3:23

  • 5

    @YunfeiChen Some people believe it encourages bad habits (like using #include <bits/stdc++.h> or writing unreadable code) that the candidate would need to un-learn on the job. Having zero programming experience is also a red flag, but that’s why we have interviews.

    Jul 8, 2020 at 1:19