It is not possible with C standard library only, so those 4 headers you list are not enough. scanf
will wait for the user to press enter, and there's no way to get it to interrupt after 30 seconds without using platform specific features or extra libraries (which hide the platform specific features).
As a guideline for generic solution, you need a way to get characters from terminal so that you can also interrupt after a specific time. So you would read character by character and put them to a buffer, until your time limit is reached or until user presses enter (note that this is not possible with for example getchar()
, because even though it gives you one character, it will still block until user presses enter, and then give you the entered characters one by one).
Then once you have an entire line (or maybe until space, or until you have read N digits, or whatever you want to do), use sscanf
or strtol
or whatever to parse the string and get the integer you want.