C语言 如何在c中划分2个int?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35069863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to divide 2 int in c?
提问by Alex
wanna divide 2 numbers and get the result like this:
想要除以 2 个数字并得到如下结果:
5 / 2 = 2.50
5 / 2 = 2.50
But it only outputs 2.
但它只输出 2。
I don't now what i'm doing wrong.
我现在不知道我做错了什么。
Here my code:
这是我的代码:
int a;
int b;
int c;
printf("First num\n");
scanf("%d", &a);
printf("Second num\n");
scanf("%d", &b);
c = a / b;
printf("%d", c);
回答by Haris
You need a doublevariable to store the result. intstores only integers. Additionally, you have to typecast the other variables also before performing the division.
您需要一个double变量来存储结果。int只存储整数。此外,您还必须在执行除法之前对其他变量进行类型转换。
Do something like this
做这样的事情
double c;
.
.
.
c = (double)a / (double)b;
printf("%f", c);
NOTE:
笔记:
You do not need the &in printf()statements.
您不需要&inprintf()语句。
回答by Jaffer Wilson
The '/' - sign is for division. Whenever in C language, you divide an integer with an integer and store the data in an integer, the answer as output is an integer. For example
'/' - 符号用于除法。每当在 C 语言中,将整数除以整数并将数据存储在整数中时,作为输出的答案是整数。例如
int a = 3, b = 2, c = 0;
c = a/b; // That is c = 3/2;
printf("%d", c);
The outputreceived is: 1
The reason is the type of variable you have used, i.e. integer (int)
Whenever an integer is used for storing the output, the result will be stored as integer and not a decimal value.
接收到的输出是: 1
原因是您使用的变量类型,即整数 ( int)
每当使用整数存储输出时,结果将存储为整数而不是十进制值。
For storing the decimal results, C language provide float, double, long floatand long double.
为了存储十进制结果,C 语言提供了float、double、long float和long double。
Whenever you perform an operation and desires an output in decimal, then you can use the above mentioned datatypes for your resultant storage variable. For example
每当您执行操作并希望以十进制输出时,您都可以将上述数据类型用于结果存储变量。例如
int a = 3, b = 2;
float c = 0.0;
c = (float)a/b; // That is c = 3/2;
printf("%.1f", c);
The outputreceived: 1.5
So, I think this will help you to understand the concept.
Remember: When you are using floatthen the access specifier is %f. You need to convert your answer into float, just as I did, and then the answer will be reflected.
收到的输出:1.5
所以,我认为这会帮助你理解这个概念。
请记住:当您使用时float,访问说明符是%f. 您需要将您的答案转换为float,就像我所做的那样,然后答案将被反映。
回答by LapizLazuli
To avoid the typecast in float you can directly use scanf with %f flag.
为了避免 float 中的类型转换,您可以直接使用带有 %f 标志的 scanf 。
float a;
float b;
float c;
printf("First number\n");
scanf("%f", &a);
printf("Second number\n");
scanf("%f", &b);
c = a / b;
printf("%f", c);
回答by Claudio Cortese
You have to use floator doublevariables, not int(integer) ones. Also note that a division between two integers will lead to an integer result, meanwhile a division between a float/doubleand an integer will lead to a float result. That's because C implicitly promote this integer to float.
您必须使用float或double变量,而不是int(整数)变量。另请注意,两个整数之间的除法将导致整数结果,同时float/double和整数之间的除法将导致浮点结果。那是因为 C 隐式地将此整数提升为float.
For example:
例如:
5/2 = 2
5/2.0f = 2.5f
Note the .0f, this actually means that we are dividing with a float.
注意.0f,这实际上意味着我们正在用浮点数进行除法。
回答by user9598609
In C, only an int type number is displayed. 5/2 gives a floating point type number. So, the compiler compiles it only with the integer value.
在 C 中,只显示一个 int 类型的数字。5/2 给出一个浮点类型数。因此,编译器仅使用整数值编译它。

