/* Variables */
int num;
char array[5]; // Array must also include NULL char
char new;
/* Accepting user input */
printf("Please enter 5 characters below:\n");
scanf("%c\r", &array[0]);
scanf("%c\r", &array[1]);
scanf("%c\r", &array[2]);
scanf("%c\r", &array[3]);
scanf("%c", &array[4]);
/* Displaying input as string */
printf("%s", array);
I am trying to create a basic program that creates a string that accepts chars one after the other. I know that arrays start at 0 and end with the null character so I made my array[5] so if I inputted "a" "b" "c" "d" "e" I would get the output string "abcde" plus the null character. Sometimes it works with one set of characters but not another. If I input "q" "w" "e" "r" "t" i might get the output "qwerta". How do I stop it from displaying that last character?
I understand that the reason why this character being displayed is that it is undefined and will show whatever value is at that memory location but I don't know how to fix it.
I have tried to include different escape sequences, putting spaces in the scanf and printf statements but what am I doing wrong?