0
What is wrong with this code?
3 Answers
+ 2
Simply change
scanf( "%d", &dig );
to
scanf( "%d\n", &dig);
The problem here is that scanf() leaves the newline character in the input buffer, which is then consumed by the fgets() call, making it store that newline character in the string and then stop. Changing the format specifier will make scanf() extract the newline character as well, without affecting "digit".
+ 1
logic wise: why is dig++ in line 8?
I removed it and got correct output.



