char firstDigit[2] and char secondDigit[2] not large enough to contain a digit, a newline, and a null terminator:
char firstDigit[3]; char secondDigit[3];
Then fgets() calls should indicate the size of the buffer arrays:
fgets(firstDigit, sizeof firstDigit, stdin); fgets(secondDigit, sizeof secondDigit, stdin);
If fgets(firstDigit, 2, stdin); used instead fgets(firstDigit, 2, stdin); , fgets() stores a maximum of two characters, including the \0 character, in firstDigit[] . This means that the \n character is still in the input stream, and this interferes with the second call to fgets() .
In response to an OP comment How do I remove unread characters from an input stream? A good start would be to use more generous selections for firstDigit[] and secondDigit[] . For example, char firstDigit[100] or even char firstDigit[1000] will be large enough for any expected input to be received using fgets() without leaving characters in the input stream. To be more sure that the input stream is empty, the portable solution should use an idiomatic loop:
int c; while ((c = getchar()) != '\n' && c != EOF) { continue; }
Note that you need to check for EOF , since getchar() can return this value if the user signals the end of the file from the keyboard or if stdin was redirected or in the unlikely event of an input error. But also note that this loop should only be used if there is at least the \n character in the input stream. Before attempting to clear the input stream using this method, the input buffer must be checked for a new line; if it is present in the buffer, the input stream is empty and the loop should not be executed. In the code below, strchr() used to check for a newline character. This function returns a null pointer if the character to be found is not found in the input string.
#include <stdio.h> #include <string.h> // for strchr() int main(void) { char firstDigit[3]; // more generous allocations would also be good char secondDigit[3]; // eg, char firstDigit[1000]; printf("Enter your first digit: "); fgets(firstDigit, sizeof firstDigit, stdin); /* Clear input stream if not empty */ if (strchr(firstDigit, '\n') == NULL) { int c; while ((c = getchar()) != '\n' && c != EOF) { continue; } } putchar('\n'); printf("Enter your second digit: "); fgets(secondDigit, sizeof secondDigit, stdin); /* Clear input stream if not empty */ if (strchr(secondDigit, '\n') == NULL) { int c; while ((c = getchar()) != '\n' && c != EOF) { continue; } } puts("\n"); printf("Your first digit is %c and your second digit is %c.\n", firstDigit[0], secondDigit[0]); return 0; }
Even better, use a single buffer[] to hold input lines, and then to store individual characters in char s. You can also write a function to clear the input stream, instead of rewriting the same loop every time you need it:
#include <stdio.h> #include <string.h> // for strchr() void clear_stdin(void); int main(void) { char buffer[1000]; char firstDigit; char secondDigit; printf("Enter your first digit: "); fgets(buffer, sizeof buffer, stdin); firstDigit = buffer[0]; /* Clear input stream if not empty */ if (strchr(buffer, '\n') == NULL) { clear_stdin(); } putchar('\n'); printf("Enter your second digit: "); fgets(buffer, sizeof buffer, stdin); secondDigit = buffer[0]; /* Clear input stream if not empty */ if (strchr(buffer, '\n') == NULL) { clear_stdin(); } puts("\n"); printf("Your first digit is %c and your second digit is %c.\n", firstDigit, secondDigit); return 0; } void clear_stdin(void) { int c; while ((c = getchar()) != '\n' && c != EOF) { continue; } }