views:

306

answers:

11

How many bytes would an int contain and how many would a long contain?

Context:

  • C++
  • 32 bit computer
  • Any difference on a 64-bit computer?
+8  A: 

See the wikipedia article about it.

changelog
A: 

Depends on the language.

chris
+1  A: 

It depends on the compiler.

On a 32 bit system, both int and long contain 32 bits. On a 16 bit system, int is 16 bits and long is 32.

There are other combinations!

MrZebra
+1  A: 

[Please delete]

You should edit your original question so others don't have to search out your thread in order to answer your question.
TomC
@Tal, you can delete it yourself. it's your answer
Nathan Fellman
A: 

It depends on your compiler. And your language, for that matter. Try asking a more specific question.

Mark Bessey
+7  A: 

it is platform and compiler specific. do sizeof(int) and sizeof(long) in c or c++.

Daniel A. White
+5  A: 

(I assume you're talking about C/C++)

It's implementation dependant, but this rule should be always valid:

sizeof(short) <= sizeof(int) <= sizeof(long)

friol
A: 

That depends greatly on the language you are using.

In C, "int" will always be the word length of the processor. So 32 bits or 4 bytes on a 32 bit architecture.

Ryan
A: 

[Please delete]

@Tal: Please stop answering. You need to edit your question instead. I have done it for you.
Geoffrey Chetwood
+1  A: 

Hi,

I think it depends on the hardware your using. on 32-bit platforms it is typically 4 bytes for both int and long. in C you can use the sizeof() operator to find out.

int intBytes;
long longBytes;
intBytes= sizeof(int);
longBytes = sizeof(long);

I'm not sure if long becomes 8 bytes on 64-bit architectures or if it stays as 4.

Dave Turvey
+2  A: 

As others have said endlessly, it depends on the compiler you're using (and even the compiler options that you select).

However, in practice, with compilers for many 32-bit machines, you will find:-

  • char: 8 bit
  • short: 16 bit
  • int: 32-bit
  • long: 32-bit
  • long long: 64-bit ( if supported)

The C standard basiucally says that a long can't be shorter than an int which can't be shorter than a short, etc...

For 64-bit CPUs, those often don't change, but you MUST beware that pointers and ints are frequently not the same size:

 sizeof(int) != sizeof(void*)
Roddy
Actually it does change for 64 bit Unix: long is 64-bit there.
Nemanja Trifunovic