用define定义一年有多少毫秒,注意越界的问题

anicepigzh 2010-11-16 11:10:06
百度面试题
#define year 365*24*60*60*1000
这个数应该是300多亿,超过存贮范围了
简单的这样定义好像不行吧
面试的人问了怎么处理越界的问题,不是很清楚
求指教!
...全文
371 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2010-11-16
  • 打赏
  • 举报
回复
#define year 31536000000i64 //365*24*60*60*1000

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

liahr 2010-11-16
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 hai040 的回复:]
#define int64(...)
[/Quote]
正解
hai040 2010-11-16
  • 打赏
  • 举报
回复
这个没办法
怎么也管不了用的人
赋值给int无意义
anicepigzh 2010-11-16
  • 打赏
  • 举报
回复
如果我随便把这个值给了一个int型变量,然后不就越界了吗?
所以在define中就因该定义函数处理这个问题
比如如果是个int型,就取低位的32位
hai040 2010-11-16
  • 打赏
  • 举报
回复
#define int64(...)
anicepigzh 2010-11-16
  • 打赏
  • 举报
回复
请给出用define 处理的代码吧
其实我也说了用longlong不会越界,可是好像还是不够啊
lhy 2010-11-16
  • 打赏
  • 举报
回复
应该是个浮点数吧。
dengqibin 2010-11-16
  • 打赏
  • 举报
回复
用无符号的长整形应该够吧
失落的凡凡 2010-11-16
  • 打赏
  • 举报
回复
用int64
yutaooo 2010-11-16
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 zhao4zhong1 的回复:]

#define year 31536000000i64 //365*24*60*60*1000

C++ Integer Constants
Integer constants are constant data elements that have no fractional parts or exponents. They always begin with a digit. You ……
[/Quote]

居然还有i64这么个后缀呀。这可要顶顶!
lijianbin9 2010-11-16
  • 打赏
  • 举报
回复
int64 是什么意思啊?哪位给介绍一下啊
anicepigzh 2010-11-16
  • 打赏
  • 举报
回复
多谢多谢 9f 还给出了详细的解释
5f,8f的我也明白了

33,311

社区成员

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

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