hii
i want to know how (gets)works

No, you don't. You want to stay away from gets() entirely, in fact. The function is extremely dangerous, because it is prone to buffer overruns, and no experienced C/C++ programmer still uses it.

Well, knowing how it works and using it are entirely different.

I agree with Schol-R-LEA in that you should never use this function. However, if you want to learn how it works internally to better understand it and why you should avoid it - that is a good thing.

Look for an open source implementation of it (FreeBSD, for example: gets.c)