4

Consigne: each integer is in the inclusive range 1 - 109.

I use variable with the type unsigned long long int

Is it enough for the stated range?

Melinsuna
  • 193
  • 1
  • 10
  • 1
    it's platform dependent. Use `stdint.h` and `uint64_t` (note that `uint32_t` is enough for such ranges) – Jean-François Fabre Apr 04 '18 at 14:25
  • 1e10 is outside 32 bit range, yes. – Jean-François Fabre Apr 04 '18 at 14:28
  • 1
    @Jean-FrançoisFabre: But you're correct here of course. I'll stop trolling your comments ;-) – Bathsheba Apr 04 '18 at 14:29
  • No, don't use `uint64_t` unless you need a type with 64 bits *and no more*. `uint_least64_t` is more portable (at least theoretically). – Toby Speight Apr 04 '18 at 14:30
  • What does “each integer” mean? Each integer your program will read and store in an object? Or will your program do various arithmetic on the values, so that, although the input values are limited to 10^9, more bits are needed to accommodate the arithmetic that will be performed on them? – Eric Postpischil Apr 04 '18 at 15:13
  • @Jean-FrançoisFabre: It's not platform-dependent. The C language defines minimum acceptable ranges for each type, and even plain `long` is sufficient for OP's needs. – R.. GitHub STOP HELPING ICE Apr 04 '18 at 15:50

4 Answers4

5

Yes it is (assuming that you are adopting the notation 10^9 to mean 1e9 - ^ is the XOR operator in C, and 10^9 is 3).

An unsigned long long has to be capable of storing a number between 0 and 264 - 1. Your maximum is much smaller than this.

Note also that an unsigned long has to be capable of storing a number between 0 and 232 - 1. Your maximum is also smaller than this.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
5

10^9 is way smaller than 2^32

So in your case, no need to use unsigned long long (it fits, yes), which is overkill and can lead to slower operation.

Use the proper type, normalized in stdint.h include: uint32_t or uint_least32_t (uint32_t vs uint_fast32_t vs uint_least32_t)

long is also guaranteed to be at least 32 bits, so it's a good & simple choice as well.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
1

Long long unsigned integer type. Contains at least the [0, +18,446,744,073,709,551,615] range; Specified since the C99 version of the standard.

source: https://en.m.wikipedia.org/wiki/C_data_types

BJTH
  • 11
  • 1
  • As the page says: That applies to C99. Before that the rules were something something like 16 bit <= int <= long <= long long But it's always best to use the uintXX_t types from stdint.h – Morty Apr 04 '18 at 14:32
  • I'm not sure, but correct me if I'm wrong. In pre C99, the type `long long` did actually exist? or was it only a `gcc` extension? – Luis Colorado Apr 06 '18 at 07:12
0

The answer is yes, but you don't need unsigned long long, unsigned long would be enough.

A 32 bit number can represent over 4 * 10^9. Up to 4 294 967 296.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
imreal
  • 10,178
  • 2
  • 32
  • 48