3

I did the following experiment to know the size of long int and int in my system

System spec: 64 bit windows 7 gcc MinGW compiler eclipse CDT

I got confused with the output I got.. I have no reason to support the result of my program, if anyone has any idea on this please share it and help me.

Program:

#include<stdio.h>
#include<conio.h>
int main(){

    unsigned long int b;
    unsigned  int a;

    printf("%d",sizeof(b));
    printf("\n");
    printf("%d",sizeof(a));

 }

OUTPUT:

4

4

Online GCC compier version 4.8.1 for the same program gives the different output

8

4

enter image description here

Vishwanath gowda k
  • 1,675
  • 24
  • 26
  • The proper way to print values of type `size_t` is `%zu`, from C99. In previous versions, `%lu` and a cast to `(unsigned long)`. `%d` is for `int` which is signed, something `size_t` is not. – unwind Mar 12 '14 at 07:18
  • If you want 64 bits, use `unsigned long long int b;` – user3386109 Mar 12 '14 at 07:23
  • 1
    MinGW is a 32-bit compiler so the sizes are conform to 32-bit windows – phuclv Mar 12 '14 at 07:27
  • @Lưu Vĩnh Phúc k but why the sizes are same..? that is my question.. what's the use of long int when there is int which can store 4 bytes of data..? – Vishwanath gowda k Mar 12 '14 at 07:53
  • because compilers can have different type sizes as long as it conforms to the standard. GCC are from *nix so long and long int are 64-bit like common in *nix operating systems – phuclv Mar 12 '14 at 08:34
  • The C standard doesn't prohibit them to be equal. For example you can see that in 16-bit x86 short and int both are 16 bits, why a short must be shorter than int? – phuclv Mar 12 '14 at 08:40

2 Answers2

3

According to documentation on the size of datatypes, your results are correct.

long int (both signed and unsigned) and int (both signed and unsigned) are both 32 bits on a 64-bit Windows installation, so they would show up as 4 bytes.

cf-
  • 8,598
  • 9
  • 36
  • 58
  • K i accept that but why they are same.. then what's the use of long int.. – Vishwanath gowda k Mar 12 '14 at 07:17
  • 1
    @Vishwanathgowdak They're not *always* the same. According to that table, on a 16-bit Windows setup or a 64-bit Unix-like system (Linux, Unix, Mac, etc.) the types will have different sizes. – cf- Mar 12 '14 at 07:20
  • "They're not always the same".. what are the difference then..? if the sizes of int and long int are same in 32 bit compiler, 64 bit os then why do we need long int.. is there any special purpose of it. – Vishwanath gowda k Mar 12 '14 at 07:55
  • @Vishwanathgowdak See [this paper](http://www.unix.org/version2/whatsnew/lp64_wp.html) for a full explanation of why Unix OS devs considered the LP64 data model superior to LLP64. A short summary is that `int` was left 32-bit for size/alignment compatibility with 32-bit systems, but a 64-bit `long` was considered more natural than a 32-bit `long` for pointer/addressing reasons. The paper also points out that the LLP64 model is forced to define `long long`, a nonstandard (until C++11) 64-bit size. – cf- Mar 12 '14 at 08:10
3

the specs say that
sizeof(int) <= sizeof(long) but at least 32bit for long and 16bit for int.

http://www.cplusplus.com/doc/tutorial/variables/

(prefix singned or unsigned make no difference for the space they need)

if you want to use a specified bit width i recommend to use int32_t, uint64_t, etc.

linluk
  • 1,650
  • 16
  • 33