Home > front end >  Difference between long and long long
Difference between long and long long

Time:11-01

what is the difference between The long and long long. i'm using 64bit windows sysem. for me sizes and ranges of long and long long are same. Answer me in detail

#include<stdio.h>
int main()
{
    printf("long size: %d\n",sizeof(long));
    printf("long long size: %d\n\n",sizeof(long long));
    long value1=9223372036854775807;
    long long value2=9223372036854775807;
    printf("Max Limits\n");
    printf("value1: %li\n",value1);
    printf("value2: %lli\n\n",value2);
    //overflow of the range
    value1  ;
    value2  ;
    printf("value1: %li\n",value1);
    printf("value2: %lli\n",value2);

    
    /* OutPut OutPut OutPut
    long size: 8
    long long size: 8

    Max Limits
    value1: 9223372036854775807
    value2: 9223372036854775807
 
    value1: -9223372036854775808
    value2: -9223372036854775808*/

}

CodePudding user response:

C types are intended to be flexible, so they can be adapted to different situations in different C implementations. Because of this, in some C implementations, long and long long are the same width (number of bits used for the value) and size (total number of bytes, including padding). (Some other types can be the same size. For example, short and int can be the same.)

In a C implementation where long and long long are the same width and size, there is no arithmetic difference between them. There are technical differences. In particular, a pointer to a long is a different type from a pointer to a long long, and you should not use one where the other is expected.

CodePudding user response:

Difference between long and long long.

long has a minimum range of /-(231 - 1).

long long has a minimum range of /-(263 - 1).

Code below has different portability.

long      value1 = 9223372036854775807;  // Not always portable.
long long value2 = 9223372036854775807; // OK

Even when long and long long have the same range, (they may differ in other implementations), they remain different types and so create different functionality with _Generic.

#define foo(X) _Generic((X), \
long: "Blue", \
long long: "No red!" \
)

int main() {
  puts(foo(1L));
  puts(foo(1LL));
}

Output

Blue
No red!
  • Related