i want to take a very long integer(it may contain upto 1000 digits). and i want to store it in an array so that each digit will sit in each place of the array.but if i try to use the code below it takes the whole number in the 0 position untill i press enter. but i can’t press enter. each digit will be scanned separately even though the user will write it as a complete number.
for(count=0;number[count]!='\0';count++){
scanf("%d",number+count);
}
what is the way there??
Don’t use
scanf. Usefgetsto get a line of input as achar*, then you can pull individual digits out of thechar*as characters, and convert them tointdigits yourself.scanf("%d", ...)will cause problems if the user enters input that can’t be parsed as a digit.fgetswill consume the entire line of input up to the newline, or stop at the number of characters specified by argument 2.If you want to continue reading digit-by-digit, then use
getchar()to read individual characters, orfgetc(FILE *)to read individual characters from a file.