-2

I want a datatype that can hold the value 12,000,000,000 (twelve billion) in C. Which datatype does accept this value?

I have tried long int and long long int for it.

A10
  • 15
  • 5

2 Answers2

2

12,000,000,000 can certainly fit in a long long and higher ranking types as long long is specified to have a range at least [−(2^63 − 1) ... +(2^63 − 1)] or 9,223,372,036,854,775,807.

long long twelve_mil = 12000000000;  // No suffix needed

Lower ranking types like unsigned long, long , unsigned, int may also work. Example: C specifies that a long has a minimum range of [-2147483647 ... +2147483647], but it may be more.

#if 12000000000 >= INT_MAX
  int twelve_mil = 12000000000;
  printf("%d\n", twelve_mil);
#elif 12000000000 >= LONG_MAX
  long twelve_mil = 12000000000;  
  printf("%ld\n", twelve_mil);
#else 
  long long twelve_mil = 12000000000;  
  printf("%lld\n", twelve_mil);
#endif

We could extend this to consider even lower ranking types like unsigned short, short , unsigned char signed char and even char. Not fruitful on many machines.

Code could consider using int64_t. That common, yet optional type is defined in #include <stdint.h>. Also declared there is int_least64_t, which is always available since C99.

#include <stdint.h>

// for format specifiers
#include <inttypes.h>

int_least64_t twelve_mil = 12000000000;
printf("%" PRIdLEAST64 "\n", twelve_mil);
// or
int64_t twelve_mil = 12000000000;  // Commonly available
printf("%" PRId64 "\n", twelve_mil);
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • How could I declare a[12000000000] in C. – A10 Jul 29 '16 at 17:01
  • And what is the format specifier for that. – A10 Jul 29 '16 at 17:02
  • @AniketSaxena I recommend you to become familiar with the very basics of C at a tutorial site. http://www.learn-c.org/ is a great place to start. – plasmacel Jul 29 '16 at 17:04
  • @Aniket Saxena To declare such a large array, some platforms may accept `some_type a[12000000000];` Many will not. Allocating is the next consideration. `some_type *a = malloc(sizeof *a * 12000000000);`. With array size and allocations, code should use the type `size_t`. `size_t sz = 12000000000; some_type *a = malloc(sizeof *a * sz);` If `SIZE_MAX < 12000000000`, then other approaches are needed. – chux - Reinstate Monica Jul 29 '16 at 17:06
  • OK but please suggest me the answer for that... – A10 Jul 29 '16 at 17:08
  • @AniketSaxena Unclear on "what is the format specifier for that".. What is "that" and for which function is code applying the desired format specifier? Code does not _need_ a suffix to express 12000000000. Just code `long long sz = 12000000000;` – chux - Reinstate Monica Jul 29 '16 at 17:08
  • @AniketSaxena concerning `a[12000000000]`: best to append that question to your post rather that only request info on it in comments. – chux - Reinstate Monica Jul 29 '16 at 17:27
  • @chux It seems to me that the OP is working on a homework and he only wants some instant solutions instead of taking time to learn about the language. It's hard to help if the other peer doesn't show any effort to help himself. – plasmacel Jul 29 '16 at 17:28
  • @plasmacel Yes - of course - yet I was simply being patient - we've all been in a hurry sometimes. It could take some time for OP to respond. – chux - Reinstate Monica Jul 29 '16 at 17:32
  • Actually the signals are not coming well here in my area that's why I am responding late... – A10 Jul 29 '16 at 17:40
  • I do not think they are _late_. Proceed at your pace. – chux - Reinstate Monica Jul 29 '16 at 17:55
  • OK I will post the question for a[12000000000] bcoz when I run the program it shows me a dialog box:A problem caused the program to stop correctly. – A10 Jul 29 '16 at 18:02
  • @AniketSaxena Did you try the suggestions in this [comment](http://stackoverflow.com/questions/38662720/data-type-in-c-language-for-value-12000000000/38663932?noredirect=1#comment64709019_38663932)? How did the 2 ideas work for you? Code likely needs to allocate memory. What _type_ is your array? – chux - Reinstate Monica Jul 29 '16 at 18:27
0

Use long long int and unsigned long long int, or the fixed size versions int64_t and uint64_t located in stdint.h. The unsigned versions has twice the max value of the signed ones. Any of them can hold your desired value.

long long int x0 = 12000000000ll;
unsigned long long int x1 = 12000000000ull;

or

#include "stdint.h"

int64_t x0 = 12000000000ll;
uint64_t x1 = 12000000000ull;

You can print the signed long long as printf("%lld", x0);, and the unsigned long long as printf("%llu", x1);.

You can also read more about the size and value range of numeric types at https://msdn.microsoft.com/en-us/library/s3f49ktz.aspx

plasmacel
  • 8,183
  • 7
  • 53
  • 101
  • 1
    Why would a `long int` not work in this case? – Daniel Margosian Jul 29 '16 at 15:49
  • 1
    @DanielMargosian `long int` might be the same size as `int`. Also it can not be hold in `int32_t`. – BLUEPIXY Jul 29 '16 at 15:53
  • `long int` is usually `int32_t` on most systems. the standard says that `long int` must be at least 32 bits long. see http://stackoverflow.com/questions/589575/what-does-the-c-standard-state-the-size-of-int-long-type-to-be – plasmacel Jul 29 '16 at 15:59
  • You don't need the suffixes for the constants. You should link to a C answer. Oh, and those are minimum ranges, not the actual ranges. – 2501 Jul 29 '16 at 16:13
  • The size of the numeric types are the same in C and C++. It would be a disaster if it were not. On the other hand it's a good practice to use suffixes. It's error prone and indicates the order of magnitude of a value. – plasmacel Jul 29 '16 at 16:14
  • Even if the *minimum ranges* for integer types are the same, it is still a different language. – 2501 Jul 29 '16 at 16:16
  • The standard only specifies the minimum ranges. The answer to the OP's question is perfectly covered by the link. I update my answer and link the MSDN reference of fundamental types, where C/C++ types are also discussed together... – plasmacel Jul 29 '16 at 16:24
  • The msdn link is actually incorrect about the ranges. – 2501 Jul 29 '16 at 16:29
  • @2501 Feel free to update my answer with a reliable link of detailed description of fundamental C types, which doesn't mention C++ and lists size and range of each type. I feel it a pointless nitpicking at this point. – plasmacel Jul 29 '16 at 16:35
  • I can do that if you mark the answer as a community answer. – 2501 Jul 29 '16 at 16:36
  • unit_64 is not working – A10 Jul 29 '16 at 16:56
  • @AniketSaxena Where did you see `unit_64` in my answer? – plasmacel Jul 29 '16 at 16:57
  • Oops sorry!! I mean uint64_t – A10 Jul 29 '16 at 17:04
  • @AniketSaxena I think you don't print the value properly. Use `printf("%llu", x1);`. – plasmacel Jul 29 '16 at 17:11
  • long long is working for a=12000000000 but not for a[12000000000]…please answer me how to resolve it… – A10 Jul 29 '16 at 17:20
  • 1
    What you mean it doesn't work? You get a compilation error? Runtime error? If you want to declare an array of 12000000000 elements, then you need to specify the type of the array elements like: `char a[12000000000];`. This is an array of 12000000000 `char` values. If you exceed the maximum size of the memory you get runtime error. See http://stackoverflow.com/questions/9386979/the-maximum-size-of-an-array-in-c – plasmacel Jul 29 '16 at 17:23
  • The msg in the dialog box I got: A problem caused the program to stop correctly. – A10 Jul 29 '16 at 17:28