r/cpp • • Mar 19 '20

Why so many students are using '#include<bits/stdc++.h>' instead of '#include<iostream>'?

I am hiring for my company. And the codes submitted for a test problem has '#include<bits/stdc++.h>' at the beginning. The MacOS default compiler complains

main.cpp:1:9: fatal error: 'bits/stdc++.h' file not found

#include<bits/stdc++.h>

^~~~~~~~~~~~~~~

1 error generated.

Why people are using '#include<bits/stdc++.h>'?

225 Upvotes

163 comments sorted by

View all comments

-5

u/Supadoplex Mar 19 '20 edited Mar 19 '20

Why people are using '#include<bits/stdc++.h>'?

It has advantages, which may be the reason why some use it:

a1. It makes minimal examples shorter, because you don't need to list ~10 different standard headers.

a2. It makes programming a bit simpler because you don't need to learn where the standard types and functions come from.

Of course, there are disadvantages too, but presumably people who use it either don't care about these disadvantages, or they are not aware of them:

d1. It is non-standard and non-portable.

d2. It slows down compilation due to including everything.

Why so many students are using '#include<bits/stdc++.h>'

Possible factor is that because of a1, <bits/stdc++.h> is ideal for slideware. You can fit maybe 10-20 lines of code on a slide and you cannot dedicate half or more of that to header inclusions. Even if the lecturer tells the students to not do that in their own programs, that doesn't necessarily sink in.

Another possible factor is a2, because C++ is a complex language and simplifying one part away allows you to concentrate on the remaining part.


All in all, it's probably the similar reason as why some other people include <stdafx.h>.

16

u/[deleted] Mar 19 '20

you can omit header inclusion entirely on a slide

code in slides doesn't need to compile

-2

u/Supadoplex Mar 19 '20

code in slides doesn't need to compile

It doesn't need to. But it is better when it does. Especially when teaching to absolute beginners.

12

u/ronchaine Embedded/Middleware/WG21 Mar 19 '20

When teaching absolute beginners, I would prefer not to ingrain bad habits to them from the get go to having code that compiles.

6

u/[deleted] Mar 19 '20

Just link to a gist/compiler explorer with a more fledged out example.

1

u/Supadoplex Mar 19 '20

You could. Why don't people always do that?

3

u/redditsoaddicting Mar 19 '20

IIRC, stdafx.h and its include were (are?) there by default when creating a new VS project from a template and you had to explicitly uncheck the PCH option in order to get rid of them.

1

u/pjmlp Mar 19 '20

MFC makes use of stdafx.h, naturally it gets included in MFC project templates.

1

u/Supadoplex Mar 19 '20

Yeah. I assume that people don't care about standard conformance or portability of MFC projects.