You are not logged in. Please login at www.codechef.com to post your questions!

×

# [closed] Fast I/O (Discussion)

 1 1 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; }  asked 26 May '13, 17:00 198●10●25●35 accept rate: 0% 4★junior94 3.2k●14●30●58

### The question has been closed for the following reason "The question is answered, right answer was accepted" by junior94 27 May '13, 00:19

 6 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 answered 26 May '13, 17:05 3★kuruma 17.7k●72●143●209 accept rate: 8% can we use this function anywhere in the program, in place of scanf..? (26 May '13, 17:08) 1 Yes, you can replace this function with scanf() if you wish so... :) (26 May '13, 17:17) kuruma3★ thanks a lot kuruma.. :) (26 May '13, 17:20) It's my pleasure ;) (26 May '13, 17:20) kuruma3★ 2 it will not work for negative numbers. (26 May '13, 20:00) 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; (27 May '13, 00:15) junior944★ 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? (24 May '14, 17:54) @amitdoshi the first loop is there to consume any blank spaces or useless characters that may be present in the input file. (25 May '14, 01:10) kcahdog3★ Oh, Thanks :) (25 May '14, 09:09) @kuldeepfouzdar what to do in case of negative numbers? (11 Oct '14, 18:08) showing 5 of 10 show all
 20 - Here goes explanation :  int readint() { 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. answered 26 May '13, 17:21 5.7k●26●68●68 accept rate: 17% 1 very well explained...thanks a lot @bit_cracker007...:) (26 May '13, 17:38) kunal3614★ 2 thank you @bit_cracker007 @chandan11111 :) (26 May '13, 23:27) wont we have to reverse the number again? (14 Nov '13, 23:35) rishik910★ 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...:) (15 Nov '13, 08:10) kunal3614★
 9 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; } answered 26 May '13, 18:29 3.6k●13●35●55 accept rate: 10% can i use this function in C? (26 May '13, 23:23) 1 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 :) (26 May '13, 23:28)
 0 this function can be found at: http://www.codechef.com/viewsolution/1648818 answered 26 May '13, 17:02 198●10●25●35 accept rate: 0%

### Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×2,698
×1,470
×217

question asked: 26 May '13, 17:00

question was seen: 12,864 times

last updated: 11 Oct '14, 18:08