What should be the sizeof(int) on a 64-bit machine? [duplicate]

Possible Duplicate:
size of int, long, etc
Does the size of an int depend on the compiler and/or processor?
What decides the sizeof an integer?

I'm using a 64-bit machine.

$ uname -m
x86_64
$ file /usr/bin/file
/usr/bin/file: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, stripped
$ 

When I ran the following program, I got the sizeof(int) as 4-bytes.

#include <stdio.h>

int main(void)
{
    printf("sizeof(int) = %d bytes\n", (int) sizeof(int));

    return 0;
}

If I'm running a 16-, 32- and 64- bit machine, then doesn't it mean that the size of an integer is 16-, 32- and 64- bit respectively?

In my machine, I found the WORD_BIT is 32. Shouldn't it be 64 on a 64-bit machine?

$ getconf WORD_BIT
32
$ 

And, shouldn't the sizeof(int) be 64-bits (8 bytes) in the above case?


Solution 1:

Size of a pointer should be 8 byte on any 64-bit C/C++ compiler, but not necessarily size of int.

Solution 2:

Doesn't have to be; "64-bit machine" can mean many things, but typically means that the CPU has registers that big. The sizeof a type is determined by the compiler, which doesn't have to have anything to do with the actual hardware (though it typically does); in fact, different compilers on the same machine can have different values for these.

Solution 3:

Not really. for backward compatibility it is 32 bits.
If you want 64 bits you have long, size_t or int64_t

Solution 4:

In C++, the size of int isn't specified explicitly. It just tells you that it must be at least the size of short int, which must be at least as large as signed char. The size of char in bits isn't specified explicitly either, although sizeof(char) is defined to be 1. If you want a 64 bit int, C++11 specifies long long to be at least 64 bits.