So I'm trying to reverse an array of strings. The code is working but the issues are:
After the last input I have to enter an extra char (any char will do) and press enter in order for the code to execute.
That it prints 2 random char or
??
before the output. Can anyone please explain why it's printing those??
...
code:
#include <stdio.h>
int main(void)
{
int tot_books;
int i;
int j;
scanf("%d\n", &tot_books);
char titles[tot_books][101];
i = 0;
while (i < tot_books)
{
scanf("%[^\n]\n", titles[i]);
i++;
}
while (tot_books >= 0)
{
printf("%s\n", titles[tot_books]);
tot_books--;
}
return (0);
}
output:
7
Germinal
Le petit prince
Le meilleur des mondes
L'ecume des jours
L'Odyssee
Les miserables
Crime et Chatiment
d
��
Crime et Chatiment
Les miserables
L'Odyssee
L'ecume des jours
Le meilleur des mondes
Le petit prince
Germinal