how to use getchar_unlocked()

I’m totally new in this,I really don’t know how to use this. I’ve googled it,but I’ve got some methods with getchar_unlocked method() somthing like dat

    #define getchar_unlocked gc()
void scanint(int &x)
{
    register int c = gc();
    x = 0;
    for(;(c<48 || c>57);c = gc());
    for(;c>47 && c<58;c = gc()) {x = (x<<1) + (x<<3) + c - 48;}
}

or

#define getcx getchar_unlocked
inline void inp( int &n )//fast input function
{
   n=0;
   int ch=getcx();int sign=1;
   while( ch < '0' || ch > '9' ){if(ch=='-')sign=-1; ch=getcx();}

   while(  ch >= '0' && ch <= '9' )
           n = (n<<3)+(n<<1) + ch-'0', ch=getcx();
   n=n*sign;
}

but didn’t get it at all. If I wanna take input char,int,long and long long int, can I use getchar_unlocked( ) for faster input?I’m getting TLE for a problem. please help me how to take input for int,char,long,long long int int c++.
Thnq :slight_smile:

Hi @tictactoecoder! Yes you can do that. Just change the data type of the value you’re passing in to this function to the data type you want to scan.

Brother, all the standard functions you use for IO are thread safe. Hence they need to lock mutexes and prevent simultaneous access to the buffer. getchar_unlocked is not thread safe. Hence all the overheads of mutual exclusion is avoided. But as we have a single thread it doesn’t matter for us. check more here. The getchar_unlocked() function gets a character from stdin. It is equivalent to getc_unlocked(stdin). It is used for faster i/o by removing the input stream lock check. But at the same time removing input stream lock check make it unsafe as well.