【请教】C语言中的常量后面加L或者U的作用是什么?

p97 2016-06-22 04:44:22
我知道在常量后面加L表示long int(同理加u表示unsigned int),但是这背后改变的是这个常量占的存储空间?
还是说如果在赋值的时候执行:
int i=345L

变量i就被转换成了long int i?
加u也是同理?
那么我一开始声明i的时候就
long int=345
是不是就等效于上面那行?
那加L还有什么别的用处吗?

谢谢各位我见过的代码实在是太少了,希望前辈能举些应用的例子,谢谢各位!
...全文
2587 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2016-06-29
  • 打赏
  • 举报
回复
常量也有类型!
ForestDB 2016-06-29
  • 打赏
  • 举报
回复
int i=345L 这里不是i变成了long,而是345变成了long,因为345本来是int; 把long(345L)赋值给int(i),这里有cast发生。 long int=345 这里是把int(345)赋值给long(i),也有cast发生。 int i = 345; long i = 345L; 这里的赋值没有cast。 对于细节,可以暂时不用细抠,有概念有意识即可,真正碰到问题的时候再来理解。
赵4老师 2016-06-22
  • 打赏
  • 举报
回复
Visual C++ Language Reference C++ Floating-Point Constants See Also Send Feedback Floating-point constants specify values that must have a fractional part. These values contain decimal points (.) and can contain exponents. Floating-point constants have a "mantissa," which specifies the value of the number, an "exponent," which specifies the magnitude of the number, and an optional suffix that specifies the constant's type. The mantissa is specified as a sequence of digits followed by a period, followed by an optional sequence of digits representing the fractional part of the number. For example: Copy Code 18.46 38. The exponent, if present, specifies the magnitude of the number as a power of 10, as shown in the following example: Copy Code 18.46e0 // 18.46 18.46e1 // 184.6 The exponent may be specified using e or E, which have the same meaning, followed by an optional sign (+ or -) and a sequence of digits. If an exponent is present, the trailing decimal point is unnecessary in whole numbers such as 18E0. Floating-point constants default to type 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. Although long double and double have the same representation, they are not the same type. For example, you can have overloaded functions like Copy Code void func( double ); and Copy Code void func( long double ); See Also Concepts Literals (C++) Send feedback on this topic to Microsoft.
赵4老师 2016-06-22
  • 打赏
  • 举报
回复
Visual C++ Language Reference C++ Integer Constants See Also Send Feedback Integer constants are constant data elements that have no fractional parts or exponents. They always begin with a digit. You can specify integer constants in decimal, octal, or hexadecimal form. They can specify signed or unsigned types and long or short types. Grammar integer-constant: decimal-constant integer-suffixopt octal-constant integer-suffixopt hexadecimal-constant integer-suffixopt 'c-char-sequence' decimal-constant: nonzero-digit decimal-constant digit octal-constant: 0 octal-constant octal-digit hexadecimal-constant: 0xhexadecimal-digit 0Xhexadecimal-digit hexadecimal-constant hexadecimal-digit nonzero-digit: one of 1 2 3 4 5 6 7 8 9 octal-digit: one of 0 1 2 3 4 5 6 7 hexadecimal-digit: one of 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F integer-suffix: unsigned-suffix long-suffixopt long-suffix unsigned-suffixopt unsigned-suffix: one of u U long-suffix: one of l L 64-bit integer-suffix: i64 LL ll To specify integer constants using octal or hexadecimal notation, use a prefix that denotes the base. To specify an integer constant of a given integral type, use a suffix that denotes the type. To specify a decimal constant, begin the specification with a nonzero digit. For example: Copy Code int i = 157; // Decimal constant int j = 0198; // Not a decimal number; erroneous octal constant int k = 0365; // Leading zero specifies octal constant, not decimal To specify an octal constant, begin the specification with 0, followed by a sequence of digits in the range 0 through 7. The digits 8 and 9 are errors in specifying an octal constant. For example: Copy Code int i = 0377; // Octal constant int j = 0397; // Error: 9 is not an octal digit To specify a hexadecimal constant, begin the specification with 0x or 0X (the case of the "x" does not matter), followed by a sequence of digits in the range 0 through 9 and a (or A) through f (or F). Hexadecimal digits a (or A) through f (or F) represent values in the range 10 through 15. For example: Copy Code int i = 0x3fff; // Hexadecimal constant int j = 0X3FFF; // Equal to i To specify an unsigned type, use either the u or U suffix. To specify a long type, use either the l or L suffix. For example: Copy Code unsigned uVal = 328u; // Unsigned value long lVal = 0x7FFFFFL; // Long value specified // as hex constant unsigned long ulVal = 0776745ul; // Unsigned long value To specify a 64-bit integral type, use the LL, ll or i64 suffix. For example, Copy Code // 64bitsuffix.cpp #include <stdio.h> enum MyEnum { IntType, Int64Type }; MyEnum f1(int) { printf("in f1(int)\n"); return IntType; } MyEnum f1(__int64) { printf_s("in f1(__int64)\n"); return Int64Type; } int main() { MyEnum t1 = f1(0x1234), t2 = f1(0x1234i64); } Output in f1(int) in f1(__int64) See Also Concepts Literals (C++) Send feedback on this topic to Microsoft.
小灸舞 2016-06-22
  • 打赏
  • 举报
回复
如果数字后面不加L,默认的取值范围是int(整型)
比如:
给a赋值:long a=2147483648; (数字超出int型取值范围)
给a赋值:long a=2147483648L;(正确,说明此时可以是long型的取值范)

U表示该常数用无符号整型方式存储,相当于unsigned int;

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧