简体   繁体   中英

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 . i want the quotient part ie for 64/20 i want 3 and don't care about the remainder. i am at loss about the error since int should truncate.

You should post the error and format your code correctly.

But your problem is that min is uninitialized.

That's obviously not your code though since that's not compliable.

Min needs to be initialized. 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. The fact that it is dividing by and being placed into an int will cause the remainder part to drop.

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);

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