You test mathSign == 'a' but mathSign is an array, so you should write mathSign[0] == 'a'. The compiler should have produced a warning. You should not ignore these warnings, they indicate problems that are often real bugs. You can get more useful diagnostics by raising the warning level: gcc -Wall -W -Werror or clang -Weverything -Werror...
Note that scanf("%s", mathSign); will cause a buffer overflow if the user types a word longer than 19 characters. You should protect the destination array with:
scanf("%19s", mathSign);
Furthermore, you should check that the conversion succeeds, otherwise you may have undefined behavior if the input cannot be converted and you use the values that are still uninitialized.
You could use " %c" to read a single character and use the classic operators for a more readability. Note the space before the %c: it lets scanf() skip pending white space characters, such as the newline from the previous response.
Here is a modified version:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int firstNumber, secondNumber, answer;
char mathSign;
printf("Enter first number: ");
if (scanf("%d", &firstNumber) != 1)
return 1;
printf("Enter operation\n + - Add\n - = Subtract\n * - Multiply\n / - Divide\n");
if (scanf(" %c", &mathSign) != 1)
return 1;
printf("Enter second number: ");
if (scanf("%d", &secondNumber) != 1)
return 1;
if (mathSign == '+') {
answer = firstNumber + secondNumber;
} else if (mathSign == '-') {
answer = firstNumber - secondNumber;
} else if (mathSign == '*') {
answer = firstNumber * secondNumber;
} else if (mathSign == '/') {
answer = firstNumber / secondNumber;
} else {
printf("unsupported operation: %c\n", mathSign);
return 1;
}
printf("Your answer is %d\n", answer);
return 0;
}
You can further simplify the program with a switch statement:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int firstNumber, secondNumber, answer;
char mathSign;
printf("Enter a simple expression: ");
if (scanf("%i %c%i", &firstNumber, &mathSign, &secondNumber) != 3)
return 1;
switch (mathSign) {
case '+': answer = firstNumber + secondNumber; break;
case '-': answer = firstNumber - secondNumber; break;
case '*': answer = firstNumber * secondNumber; break;
case '/': answer = firstNumber / secondNumber; break;
case '%': answer = firstNumber % secondNumber; break;
default:
printf("unsupported operation: %c\n", mathSign);
return 1;
}
printf("the result is %d\n", answer);
return 0;
}