8 个解决方案
#1
加L表示是长整型,不加的话,那么就是一个整形了。
#2
但是我不是定义了t1 ,t2是long 类型吗?
那什么时候要加后缀?
#3
后面一个有转型
#4
不加的话是把5看做是一个int型再转成long类型。低位到高位的转换这样没什么影响
如果加L就直接是long类型了,不用转换
如果加L就直接是long类型了,不用转换
#5
不加的话是把5看做是一个int型再转成long类型 //这不是始终还是long类型吗?
不加的话,是不是在调用函数时,假设原型如下
void F (long &)
是不是会类型不对?
在定义long类型时要使用L后缀吗?那么应该什么时候使用后缀?
#6
如果是long t1 =5L 与long t2=5 这个例子的话,没有任何区别
long t1 =5L 是把long型的5 赋值给long型的t1
long t2 =5 是把int型的5 赋值给long型的t2,由于int->long是无损的,所有这两者没有任何区别
那什么时候用L呢,
举个例子, long t3 = 5 * INT_MAX, 这个时候就得看编译器了,编译器有可能把5 * INT_MAX看作int * int, 当然结果还是int, 这个时候数据已经出错了, 所以t3就错了
但如果long t3 = 5L * INT_MAX,由于是long * int,所以结果是long,数据就不会出错,
long t1 =5L 是把long型的5 赋值给long型的t1
long t2 =5 是把int型的5 赋值给long型的t2,由于int->long是无损的,所有这两者没有任何区别
那什么时候用L呢,
举个例子, long t3 = 5 * INT_MAX, 这个时候就得看编译器了,编译器有可能把5 * INT_MAX看作int * int, 当然结果还是int, 这个时候数据已经出错了, 所以t3就错了
但如果long t3 = 5L * INT_MAX,由于是long * int,所以结果是long,数据就不会出错,
#7
C++ Integer Constants
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.
Syntax
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 :
0x hexadecimal-digit
0X hexadecimal-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
C++ Floating-Point Constants
Floating-point constants specify values that must have a fractional part. These values contain decimal points (.) and can contain exponents.
Syntax
floating-constant :
fractional-constant exponent-partopt floating-suffixopt
digit-sequence exponent-part floating-suffixopt
fractional-constant :
digit-sequenceopt . digit-sequence
digit-sequence .
exponent-part :
e signopt digit-sequence
E signopt digit-sequence
sign : one of
+ –
digit-sequence :
digit
digit-sequence digit
floating-suffix :one of
f l F L
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.
Syntax
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 :
0x hexadecimal-digit
0X hexadecimal-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
C++ Floating-Point Constants
Floating-point constants specify values that must have a fractional part. These values contain decimal points (.) and can contain exponents.
Syntax
floating-constant :
fractional-constant exponent-partopt floating-suffixopt
digit-sequence exponent-part floating-suffixopt
fractional-constant :
digit-sequenceopt . digit-sequence
digit-sequence .
exponent-part :
e signopt digit-sequence
E signopt digit-sequence
sign : one of
+ –
digit-sequence :
digit
digit-sequence digit
floating-suffix :one of
f l F L
#8
谢谢大家回答
#1
加L表示是长整型,不加的话,那么就是一个整形了。
#2
但是我不是定义了t1 ,t2是long 类型吗?
那什么时候要加后缀?
#3
后面一个有转型
#4
不加的话是把5看做是一个int型再转成long类型。低位到高位的转换这样没什么影响
如果加L就直接是long类型了,不用转换
如果加L就直接是long类型了,不用转换
#5
不加的话是把5看做是一个int型再转成long类型 //这不是始终还是long类型吗?
不加的话,是不是在调用函数时,假设原型如下
void F (long &)
是不是会类型不对?
在定义long类型时要使用L后缀吗?那么应该什么时候使用后缀?
#6
如果是long t1 =5L 与long t2=5 这个例子的话,没有任何区别
long t1 =5L 是把long型的5 赋值给long型的t1
long t2 =5 是把int型的5 赋值给long型的t2,由于int->long是无损的,所有这两者没有任何区别
那什么时候用L呢,
举个例子, long t3 = 5 * INT_MAX, 这个时候就得看编译器了,编译器有可能把5 * INT_MAX看作int * int, 当然结果还是int, 这个时候数据已经出错了, 所以t3就错了
但如果long t3 = 5L * INT_MAX,由于是long * int,所以结果是long,数据就不会出错,
long t1 =5L 是把long型的5 赋值给long型的t1
long t2 =5 是把int型的5 赋值给long型的t2,由于int->long是无损的,所有这两者没有任何区别
那什么时候用L呢,
举个例子, long t3 = 5 * INT_MAX, 这个时候就得看编译器了,编译器有可能把5 * INT_MAX看作int * int, 当然结果还是int, 这个时候数据已经出错了, 所以t3就错了
但如果long t3 = 5L * INT_MAX,由于是long * int,所以结果是long,数据就不会出错,
#7
C++ Integer Constants
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.
Syntax
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 :
0x hexadecimal-digit
0X hexadecimal-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
C++ Floating-Point Constants
Floating-point constants specify values that must have a fractional part. These values contain decimal points (.) and can contain exponents.
Syntax
floating-constant :
fractional-constant exponent-partopt floating-suffixopt
digit-sequence exponent-part floating-suffixopt
fractional-constant :
digit-sequenceopt . digit-sequence
digit-sequence .
exponent-part :
e signopt digit-sequence
E signopt digit-sequence
sign : one of
+ –
digit-sequence :
digit
digit-sequence digit
floating-suffix :one of
f l F L
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.
Syntax
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 :
0x hexadecimal-digit
0X hexadecimal-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
C++ Floating-Point Constants
Floating-point constants specify values that must have a fractional part. These values contain decimal points (.) and can contain exponents.
Syntax
floating-constant :
fractional-constant exponent-partopt floating-suffixopt
digit-sequence exponent-part floating-suffixopt
fractional-constant :
digit-sequenceopt . digit-sequence
digit-sequence .
exponent-part :
e signopt digit-sequence
E signopt digit-sequence
sign : one of
+ –
digit-sequence :
digit
digit-sequence digit
floating-suffix :one of
f l F L
#8
谢谢大家回答