设为首页 加入收藏

TOP

2.2 Literal Constants (3)
2013-10-07 15:25:02 来源: 作者: 【 】 浏览:70
Tags:2.2 Literal Constants

int and larger values are type long. By adding a suffix, we can force the type of a literal integer constant to be type long or unsigned or unsigned long. We specify that a constant is a long by immediately following the value with either L or l (the letter “ell” in either uppercase or lowercase).

根据语法, 八进制或十六进制的整数字面常量有可能是unsigned 类型(unsigned int 或unsignedlong),而十进制的整数字面常量只能是int 或long 类型。不过代码中还是直接以后缀标明类型为佳。

When specifying a long, use the uppercase L: the lowercase letter l is too easily mistaken for the digit 1.

In a similar manner, we can specify unsigned by following the literal with either U or u. We can obtain an unsigned long literal constant by following the value by both L and U. The suffix must appear with no intervening space:

  1. 128u /* unsigned */ 1024UL /* unsigned long */ 
  2. 1L /* long */ 8Lu /* unsigned long */ 

There are no literals of type short.

注意:整数字面常量没有负数,代码中出现的形如“-1”的数是个表达式,表示1 这个字面常量的相反数(详见第5.1 节:一元取反操作符)。

Rules for Floating-Point Literals

We can use either common decimal notation or scientific notation to write floatingpoint literal constants. Using scientific notation, the exponent is indicated either by E or e. By default, floating-point literals are type double. We indicate single
precision by following the value with either F or f. Similarly, we specify extended precision by following the value with either L or l (again, use of the lowercase l is discouraged). Each pair of literals below denote the same underlying value:

3.14159F .001f 12.345L 0.
3.14159E0f 1E-3F 1.2345E1L 0e0

Boolean and Character Literals

The words true and false are literals of type bool:

  1. bool test = false

Printable character literals are written by enclosing the character within single quotation marks:

  1. 'a'   '2'   ','  ' ' // blank 

注意:一对单引号之间只能有一个字符,否则编译器会给出警告。

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇2.2 Literal Constants (4) 下一篇2.2 Literal Constants (2)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·【超详细】JDK 下载 (2025-12-24 18:19:32)
·Java_百度百科 (2025-12-24 18:19:29)
·简介 - Java教程 - (2025-12-24 18:19:27)
·C++ 语言社区-CSDN社 (2025-12-24 17:48:24)
·CSDN问答专区社区-CS (2025-12-24 17:48:22)