Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
797 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 Reply

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...