The values of characters x,y,z are ASCII characters not numbers. In ASCII, the digits 0 through 9 are represented by the characters '0' through '9' consecutively. The int value of the character '0' is 48 in decimal, character '1' is 49, and so on. Type "man ascii" from a linux prompt to see the full list of ASCII characters.
So to convert a '0' to an int value, subtract '0' from it, and you get 0.
Because the characters are consecutive in the ASCII table, this works for all 10 numeric ASCII characters (this is no accident, it was designed this way). Note as David points out: While there are other encodings besides ASCII, all encodings require that the numeric characters be consecutive, so this math to convert to integers always works.
So if you have any char C which is a numeric char from '0' to '9', you can get the numeric value of it using
int i = C - '0';
Your values for a and b are wrong, you are converting the ASCII character value to an int using a cast, but you need to convert it by subtracting the value of the character '0':
#include <stdio.h>
#include <stdlib.h>
int main()
{
char x, y, z;
printf("Enter the calculation: ");
scanf("%c %c %c", &x, &y, &z);
// Convert ASCII char to int by subtracting value of char '0'
int a = x - '0';
int b = z - '0';
if (y == '+') {
printf("The answer is %d", a+b);
}
else if(y == '*') {
printf("The answer is %d", a*b);
}
else {
printf("Use only +, -, /, * signs");
}
return 0;
}
For converting strings to ints, use the atoi() function mentioned in this post here:
char *myString = "1234";
int myIntVal = atoi(myString);
This is a duplicate of https://stackoverflow.com/a/868508/6693299