1

Is there a way for the compiler treat operator as true meaning of +,-,/ or *? Below does not work.

#include <stdio.h>

int main(void) {

int number1=10, number2=5;
char operator;
int result=0;

printf("Enter Operator ");
scanf("%c",&operator);

if (operator=='+')
result=number1+operator+number2;

printf("%d",result);


}
Lyrk
  • 1,936
  • 4
  • 26
  • 48

6 Answers6

3

This:

result=number1+operator+number2;

Will never work in C as you suppose. As you actually add the ascii code of the + to number1 and number2.

So, I'd like to do the following:

switch(operator){
    case '+':
        result = number1 + number2;
        break;
    case '-':
        result = number1 - number2;
        break;
    case '*':
        result = number1 * number2;
        break;
    case '/':
        result = number1 / number2;
        break;
    default:
        puts("Incorrect operator");
}

This will allow to get the correct result.

Alex
  • 9,891
  • 11
  • 53
  • 87
2

No. C is not JavaScript. It has no run-time evaluation capabilities. If you want to parse and evaluate mathematical expressions, then you have to do something more complex, like this guy did.

Community
  • 1
  • 1
2

No, C is a statically typed language. You cannot do that.

What you can do is, ask the user for selecting an operation and based on user input you can have a switch case which performs the operation user suggests.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
1

No, C can't do that.

Alternatively :

if (operator=='+')
    result=number1+number2;
Thanakron Tandavas
  • 5,615
  • 5
  • 28
  • 41
1

The value of operator is a character, not a function or operator. You can't just interpret the character, which is entered at run time, as part of your program which is built when they program is compiled.

Do this instead:

if (operator == ’+') return number1 + number2;
Caleb
  • 124,013
  • 19
  • 183
  • 272
  • Do you mean that addition is a function under the hood? Oops – Lyrk Apr 21 '13 at 12:39
  • No, it's not, it's a built-in operator. That's why I distinguished between them. Nevertheless, if you want the *meaning* of `+` according to the compiler, you need to compile the code. Alternatively, write a program that interprets the input the way you want, like above. – Caleb Apr 21 '13 at 13:08
1

I'm afraid you have to handle each operator in switch or statement like that:

switch(operator){
    case '+': // Note that this works only for scalar values not string
        result = number1+number2;
    // ...
}

Alliteratively you could prepare operator callback list like this:

typedef int (*operator_callback) (int operand1, int operand2);
typedef struct {
    char name;
    operator_callback callback;
} operator_definition;

int operator_add(int x, int y) {return x+y;}
int operator_minus(int x, int y) {return x-y;}

// And prepare list of those
operator_definition[] = {
    {'+', operator_add},
    {'-', operator_minus},
    {NULL, NULL}
};

// Function for fetching correct operator callback
operator_callback get_operator_callback(char op)
{
    int i;
    for( i = 0; operator_definition[i].name != NULL; ++i){
        if( operator_definition[i].name == op){
            return operator_definition[i].callback;
        }
    }
    return NULL;
}

// And in main function
operator_callback clbk = get_operator_callback(operator);
if( !clbk){
    printf( "Unknown operator %c\n", operator);
    return -1;
}
result = clbk(number1, number2);
Vyktor
  • 20,559
  • 6
  • 64
  • 96