简体   繁体   English

C中的整数除法

[英]integer division in C

Following is a code segment i am using: 以下是我正在使用的代码段:

int q;
int cacheline = 64;
int min;

q = cacheline/min;

when run, this always gets an exception for q . 运行时,这始终会导致q异常。 i want the quotient part ie for 64/20 i want 3 and don't care about the remainder. 我想要商数部分,即对于64/20,我想要3,而不关心余数。 i am at loss about the error since int should truncate. 我不知道该错误,因为int应该截断。

You should post the error and format your code correctly. 您应该发布错误并正确设置代码格式。

But your problem is that min is uninitialized. 但是您的问题是min未初始化。

That's obviously not your code though since that's not compliable. 显然,那不是您的代码,因为那是不兼容的。

Min needs to be initialized. Min需要初始化。 It is currently undefined. 当前未定义。

In order to get the truncated integer division, all you would need to do is to initialize min and do the division. 为了得到截断的整数除法,您需要做的是初始化min并进行除法。 The fact that it is dividing by and being placed into an int will cause the remainder part to drop. 它被除以并放入一个int的事实将导致其余部分下降。

This worked fine for me.... so as Falmarri,Joey, Mark said check your code !! 这对我来说很好用。...法尔玛瑞,乔伊,马克说要检查您的代码! int q; int c = 64; int m = 20; q = c/m; printf("output:%d",q);

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

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