Storing a character in the vector

3

For some reason, the program is not saving the character in the vector, as it should be stored, it is jumping the first character. For example:

// Declarando o vetor

char novosValores[4] = {0};

scanf("%c %c %c %c", &novosValores[0], &novosValores[1], &novosValores[2], &novosValores[3]);

Follow the image:

    
asked by anonymous 14.02.2018 / 17:25

1 answer

1

The scanf function uses a buffer to get the input. The user entering the input into a terminal is stored in a buffer. The scanf function reads the characters from this buffer.

What happens in your example is that you are entering, after 4, the string

"\nE C A F\n"

So the first character the scanf function reads is '\n' .

A blank space in the scanf format tells the whitespace whitespace (% cos_de% spaces tabs, etc.) function to skip. In this way, you should start the string with a blank space to skip the "whitespace" and read the character you want.

    
31.08.2018 / 16:44