I am trying to write a c program. It have to enter two arrays and input should be space seperated. I tried somehow to eliminate the '\n'.
#include <stdio.h>
int main()
{
char temp;
int alc[3]={0}, bob[3]={0}, i=0;
//enter alice
do
{
scanf("%d%c", &alc[i], &temp);
i++;
} while(temp != '\n');
i=0;
//enter bob
do
{
scanf("%d%c", &bob[i], &temp);
i++;
} while(temp != '\n');
//print alice
for(i = 0; i < 3 ; i++)
{
printf("%d ", alc[i]);
}
//print bob
for(i = 0; i < 3 ; i++)
{
printf("%d ", bob[i]);
}
return 0;
}
output ./a.out
5 6 7
3 6 10
5 6 7 3 6 10
Is there a better way to do same?
do whilewith array size 3, best of luck. :)scanf()family of functions, always check the returned value (not the parameter values) to assure the operation was successful. In the current scenario, any returned value other than 2 indicates an error. Note: scanf() does not seterrnowhen some input format specifier fails, so should usefprintf( stderr, "...\n" )rather thanperror()when reporting the error.