How to Use Getchar() for fast i/o?

Hey! I’ve read at many places while searching for fast i/o methods that getchar unlocked() is the fastest.But,as i’m using windows it doesn’t work on my machine,instead getchar() worked.But I’m not able to understand how it is used,i mean to say what’s actually happening in the following code snippet which i’ve seen is used for fast i/o.

inline void scanint(long long int &x)  
    register long long int c = getchar();  
    x = 0;  
    long long int neg = 0;  
    for(;((c<48 || c>57) && c != '-');c = getchar());  
    if(c=='-') {neg=1;c=getchar();}  
    for(;c>47 && c<58;c = getchar()) {x = (x<<1) + (x<<3) + c - 48;}  
    if(neg) x=-x;  
int main()  
{long long int n;  
return 0;  

Can anyone please explain what’s happening in the above code? Also
How to start writing the code for fast i/o using getchar()?
Thanks!! For your Help.

Why would you need fast I/O? People somehow assume that optimizing the parts that aren’t related to the algorithm is more important than optimizing algorithmic skills, but it doesn’t work that way - you can get much more problems ACd with fast cin and good algorithms than getchar_unlocked and brute force.


Ok :)I got that,but still i just wanna know how it is being used?(just out of curiousity)
Thanks for answering :slight_smile:

Here is the function for reading integers using getchar_unlocked(). This is one of the faster methods for reading integer inputs. There is some explanation also regarding how it works.

1 Like

Thank you!! :):slight_smile: