getchar_unlocked() – Faster Input in C/C++ For Competitive Programming Last Updated : 08 Feb, 2022 Comments Improve Suggest changes Like Article Like Report getchar_unlocked() is similar to getchar() with the exception that it is not thread-safe. This function can be securely used in a multi-threaded program if and only if they are invoked when the invoking thread possesses the (FILE*) object, as is the situation after calling flockfile() or ftrylockfile(). Syntax: int getchar_unlocked(void); Example: Input: g C++ // C++ program to demonstrate // working of getchar_unlocked() #include <iostream> using namespace std; int main() { // Syntax is same as getchar() char c = getchar_unlocked(); cout << "Entered character is " << c; return 0; } // This code is contributed by SUBHAMSINGH10. C // C program to demonstrate // working of getchar_unlocked() #include <stdio.h> int main() { // Syntax is same as getchar() char c = getchar_unlocked(); printf("Entered character is %c", c); return 0; } Output Entered character is g Following are Some Important Points: Since it is not thread-safe, all overheads of mutual exclusion are avoided and it is faster than getchar().Can be especially useful for competitive programming problems with “Warning: Large I/O data, be careful with certain languages (though most should be OK if the algorithm is well designed)”.There is no issue with using getchar_unlocked() even in a multithreaded environment as long as the thread using it is the only thread accessing file objectOne more difference with getchar() is, it is not a C standard library function, but a POSIX function. It may not work on Windows-based compilers.It is a known fact that scanf() is faster than cin and getchar() is faster than scanf() in general. getchar_unlocked() is faster than getchar(), hence fastest of all.Similarly, there are getc_unlocked() putc_unlocked(), and putchar_unlocked() which are non-thread-safe versions of getc(), putc() and putchar() respectively. Example: Input: g C++ // C++ program to demonstrate // working of putchar_unlocked() #include <iostream> using namespace std; int main() { // Syntax is same as getchar() char c = getchar_unlocked(); putchar_unlocked(c); return 0; } //This code is contributed by Shubham Singh C // C program to demonstrate // working of putchar_unlocked() #include <stdio.h> int main() { // Syntax is same as getchar() char c = getchar_unlocked(); putchar_unlocked(c); return 0; } Output g As an exercise, the readers may try solutions given here with getchar_unlocked() and compare performance with getchar(). Comment More infoAdvertise with us Next Article getchar_unlocked() – Faster Input in C/C++ For Competitive Programming K kartik Improve Article Tags : Competitive Programming C Language C++ DSA CPP-Library cpp-input-output +2 More Practice Tags : CPP Similar Reads Fast I/O for Competitive Programming In competitive programming, it is important to read input as fast as possible so we save valuable time. You must have seen various problem statements saying: " Warning: Large I/O data, be careful with certain languages (though most should be OK if the algorithm is well designed)" . The key for such 4 min read C++ tricks for competitive programming (for C++ 11) We have discussed some tricks in the below post. In this post, some more tricks are discussed. Writing C/C++ code efficiently in Competitive programming Although, practice is the only way that ensures increased performance in programming contests but having some tricks up your sleeve ensures an uppe 5 min read Top 10 Most Used Inbuilt C++ functions for Competitive Programming In this article, we will discuss about the 10 most used inbuilt functions of C++ which will help you to save time and make code concise as well during competitive programming. List of top 10 inbuilt functions in C++ pow()sqrt()min()max()swap()gcd()toupper()tolower()floor()ceil()1. pow( ) This functi 7 min read Writing code faster during Competitive Programming in C++ This article focuses on how to implement your solutions and implement them fast while doing competitive programming. Setup Please refer Setting up a C++ Competitive Programming Environment Snippets Snippet is a programming term for a small region of re-usable source Code. A lot of modern text editor 3 min read Input/Output from external file in C/C++, Java and Python for Competitive Programming In Competitive Programming, most of the time we need to enter input for checking our code manually. But it would become cumbersome if we have a questions like Graphs, strings, or bulky entry of input data because it will definitely time out or the chances of typing incorrect data would be increased. 4 min read Like