How can I deal with lots of precision warning flags?
The following simple code
int generated;
generated = (random() % 100) + 1;
gives a warning flag for loss of precision, 'long' to 'int', so I have been correcting it by rewriting the assignment code as
generated = ((int)random() % 100) + 开发者_C百科1;
Is this a valid way of correcting the problem or am I just covering up errors elsewhere?
You can also use long for your constants:
generated = (random() % 100L) + 1L;
Note that this assume that generated is long.
EDIT: Since generated is an int, you just need to cast it after you are done:
generated = (int)((random() % 100L) + 1L);
In your example you will be truncating the random()
result too early. You need to cast the mod operation.
int generated;
generated = (int)(random() % 100) + 1;
精彩评论