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

Categories

0 votes
801 views
in Technique[技术] by (71.8m points)

c - What decides what datatype that will be used to store the temporary value in?

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

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

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...