I have seen many programmers use something as “getchar_unlocked()” function in their program in C for accepting input . Can anybody tell me what this function does and what is the advantage of using this instead of simple “scanf()” ?
The main advantage of getchar_unkocked() is SPEED. It works same as getchar() works.
I found this on Stack overflow -
link
Description - Two points to consider.
getchar_unlocked is deprecated in Windows because it is thread unsafe version of getchar().
Unless speed factor is too much necessary, try to avoid getchar_unlocked.
Now, as far as speed is concerned.
getchar_unlocked > getchar
because there is no input stream lock check in getchar_unlocked which makes it unsafe.
getchar > scanf
because getchar reads a single character of input which is char type whereas scanf can read most of the primitive types available in c.
So, finally
getchar_unlocked > getchar > scanf
1 Like
Ok, great, that helped me …