I'm trying to input a series of numbers (i.e. 123456789
), and then having output it each one at a time (i.e. 1, 2, 3, ... , 9
). However, after the 9, which is str[9]
onwards, the output value would be random numbers such as -48, 32, 9, -112
. How do I make sure the output numbers stops at 9, since the last number I had input was 9?
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
int n,num[100];
scanf("%s", str);
for(n=0;n<100;n++)
{
num[n] = str[n] - '0';
printf("%d\n", num[n]);
}
return 0;
}