简体   繁体   中英

Why printf does not print “operator” on console?

Bottom printf() does not print operator variable on console. But only number1 and number2 are printed. What can be the cause?

Output : How much is 2 7 ?

char operator;

    switch(type){   
        case 1: 
        operator=='+';
        result=number1+number2;
        break;
        case 2: operator=='-';
        result=number1-number2;
        break;
        case 3: operator=='*';
        result=number1*number2;
        break;
        case 4: 
        operator=='/';
        result=number1/number2;
        break;

    }       
    printf("How much is %d %c %d ?",number1, operator, number2);
operator=='+';

should be

operator='+';

The same for the other operators

The assignment in c is with = only

The equal comparison is with == example if(operator == '+')

== is equality operator, it doen't assign a value just checks if the two sides are equal.

You must use the assignment operator = .

For example:

operator='/'

Instead of using of the assignment operator = , you are using the comparison operator == .

Thus after the operator=='+' the value of the operator remains unchanged.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM