msc
msc

Reputation: 34568

Why does "unsigned int64_t" give an error in C?

Why does the following program give an error?

#include <stdio.h>

int main() 
{
    unsigned int64_t i = 12;
    printf("%lld\n", i);
    return 0;
}

Error:

 In function 'main':
5:19: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'i'
  unsigned int64_t i = 12;
                   ^
5:19: error: 'i' undeclared (first use in this function)
5:19: note: each undeclared identifier is reported only once for each function it appears in

But, If I remove the unsigned keyword, it's working fine. So, Why does unsigned int64_t i give me an error?

Upvotes: 7

Views: 17282

Answers (4)

Jayesh
Jayesh

Reputation: 4893

typedef of int64_t is something like:

typedef signed long long int int64_t;

So, unsigned int64_t i; becomes something like:

unsigned signed long long int i;  

Which is obviously a compiler error.

So, use int64_t instead of unsigned int64_t.

Also, add #include <stdint.h> header file in your program.

Upvotes: 0

Mischa
Mischa

Reputation: 2288

int64_t is not some builtin type. Try adding #include <stdint.h> to define such types; then use uint64_t which means what you appear to intend.

Upvotes: 6

user694733
user694733

Reputation: 16033

int64_t is a typedef name. N1570 §7.20.1.1 p1:

The typedef name intN_t designates a signed integer type with width N, no padding bits, and a two’s complement representation. Thus, int8_t denotes such a signed integer type with a width of exactly 8 bits.

Standard lists what combinations are legal in §6.7.2 p2:

  • char
  • signed char
  • unsigned char
  • short, signed short, short int, or signed short int
  • unsigned short, or unsigned short int
  • int, signed, or signed int
  • unsigned, or unsigned int
  • long, signed long, long int, or signed long int
  • unsigned long, or unsigned long int
  • long long, signed long long, long long int, or signed long long int
  • unsigned long long, or unsigned long long int

...

  • typedef name

Types that are not relevant to the question have been removed from the list.

Note how you cannot mix typedef name with unsigned.


To use unsigned 64-bit type, you need to:

  • Use uint64_t (note the leading u) without unsigned specifier.

    uint64_t i = 12;
    
  • Include stdint.h (or inttypes.h) where uint64_t is defined.

  • To print uint64_t you need to include inttypes.h and use PRIu64:

    printf("%" PRIu64 "\n", i);
    

    You can also or cast to unsigned long long which is 64-bits or more. However it's preferable to avoid casting when it's not strictly necessary, so the you should prefer PRIu64 method.

    printf("%llu\n", (unsigned long long)i);
    

Upvotes: 2

Emil Laine
Emil Laine

Reputation: 42828

You cannot apply the unsigned modifier on the type int64_t. It only works on char, short, int, long, and long long.

You probably want to use uint64_t which is the unsigned counterpart of int64_t.

Also note that int64_t et al. are defined in the header stdint.h, which you should include if you want to use these types.

Upvotes: 19

Related Questions