What is the difference between long, long long, long int, and long long int

This blog post explains the difference between a long, long long, long int, and long long int. You will learn here issues with int, long int, and long long int with some example codes.

But before starting the blog post, I want to make you clear that long and long int are identical and also long long and long long int. In both cases, the int is optional.

There are several shorthands for built-in types. Let’s see some examples of signed built-in types.

short int >>> short

long int  >>> long

long long int >>> long long

 

The standard only explained the minimum size of int, long int, and long long int.  Let’s see the below table which explains the size of int, long int, and long long int according to standard,

int must be at least 16 bits

long must be at least 32 bits

long long must be at least 64 bits

 

So if we will arrange ‘int’, ‘long int’, and ‘long long int’ in the increasing order, then the order would be,

sizeof(int) <= sizeof(long) <= sizeof(long long)

It is clear from the above discussion that the main difference between long and long long is their range. Also, standard mandates minimum ranges for each, and that long long is at least as wide as long.

So you can see that size is not fixed for the above-mentioned inbuilt types. But if you need a specific integer size for a particular application, then good news for you, the standard already introduced fixed-width integers. You can use these fixed-width integers by including the header file #include <stdint.h> (or <cstdint>). Let’s see some newly defined fixed-width integer types,

Size Signed Unsigned
8-bit: int8_t uint8_t
16-bit: int16_t uint16_t
32-bit: int32_t uint32_t
64-bit: int64_t uint64_t

 

#include <stdio.h>
#include <stdint.h>

int main()
{
    //signed
    printf("sizeof(int8_t)   = %zu\n", sizeof(int8_t));
    printf("sizeof(int16_t)  = %zu\n", sizeof(int16_t));
    printf("sizeof(int32_t)) = %zu\n", sizeof(int32_t));
    printf("sizeof(int64_t)  = %zu\n", sizeof(int64_t));
    //unsigned
    printf("sizeof(uint8_t)  = %zu\n", sizeof(uint8_t));
    printf("sizeof(uint16_t) = %zu\n", sizeof(uint16_t));
    printf("sizeof(uint32_t) = %zu\n", sizeof(uint32_t));
    printf("sizeof(uint64_t) = %zu\n", sizeof(uint64_t));
    return 0;
}

Output:

Fixed width integer types

 

Recommended Post for you