Here is my code:
#include <stdio.h>
void scan(int* i)
{
int t=0;
char c;
bool negative=false;
c=getchar_unlocked();
while(c<'0'&&c>'9')
{
if(c=='-')
negative=true;
c=getchar_unlocked();
}
while(c>'0'&&c<'9')
{
t=(t<<3)+(t<<1)+c-'0';
c=getchar_unlocked();
}
if(negative)
t=~(t-1); //negative
*i=t;
}
int main(int argc, char const *argv[])
{
int i;
scan(&i);
return 0;
}
I know that the function defined here as scan is faster than scanf and is very useful on programming competitions. But for some reason this code is not working on Windows and is working on Linux. What do I do to get it working on Windows? I am using the g++ compiler from Dev-C++.
getchar_unlockedis not a C or C++ standard function and therefore it’s no surprise that it doesn’t work on Windows. It is a POSIX standard I think, but Windows compilers don’t support all POSIX functions.If you replaced
getchar_unlockedwithgetchar, it would kind of work, although the algorithm doesn’t seem quite right.You could do this with conditional compilation, like this for instance