size of int

i think that in c++ every int takes 2 Bytes of the ram.
but in this program output shows 4 Bytes
i'll be thankful if someone help

#include "iostream"
#include "conio.h"
using namespace std;
int main()
{
cout<<"The size is "<<(sizeof(int));
return 0;
}
--------
output
The size is 4
--------
why does this happen?
i think that in c++ every int takes 2 Bytes of the ram.


You think wrong.

'int' must be at least 2 bytes, but can be larger. From what I understand it is typically whatever size is most 'natural' for the OS to work with so it gets peak performance. On modern computers that would be at least 32-bit... possibly 64-bit (though I think most implementations are still 32-bit ints).
It's going to be 4 on most computers these days.

However, the size of int in C or C++ is not standardized. It could be different from computer to computer, so don't rely on int always being 32-bit.

If you need exact sizes, use types found in <cstdint> (ex: int16_t)
thank you i got it
Topic archived. No new replies allowed.