maty_nz maty_nz - 2 months ago 6
C Question

how to read scanf with spaces

I'm having a weird problem

i'm trying to read a string from a console with scanf()

like this


but it doesnt read anything. it just skips the entire scanf.

I'm trying it in gcc compiler


Trying to use scanf to read strings with spaces can bring unnecessary problems of buffer overflow and stray newlines staying in the input buffer to be read later. gets() is often suggested as a solution to this, however,

From the manpage:

Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets() instead.

So instead of using gets, use fgets with the STDIN stream to read strings from the keyboard