# Fast I/O (Discussion)

I don’t understand what’s going on in here in this functions… i have seen it may times.

``````    int readint()
{
int cc = getc(stdin);
for (;cc < '0' || cc > '9';)  cc = getc(stdin);
int ret = 0;
for (;cc >= '0' && cc <= '9';)
{
ret = ret * 10 + cc - '0';
cc = getc(stdin);
}
return ret;
}``````
1 Like

this function can be found at: http://www.codechef.com/viewsolution/1648818

1 Like

Hello,

What this function is doing is what it’s known as “very fast I/O methods”.

Basically, what is being done here is that an integer number is being read from the standard input (stdin) as a stream of ASCII characters.

The line `ret = ret * 10 + cc - '0';` simply converts the stream of characters to the int number they are supposed to represent. (remember, characters in C are also numbers )

Best regards,

Bruno

5 Likes

can we use this function anywhere in the program, in place of scanf…?

Yes, you can replace this function with scanf() if you wish so… 1 Like

thanks a lot kuruma… It’s my pleasure - Here goes explanation :

``````
{
int cc = getc(stdin);
for (;cc < '0' || cc > '9';)  cc = getc(stdin); //ignores char stream other than 0-9.
int ret = 0;
for (;cc >= '0' && cc <= '9';) // if stream of numbers comes, start taking input
{
ret = ret * 10 + cc - '0'; // convert each char to integral digit and extend the final number
cc = getc(stdin);          // input continues
}
return ret;                   // return final extended number.
}

``````

Yes, you can use it anywhere in place of scanf(), but remember changing “int” to “long long” or “unsigned long long” depending on nature of input data.

20 Likes

very well explained…thanks a lot @bit_cracker007 1 Like

make the function inline if u r using c++ and use getchar_unlocked() for more fast I/O
becuz
getchar_unlocked() is the thread unsafe version of getchar()The reason that getchar_unlocked() seems faster is that it doesn’t check for any locks on the input stream from where it is supposed to fetch a character. So if another thread has locked the input stream, this thread is supposed to wait till lock count has come to zero. But this function doesn’t care about it, thereby destroying synchronisation between threads.
But if you are sure that lack of synchronisation wont harm you, then this function might help you to be a bit faster.

inline int scan( )
{

int n=0;

int ch=getchar_unlocked();

while( ch <48 )ch=getchar_unlocked();

while( ch >47 )

n = (n<<3)+(n<<1) + ch-‘0’, ch=getchar_unlocked();

return n;

}

10 Likes

it will not work for negative numbers.

2 Likes

can i use this function in C?

thank you @bit_cracker007 @chandan11111 2 Likes

ofcourse but inline function is feature of C++ there is macros in C for that. u can also use it in C remove tag inline from the function ,enjoy 1 Like

For negative numbers you must first check if the first character is ‘-’ in the start of the method and store it the result in some variable (multiplier). If it is just continue normally. If it isn’t don’t read the variable cc again and continue normally. In the end return ret*multiplier. Like this:

int cc = getc(stdin);

int multiplier = 1; //this will be multiplied by ret in the end

if(cc==’-’) {

``````multiplier = -1; // change the multiplier

cc = getc(stdin); //read cc again
``````

}

//do everything else starting from the first for loop

return ret*multiplier;

wont we have to reverse the number again?

no…the fxn will be reading the input from the 1st number onwards…eg if input is 1234…in the 1st iteration it will read 1 then 2 then 3 and in the end 4…and u r merging then in the same order like…1->12->123->1234…hope this helps… I didn’t get the first two lines of code.

int cc = getc(stdin);//Reads one char

for (;cc < ‘0’ || cc > ‘9’ cc = getc(stdin);//If char is not 0-9 loop breaks.

Correct me if I am wrong.
But the next for loop itself checks whether it is a digit or not.
Then why do you read in the first for loop?

@amitdoshi the first loop is there to consume any blank spaces or useless characters that may be present in the input file.

Oh, Thanks 