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

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?

This question is related to c++ c sizeof

The answer is


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


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.


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


Examples related to c++

Method Call Chaining; returning a pointer vs a reference? How can I tell if an algorithm is efficient? Difference between opening a file in binary vs text How can compare-and-swap be used for a wait-free mutual exclusion for any shared data structure? Install Qt on Ubuntu #include errors detected in vscode Cannot open include file: 'stdio.h' - Visual Studio Community 2017 - C++ Error How to fix the error "Windows SDK version 8.1" was not found? Visual Studio 2017 errors on standard headers How do I check if a Key is pressed on C++

Examples related to c

conflicting types for 'outchar' Can't compile C program on a Mac after upgrade to Mojave Program to find largest and second largest number in array Prime numbers between 1 to 100 in C Programming Language In c, in bool, true == 1 and false == 0? How I can print to stderr in C? Visual Studio Code includePath "error: assignment to expression with array type error" when I assign a struct field (C) Compiling an application for use in highly radioactive environments How can you print multiple variables inside a string using printf?

Examples related to sizeof

How to get the string size in bytes? How can I find the number of elements in an array? What should be the sizeof(int) on a 64-bit machine? How to specify 64 bit integers in c what is the size of an enum type data in C++? What is the size of a pointer? Element count of an array in C++ Printing a char with printf How to get the size of a JavaScript object? What's sizeof(size_t) on 32-bit vs the various 64-bit data models?