The program skips the scanf("%c", operator) whenever I run this code (C). I am just learning C and find this confusing. The program is working as expected when I ask for the scanf("%c", operator) before asking for any other input. What is at play here?
#include <stdio.h>
#include <string.h>
int main() {
char variable[2];
char operator;
int first, second;
printf("Enter the variable: \n");
scanf("%s", &variable);
printf("Enter an operator (+, -): ");
scanf("%c", &operator);
printf("Enter two operands (one before equal and one after equal sign): ");
scanf("%d %d", &first, &second);
switch (operator) {
case '+':
printf("x = %d and x = %d", first + second, first - second);
break;
case '-':
printf("x = %d and x = %d", first + second, first - second);
break;
// Absolute Value for simple calculations
default:
printf("Error! Difficult computation");
}
return 0;
}