简体   繁体   English

算术运算不适用于变量

[英]Arithmetic operation doesn't work with variable

I created this variable float avg_perc and performed some arithmetic operation to find average percentage.我创建了这个变量float avg_perc并执行了一些算术运算来找到平均百分比。 The output is unexpected ie 0 for any value of marks1, mark2, marks3. output 是出乎意料的,即标记1、标记2、标记3 的任何值都为0。

#include <stdio.h>

int main(){
    int marks1, marks2, marks3;
    printf("Enter your marks in all 3 subjects : ");
    scanf("%d %d %d", &marks1, &marks2, &marks3);
    float avg_perc = ((marks1+marks2+marks3)/300)*100;    //problem part
    printf("%f \n", avg_perc);
    if(avg_perc>=40 &&marks1>=33 && marks2>=33 && marks3>=33){
        printf("Pass.");
    }
    else{
        printf("Fail.");
    }
     return 0;

}

It doesn't really matter that you have float avg_perc =... for storing the result, because that type does not affect the type used for the calculations.使用float avg_perc =...来存储结果并不重要,因为该类型不会影响用于计算的类型。

In C, every operand and integer constant in an expression has a type.在 C 中,表达式中的每个操作数和 integer 常量都有一个类型。 And depending on the types of the operands, each operation is carried out on a specific type which then also becomes the resulting type of the (sub) expression.并且根据操作数的类型,每个操作都在特定类型上执行,然后该类型也成为(子)表达式的结果类型。

In this case the types are as indicated by the comment below:在这种情况下,类型如下面的注释所示:

    float avg_perc = ((marks1 + marks2 + marks3) / 300) * 100;
//        float        int    + int    + int     / int  * int

Operator precedence determines which operands that belong to which operator.运算符优先级决定了哪些操作数属于哪个运算符。 In this case the () parenthesis has the highest precedence, then * and / , then + and finally = .在这种情况下, ()括号具有最高优先级,然后是*/ ,然后是+ ,最后是=

The subexpression (marks1 + marks2 + marks3) will get all calculations carried out on int type since all involved operands of + are int .由于所有涉及的 + 操作数都是int ,因此子表达式(marks1 + marks2 + marks3)将在int类型上执行所有计算。 Then the result of that will form a new expression "result / 300" where 300 is int .然后结果将形成一个新的表达式“result / 300”,其中300int Again, calculation is carried out on int .同样,计算是在int上进行的。 And then finally the same thing with * 100 .最后与* 100相同。

When all the above calculations have been carried out on int , then the assignment happens last of all, since = has the lowest precedence.当所有上述计算都在int上执行时,分配最后发生,因为=具有最低优先级。 During assignment, there is a special conversion rule stating that the value of the right operand is is converted to the type of the left operand.在赋值过程中,有一个特殊的转换规则,将右操作数的值转换为左操作数的类型。 So this conversion of the result to float happens last, when all of the other calculations have already been carried out on int .因此,当所有其他计算都已经在int上执行时,结果到float的转换发生在最后。

You need to make the divisor a type of double by appending .0 into it to avoid the integer division and this was exactly the reason you were having this strange issue.您需要通过将除数附加到其中来使除数成为double精度数,以避免.0除数,这正是您遇到这个奇怪问题的原因。

Change:改变:

(marks1 + marks2 + marks3) / 300

into:进入:

(marks1 + marks2 + marks3) / 300.0

Note that if you put a floating point prefix, for instance 300.0f then you'll see a miscalculation.请注意,如果您输入浮点前缀,例如300.0f ,那么您会看到计算错误。 For example, you get 30.000002 after inputting 30 30 30 in the program.例如,在程序中输入30 30 30后得到30.000002

In your code在您的代码中

  ((marks1+marks2+marks3)/300)*100; 

is integer arithmetic.是 integer 算术。 You need to ensure at least one of the participating arguments are floating point (or casted to a float or double) in order to ensure floating point arithmetic.您需要确保至少有一个参与的 arguments 是浮点数(或强制转换为浮点数或双精度数),以确保浮点运算。 Something like就像是

(((float)marks1+marks2+marks3)/300)*100;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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