简体   繁体   English

在C ++中靠近数字常量的字母?

[英]The letter near a digit constant in C++?

What does the letter near the digit constant mean? 数字常数附近的字母是什么意思? Just for example: 例如:

int number = 0;
float decimal = 2.5f;
number = decimal;

What's difference betweet 2.5f and f2.5 ? betweet 2.5f和f2.5有什么区别? I have already looked in manuals, but I really cant understand it. 我已经看过手册,但是我真的听不懂。 Explaine it me please in a simple format. 请以简单的格式向我解释。

The value 2.5 would be a double, whereas 2.5f is a float. 值2.5是双精度数,而2.5f是浮点数。 Unless you have a specific reason not to, it is generally better to use doubles rather than floats - they are more precise and may even be faster. 除非有特殊原因,否则通常最好使用double而不是float-它们更精确,甚至可能更快。

From here : 这里

Floating-point constants default to type double. 浮点常量默认为double类型。 By using the suffixes f or l (or F or L — the suffix is not case sensitive), the constant can be specified as float or long double, respectively. 通过使用后缀f或l(或F或L-后缀不区分大小写),可以将常数分别指定为float或long double。

I don't thing the format f2.5 is legal. 我认为f2.5格式不合法。

What's difference betweet 2.5f and f2.5 betweet 2.5f和f2.5有什么区别

  • 2.5f is the value 2.5 as a floating point number, 2.5f是值为2.5的浮点数,
  • f2.5 is a variable called "f2" followed by ".5" which would be a syntax error. f2.5是一个名为“ f2”的变量,后跟“ .5”,这是语法错误。

Purely as additional information (not really a direct answer to the question) I'd note that to specify the type of a character or string literal, you use a prefix (eg, L"wide string"), whereas with a numeric literal you use a suffix (eg, 2L or 3.5f). 纯粹作为附加信息(实际上并不是对问题的直接答案),我注意到要指定字符或字符串文字的类型,您可以使用前缀(例如L“宽字符串”),而对于数字文字,您可以使用后缀(例如2L或3.5f)。

C++0x adds quite a few more of both prefixes and suffixes to specify more data types (eg, there are currently only narrow and wide string literals, but C++0x will have narrow, wide, Unicode, raw, and probably at least a couple more I can't think of at the moment). C ++ 0x会同时在前缀后缀中添加很多以指定更多的数据类型(例如,目前只有窄和宽字符串文字,但是C ++ 0x将具有窄,宽,Unicode,原始且可能为至少我目前无法想到的几个)。 It also adds user-defined literals that let you define your own suffixes, so something like 150km could be used to create a distance object, or "127.0.0.1"ip to create an IP_address object. 它还添加了用户定义的文字,使您可以定义自己的后缀,因此可以使用诸如150km东西来创建distance对象,或者使用"127.0.0.1"ip来创建IP_address对象。

f2.5 is illegal. f2.5是非法的。 2.5f (or 2.5F ) forces 2.5 to be interpreted as a float, not as double. 2.5f (或2.5F )强制将2.5解释为浮点数,而不是两倍。 Relatedly, there's the l / L suffix to make integer constants long s. 相关地,有l / L后缀使整数常量long s。

number = decimal truncates the latter, I suppose. 我想number = decimal截断后者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM