In C, why are there parentheses around (int) in th

2019-09-01 02:01发布

This question already has an answer here:

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;?

3条回答
走好不送
2楼-- · 2019-09-01 02:34

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.

查看更多
做个烂人
3楼-- · 2019-09-01 02:36

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

查看更多
4楼-- · 2019-09-01 02:36

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
查看更多
登录 后发表回答