如何存储整数值等于10 ^ 18在C程序或C ++? [英] How to store a integer value equal to 10^18 in c programs or c++?

查看:785
本文介绍了如何存储整数值等于10 ^ 18在C程序或C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么可以存储在C变大的整数值?
如果我声明与 int类型的; 将无法正常工作。
我与使用该得到long long int 。它是行不通的。

 如果(A> = 0&放大器;&安培; A< =(1000000000000000000))

什么来声明一个变量,这样就不会那么任何error.It应该是整数。

 编译器错误
整型常量是long类型太大。


解决方案

让我们看看你写了这个如果语句:

 如果(A> = 0&放大器;&安培; A< =(1000000000000000000))

1000000000000000000 太大为一体的文字,所以你需要一个更大的文字类型。你应该申报 A 的int64_t 和不喜欢该水平的研究:

 如果(A> = INT64_C(0)及和放大器; A< = INT64_C(1000000000000000000))

请注意,这将只在 C99 C ++ 11 编译器,当你<$ C $工作C>的#include&LT; cstdint&GT; 或的#include&LT; stdint.h&GT;

编辑:在目前的标准草案,你可以找到这个句子(2.14.2 / 2):


  

类型字面的整数的是第一相应列表的
  表6在其值可以被重新presented


这意味着,编译器会自动使用所需的文本类型,使您的文字配合。顺便说一句,我没有看到那种编译器。

How can I store a large integer value in a variable of C ? If i am declaring a with int a; it won't work. I have used this with long long int.It is not working.

if( a>=0 && a <= (1000000000000000000))

What to declare variable a so that it will not so any error.It should be integer.

Compiler error
integer constant is too large for long type.

解决方案

Lets look at this if statement you wrote:

if( a>=0 && a <= (1000000000000000000))

1000000000000000000 is too big for an integral literal so you will need a bigger literal type. You should declare a as an int64_t and do the comparion like that:

if( a>=INT64_C(0) && a <= INT64_C(1000000000000000000))

Note that this will only work in a C99 or C++11 compiler when you #include <cstdint> or #include <stdint.h>

Edit: in current draft of the standard you can find this sentence (2.14.2/2):

The type of an integer literal is the first of the corresponding list in Table 6 in which its value can be represented.

It means that compiler should use the required literal type automatically to make your literal fit. Btw I didn't see that kind of compiler.

这篇关于如何存储整数值等于10 ^ 18在C程序或C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆