In C, why are there parentheses around (int) in this example? [duplicate]

╄→尐↘猪︶ㄣ 提交于 2019-12-12 03:37:11

问题


decimal d = 2;
int i = (int) d;

I've seen this several times in which parentheses are wrapped around data types.

Why not just use int i = int d;?


回答1:


The usage of (int) is called casting (or, type-casting). It is essentially telling that, interpret convert the value of d as to an int (integer) and store it into i.

In other words, it is a way of converting a type to another one (subject to validity of the conversion).

BTW, int i = int d;, as is, is not a valid statement.




回答2:


You're casting d to type int from type decimal. This happens in other languages as well that use static typing.




回答3:


It is a way of converting data type from one type to another using cast operator, usage is as follows:

  • (result data type) expression to be converted


来源:https://stackoverflow.com/questions/30790649/in-c-why-are-there-parentheses-around-int-in-this-example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!