Error reading characters

0

Instead of the code reading the three char variables, it reads 2 and prints the 1st.

#include <stdio.h>

int main() {
    char ch1, ch2, ch3;

    puts("Digite 3 caracteres, um apos o outro:");
    scanf("%c%c%c", &ch1, &ch2, &ch3);
    printf("%c\n%c\n%c", ch1, ch2, ch3);
    return 0;
}
    
asked by anonymous 29.07.2014 / 13:25

1 answer

2

What is happening is this: you are typing enter with each character typed. Do not type enter, just type one character next to the other. And add a system ("pause") so you can see the output:

#include <stdio.h>
#include <stdlib.h>

int main() {
    char ch1, ch2, ch3;

    puts("Digite 3 caracteres, um apos o outro:");
    scanf("%c%c%c", &ch1, &ch2, &ch3);
    printf("%c\n%c\n%c\n", ch1, ch2, ch3);
    system("pause");
    return 0;
}
    
29.07.2014 / 13:52