DON'T USE bits/stdc++.h

To those programmers who use the header <bits/stdc++.h> , I strongly recommend them to stop using this header.I know its convenient to use the header as you don’t need to include any other header but it isn’t any standard header of C++ and you’ve no idea what it will do to the program.

More information: More info

As an example I’m providing 2 of my codes. The only difference between them is that one uses <bits/stdc++.h> (and gives Wrong Answer for one of the test cases) and another does not use <bits/stdc++.h> (and gives Correct Answer for the same test case)

The one with <bits/stdc++.h> : Solution 1
The one without <bits/stdc++.h> : Solution 2

The problem is GMEDIAN, from November Long Challenge. I’ve written an editorial on the question as well.
In case you need : Editorial

PS: Yes. I had a tough time debugging this :stuck_out_tongue:

3 Likes

Is this for real?

really weird :open_mouth:

1 Like

@harrypotter0 yes. It is :stuck_out_tongue:

1 Like

@divik544 I agree :confused:

Thanks for the post. I will now switch from

#include<bits/stdc++.h>
using namespace std;

to this

#include <algorithm>
#include <iostream>
#include <cstring>
#include <complex>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <vector>  
#include <string>
#include <cmath>
#include <ctime>
#include <queue>
#include <list>
#include <map>
#include <set>
using namespace std;

Can anyone explain why this is happening? What would be the test case that causes this issue?

I still feel its not the header but some undefined behavior in code which fortunately decided to behave correctly on changing header.

1 Like

The same thing happens if you include instead of <stdio.h>.

https://www.codechef.com/viewsolution/21642809

1 Like

#1)Here is wrong answer solution with iostream (standard i/o library)
#2)Here is accepted solution with bits/stdc++.h (just used cout instead of printf)

Now should I stop using iostream too ? XD

It’s because of printf,scanf.
Though thanks for sharing.

It does seem iostream is the culprit, cstdio seems to work. However I don’t understand why iostream exposes printf and scanf at all… the reference pages definitely do not document this.

1 Like

I was unable to replicate this on my machine with random test cases. Please share such a test case if you find one.

@meoow exactly!
I’m confused what to use from now on :confused:

Can it be because of using %lld in scanf? I am not sure but if that is an issue in first place, but I used %lld for long long int and not long long.

Switching from scanf to cin for n only fixes the problem.

https://www.codechef.com/viewsolution/21643280

1 Like

@vijju123 Is there a difference? both long long and long long int are same AFAIK

May be a subtle bug in compiler.
In my solution is with iostream.

long long is the same type as long long int, just as long is the same as long int.

Using cout to print the answer instead of printf gives correct answer for that test case

In the two solutions I have provided, I am pre-calculating the factorials at the starting of main() function.
If I calculate the factorials by calling a function, I’m again getting correct answer.
Here, I’m pre-calculating factorials by using a function fill() : https://www.codechef.com/viewsolution/21652861

There is too much of undefined behaviour of this code, it would be nice if someone answers this question.

1 Like