In C, space is a normal character, just like any other.
What you're seeing is strange there, in fact the scanf
function uses space as an input value separator by default.
If you instead of scanf
use the fgets
function, you will have fewer surprises: all characters entered by the user will be transferred to the vector you have determined until a line break (a string that depends on the operating system ).
In addition, fgets, just like scanf, adds a \x00
character, which represents the end of the string for most functions in C.
Instead of:
scanf("%s", nome);
Your line of reading would look like this:
fgets(nome, 20, stdin);
and you would not be surprised with spaces, which would be a normal character. In addition, fgets can be used in programs and libraries of real systems, which go to production, because it has the maximum size of the string to be read, and thus avoids buffer overflow problems. Scanf is a very versatile function to read an arbitrary number of tokens, and even different data types, but it is harder to use correctly for simple cases.