Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

This might be very basic but I got very curious what the reason is.

When dealing with different types of operations such as multiplication and division with data by different types (int, float etc) what decides which datatype that gets picked?

For example, if I do the following:

float a = 5 / 10;

I will get "0" as result since the 5 and the 10 are temporarily stored in an int where we do the division, and then we put it in a float. Right?

But if we instead do:

float a = (float)5 / 10;

We get 0.5 instead.

How does the decision making look when float is prefered over int in this case in C?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
745 views
Welcome To Ask or Share your Answers For Others

1 Answer

Different languages handle this differently; the specific rules regarding type conversion are part of the language specification.

The rules for C are described in the ANSI specification, and more conveniently here: http://www.eskimo.com/~scs/cclass/int/sx4cb.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...